當(dāng)前備份數(shù)據(jù)庫(kù):http://pan.baidu.com/s/1nvzA68p
一祟辟、登錄數(shù)據(jù)庫(kù)
在此就不多敘述了
二喧笔、輸入查詢
這是一個(gè)簡(jiǎn)單的命令轰豆,要求服務(wù)器告訴它的版本號(hào)和當(dāng)前日期迷殿。在mysql>提示輸入如下命令并按回車鍵:
查詢版本號(hào)乱顾,當(dāng)前日期 select version(),current_date;
mysql> select version(),current_date;
+------------+--------------+
| version() | current_date |
+------------+--------------+
| 5.7.17-log | 2017-06-07 |
+------------+--------------+
1 row in set (0.00 sec)
不必全在一個(gè)行內(nèi)給出一個(gè)命令盟戏,較長(zhǎng)命令可以輸入到多個(gè)行中绪妹。mysql通過(guò)尋找終止分號(hào)而不是輸入行的結(jié)束來(lái)決定語(yǔ)句在哪兒結(jié)束。(換句話說(shuō)柿究,mysql接受自由格式的輸入:它收集輸入行但直到看見分號(hào)才執(zhí)行邮旷。)
這里是一個(gè)簡(jiǎn)單的多行語(yǔ)句的例子:
* mysql> SELECT
-> USER()
-> ,
-> CURRENT_DATE;
+---------------+--------------+
| USER() | CURRENT_DATE |
+---------------+--------------+
| jon@localhost | 2005-10-11 |
+---------------+--------------+
如果你決定不想執(zhí)行正在輸入過(guò)程中的一個(gè)命令匕坯,輸入/c取消它:
* mysql> SELECT
-> USER()
-> /c
下表顯示出可以看見的各個(gè)提示符并簡(jiǎn)述它們所表示的mysql的狀態(tài):
提示符 含義
mysql> 準(zhǔn)備好接受新命令俱箱。
-> 等待多行命令的下一行。
'> 等待下一行舵鳞,等待以單引號(hào)(“'”)開始的字符串的結(jié)束探入。
"> 等待下一行狡孔,等待以雙引號(hào)(“"”)開始的字符串的結(jié)束。
> 等待下一行蜂嗽,等待以反斜點(diǎn)(‘
’)開始的識(shí)別符的結(jié)束苗膝。
/> 等待下一行,等待以/開始的注釋的結(jié)束植旧。
查詢用戶 select user();
* mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
三辱揭、創(chuàng)建并使用數(shù)據(jù)庫(kù)
使用SHOW語(yǔ)句找出服務(wù)器上當(dāng)前存在什么數(shù)據(jù)庫(kù):
顯示數(shù)據(jù)庫(kù) show databases;
* mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| test |
| world |
+--------------------+
7 rows in set (0.00 sec)
如果test數(shù)據(jù)庫(kù)存在,嘗試訪問它:
使用數(shù)據(jù)庫(kù) use test
* mysql> use test
Database changed
創(chuàng)建數(shù)據(jù)庫(kù)并使用 create database student; use student
* mysql> create database student;
Query OK, 1 row affected (0.01 sec)
創(chuàng)建數(shù)據(jù)庫(kù)并不表示選定并使用它病附,你必須明確地操作问窃。為了使student成為當(dāng)前的數(shù)據(jù)庫(kù),使用這個(gè)命令:
* mysql> use student
Database changed
數(shù)據(jù)庫(kù)只需要?jiǎng)?chuàng)建一次完沪,但是必須在每次啟動(dòng)mysql會(huì)話時(shí)在使用前先選擇它域庇。你可以根據(jù)上面的例子執(zhí)行一個(gè)USE語(yǔ)句來(lái)實(shí)現(xiàn)嵌戈。還可以在調(diào)用mysql時(shí),通過(guò)命令行選擇數(shù)據(jù)庫(kù)听皿,只需要在提供連接參數(shù)之后指定數(shù)據(jù)庫(kù)名稱熟呛。例如:
* shell> mysql -h host -u user -p menagerie
Enter password: ********
四、創(chuàng)建表
創(chuàng)建數(shù)據(jù)庫(kù)是很容易的部分尉姨,但是在這時(shí)它是空的庵朝,正如SHOW TABLES將告訴你的:
顯示表 show tables;
* mysql> show tables;
Empty set (0.00 sec)
***
創(chuàng)建表
使用一個(gè)CREATE TABLE語(yǔ)句指定你的數(shù)據(jù)庫(kù)表的布局:
* mysql> create table student(
-> name varchar(20),
-> age int,
-> sex char(1),
-> birth date,
-> death date);
Query OK, 0 rows affected (0.07 sec)
顯示表結(jié)構(gòu) describe student;
為了驗(yàn)證你的表是按你期望的方式創(chuàng)建,使用一個(gè)DESCRIBE語(yǔ)句:
* mysql> describe student;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
插入一條數(shù)據(jù)
mysql> insert into student
-> values('hsy',18,'f','1999-09-09',null);
Query OK, 1 row affected (0.01 sec)
五又厉、從表檢索信息
查詢所有數(shù)據(jù)
mysql> select * from student;
+------+------+------+------------+-------+
| name | age | sex | birth | death |
+------+------+------+------------+-------+
| hsy | 18 | f | 1999-09-09 | NULL |
+------+------+------+------------+-------+
1 row in set (0.00 sec)
選擇特殊行
mysql> select * from student where name='hsy';
+------+------+------+------------+-------+
| name | age | sex | birth | death |
+------+------+------+------------+-------+
| hsy | 18 | f | 1999-09-09 | NULL |
+------+------+------+------------+-------+
1 row in set (0.00 sec)
mysql> select * from student where birth>'1999-1-1'and sex='f';
+------+------+------+------------+-------+
| name | age | sex | birth | death |
+------+------+------+------------+-------+
| hsy | 18 | f | 1999-09-09 | NULL |
+------+------+------+------------+-------+
1 row in set (0.00 sec)
選擇特殊列
mysql> select name from student;
+------+
| name |
+------+
| hsy |
| hsy |
| hsy |
+------+
3 rows in set (0.00 sec)
請(qǐng)注意該查詢只是簡(jiǎn)單地檢索每個(gè)記錄的owner列九府,并且他們中的一些出現(xiàn)多次。為了使輸出減到最少覆致,增加關(guān)鍵字DISTINCT檢索出每個(gè)唯一的輸出記錄
mysql> select distinct name from student;
+------+
| name |
+------+
| hsy |
+------+
1 row in set (0.00 sec)
分類行
mysql> select * from student order by birth;
+------+------+------+------------+------------+
| name | age | sex | birth | death |
+------+------+------+------------+------------+
| hsy | 18 | m | 1989-09-09 | NULL |
| hh | 19 | f | 1994-03-04 | 1992-02-01 |
| hh | 19 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hrr | 29 | f | 1997-03-04 | 1992-02-01 |
| rrrr | 29 | f | 1997-03-04 | 1992-02-01 |
| rrrr | 29 | 男 | 1997-03-04 | 1992-02-01 |
| rrrr | 29 | 女 | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hsy | 18 | f | 1999-09-09 | NULL |
| hsy | 18 | m | 1999-09-09 | NULL |
+------+------+------+------------+------------+
15 rows in set (0.00 sec)
mysql> select * from student order by birth desc;
+------+------+------+------------+------------+
| name | age | sex | birth | death |
+------+------+------+------------+------------+
| hsy | 18 | f | 1999-09-09 | NULL |
| hsy | 18 | m | 1999-09-09 | NULL |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hrr | 29 | f | 1997-03-04 | 1992-02-01 |
| rrrr | 29 | f | 1997-03-04 | 1992-02-01 |
| rrrr | 29 | 男 | 1997-03-04 | 1992-02-01 |
| rrrr | 29 | 女 | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 19 | f | 1994-03-04 | 1992-02-01 |
| hh | 19 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1994-03-04 | 1992-02-01 |
| hsy | 18 | m | 1989-09-09 | NULL |
+------+------+------+------------+------------+
15 rows in set (0.00 sec)
日期計(jì)算
curdate()當(dāng)前系統(tǒng)日期
要想確定每個(gè)student有多大昔逗,可以計(jì)算當(dāng)前日期的年和出生日期之間的差。如果當(dāng)前日期的日歷年比出生日期早篷朵,則減去一年勾怒。以下查詢顯示了每個(gè)student的出生日期、當(dāng)前日期和年齡數(shù)值的年數(shù)字声旺。
mysql> select name ,birth, curdate(),
-> (year(curdate())-year(birth))-(right(curdate(),5)<right(birth,5)) as age
-> from student;
+------+------------+------------+------+
| name | birth | curdate() | age |
+------+------------+------------+------+
| hsy | 1999-09-09 | 2017-06-07 | 17 |
| hsy | 1999-09-09 | 2017-06-07 | 17 |
| hsy | 1989-09-09 | 2017-06-07 | 27 |
| hh | 1994-03-04 | 2017-06-07 | 23 |
| hh | 1994-03-04 | 2017-06-07 | 23 |
| hh | 1994-03-04 | 2017-06-07 | 23 |
| hh | 1994-03-04 | 2017-06-07 | 23 |
| hh | 1997-03-04 | 2017-06-07 | 20 |
| hh | 1997-03-04 | 2017-06-07 | 20 |
| hrr | 1997-03-04 | 2017-06-07 | 20 |
| rrrr | 1997-03-04 | 2017-06-07 | 20 |
| rrrr | 1997-03-04 | 2017-06-07 | 20 |
| rrrr | 1997-03-04 | 2017-06-07 | 20 |
| hh | 1997-03-04 | 2017-06-07 | 20 |
| hh | 1997-03-04 | 2017-06-07 | 20 |
+------+------------+------------+------+
15 rows in set (0.00 sec)
此處笔链,YEAR()提取日期的年部分,RIGHT()提取日期的MM-DD (日歷年)部分的最右面5個(gè)字符腮猖。比較MM-DD值的表達(dá)式部分的值一般為1或0鉴扫,如果CURDATE()的年比birth的年早,則年份應(yīng)減去1澈缺。整個(gè)表達(dá)式有些難懂坪创,使用alias (age)來(lái)使輸出的列標(biāo)記更有意義。
找出下個(gè)月生日的student也是容易的姐赡。假定當(dāng)前月是8月莱预,那么月值是8,你可以找在9月出生的動(dòng)物 (9月)项滑,方法是:
mysql> select name ,birth
-> from student
-> where month(birth)=9;
+------+------------+
| name | birth |
+------+------------+
| hsy | 1999-09-09 |
| hsy | 1999-09-09 |
| hsy | 1989-09-09 |
+------+------------+
3 rows in set (0.00 sec)
如果當(dāng)前月份是12月依沮,就有點(diǎn)復(fù)雜了。你不能只把1加到月份數(shù)(12)上并尋找在13月出生的枪狂,因?yàn)闆]有這樣的月份危喉。相反,你應(yīng)尋找在1月出生的(1月) 州疾。
你甚至可以編寫查詢辜限,不管當(dāng)前月份是什么它都能工作。采用這種方法不必在查詢中使用一個(gè)特定的月份严蓖,DATE_ADD( )允許在一個(gè)給定的日期上加上時(shí)間間隔薄嫡。如果在NOW( )值上加上一個(gè)月氧急,然后用MONTH()提取月份,結(jié)果產(chǎn)生生日所在月份:
mysql> select name ,birth
-> from student
-> where month(birth)=month(date_add(curdate(),interval 1 month));
+----------+------------+
| name | birth |
+----------+------------+
| xiaomimg | 1997-07-04 |
| hong | 1997-07-08 |
+----------+------------+
2 rows in set (0.00 sec)
完成該任務(wù)的另一個(gè)方法是加1以得出當(dāng)前月份的下一個(gè)月(在使用取模函數(shù)(MOD)后岂座,如果月份當(dāng)前值是12,則“回滾”到值0):
mysql> select name ,birth
-> from student
-> where month(birth)=mod(month(curdate()),12)+1;
+----------+------------+
| name | birth |
+----------+------------+
| xiaomimg | 1997-07-04 |
| hong | 1997-07-08 |
+----------+------------+
2 rows in set (0.00 sec)
注意杭措,MONTH返回在1和12之間的一個(gè)數(shù)字费什,且MOD(something,12)返回在0和11之間的一個(gè)數(shù)字,因此必須在MOD( )以后加1手素,否則我們將從11月( 11 )跳到1月(1)鸳址。
NULL值操作
相反使用IS NULL和IS NOT NULL操作符:
mysql> select name , death is null from student;
+----------+---------------+
| name | death is null |
+----------+---------------+
| hsy | 1 |
| hsy | 1 |
| hsy | 1 |
| hh | 0 |
| hh | 0 |
| hh | 0 |
| hh | 0 |
| hh | 0 |
| hh | 0 |
| hrr | 0 |
| rrrr | 0 |
| rrrr | 0 |
| rrrr | 0 |
| hh | 0 |
| hh | 0 |
| xiaomimg | 0 |
| hong | 0 |
+----------+---------------+
17 rows in set (0.00 sec)
mysql> select name , death is null,death is not null from student;
+----------+---------------+-------------------+
| name | death is null | death is not null |
+----------+---------------+-------------------+
| hsy | 1 | 0 |
| hsy | 1 | 0 |
| hsy | 1 | 0 |
| hh | 0 | 1 |
| hh | 0 | 1 |
| hh | 0 | 1 |
| hh | 0 | 1 |
| hh | 0 | 1 |
| hh | 0 | 1 |
| hrr | 0 | 1 |
| rrrr | 0 | 1 |
| rrrr | 0 | 1 |
| rrrr | 0 | 1 |
| hh | 0 | 1 |
| hh | 0 | 1 |
| xiaomimg | 0 | 1 |
| hong | 0 | 1 |
+----------+---------------+-------------------+
17 rows in set (0.00 sec)
模式匹配
SQL模式匹配允許你使用“_”匹配任何單個(gè)字符,而“%”匹配任意數(shù)目字符(包括零字符)泉懦。在MySQL中稿黍,SQL的模式默認(rèn)是忽略大小寫的。下面給出一些例子崩哩。注意使用SQL模式時(shí)巡球,不能使用=或!=;而應(yīng)使用LIKE或NOT LIKE比較操作符
找出含h的
mysql> select * from student where name like '%h%';
+------+------+------+------------+------------+
| name | age | sex | birth | death |
+------+------+------+------------+------------+
| hsy | 18 | f | 1999-09-09 | NULL |
| hsy | 18 | m | 1999-09-09 | NULL |
| hsy | 18 | m | 1989-09-09 | NULL |
| hh | 19 | f | 1994-03-04 | 1992-02-01 |
| hh | 19 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hrr | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hong | 29 | 女 | 1997-07-08 | 1992-02-01 |
+------+------+------+------------+------------+
13 rows in set (0.00 sec)
長(zhǎng)度是三個(gè)的
mysql> select * from student where name like '_ _ _';
+------+------+------+------------+------------+
| name | age | sex | birth | death |
+------+------+------+------------+------------+
| hsy | 18 | f | 1999-09-09 | NULL |
| hsy | 18 | m | 1999-09-09 | NULL |
| hsy | 18 | m | 1989-09-09 | NULL |
| hrr | 29 | f | 1997-03-04 | 1992-02-01 |
+------+------+------+------------+------------+
4 rows in set (0.00 sec)
由MySQL提供的模式匹配的其它類型是使用擴(kuò)展正則表達(dá)式邓嘹。當(dāng)你對(duì)這類模式進(jìn)行匹配測(cè)試時(shí)酣栈,使用REGEXP和NOT REGEXP操作符(或RLIKE和NOT RLIKE,它們是同義詞)汹押。
以h開頭(like 'h%')
mysql> select * from student where name regexp '^h';
+------+------+------+------------+------------+
| name | age | sex | birth | death |
+------+------+------+------------+------------+
| hsy | 18 | f | 1999-09-09 | NULL |
| hsy | 18 | m | 1999-09-09 | NULL |
| hsy | 18 | m | 1989-09-09 | NULL |
| hh | 19 | f | 1994-03-04 | 1992-02-01 |
| hh | 19 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hrr | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hong | 29 | 女 | 1997-07-08 | 1992-02-01 |
+------+------+------+------------+------------+
13 rows in set (0.00 sec)
以g結(jié)尾(like ''%g')
mysql> select * from student where name regexp 'g$';
+----------+------+------+------------+------------+
| name | age | sex | birth | death |
+----------+------+------+------------+------------+
| xiaomimg | 29 | 女 | 1997-07-04 | 1992-02-01 |
| hong | 29 | 女 | 1997-07-08 | 1992-02-01 |
+----------+------+------+------------+------------+
2 rows in set (0.00 sec)
含h的(like '%h%')
mysql> select * from student where name regexp 'h';
+------+------+------+------------+------------+
| name | age | sex | birth | death |
+------+------+------+------------+------------+
| hsy | 18 | f | 1999-09-09 | NULL |
| hsy | 18 | m | 1999-09-09 | NULL |
| hsy | 18 | m | 1989-09-09 | NULL |
| hh | 19 | f | 1994-03-04 | 1992-02-01 |
| hh | 19 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1994-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hrr | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hh | 29 | f | 1997-03-04 | 1992-02-01 |
| hong | 29 | 女 | 1997-07-08 | 1992-02-01 |
+------+------+------+------------+------------+
13 rows in set (0.00 sec)
為了找出包含正好3個(gè)字符的名字矿筝,使用“^”和“$”匹配名字的開始和結(jié)尾,和3個(gè)“.”實(shí)例在兩者之間:
(like '_ _ _')
mysql> select * from student where name regexp '^...$';
+------+------+------+------------+------------+
| name | age | sex | birth | death |
+------+------+------+------------+------------+
| hsy | 18 | f | 1999-09-09 | NULL |
| hsy | 18 | m | 1999-09-09 | NULL |
| hsy | 18 | m | 1989-09-09 | NULL |
| hrr | 29 | f | 1997-03-04 | 1992-02-01 |
+------+------+------+------------+------------+
4 rows in set (0.00 sec)
計(jì)數(shù)行
COUNT(*)函數(shù)計(jì)算行數(shù)棚贾,所以計(jì)算數(shù)目的查詢應(yīng)為:
mysql> select count(*) from student ;
+----------+
| count(*) |
+----------+
| 17 |
+----------+
1 row in set (0.00 sec)
COUNT( )和GROUP BY以各種方式分類你的數(shù)據(jù)窖维。下列例子顯示出進(jìn)行動(dòng)物普查操作的不同方式。
mysql> select name, count(*) from student group by name;
+----------+----------+
| name | count(*) |
+----------+----------+
| hh | 8 |
| hong | 1 |
| hrr | 1 |
| hsy | 3 |
| rrrr | 3 |
| xiaomimg | 1 |
+----------+----------+
6 rows in set (0.00 sec)
使用1個(gè)以上的表
mysql> select student.name ,birth
-> from student ,event
-> where student.name=event.name;
+------+------------+
| name | birth |
+------+------------+
| hsy | 1999-09-09 |
| hsy | 1999-09-09 |
| hsy | 1999-09-09 |
| hsy | 1999-09-09 |
| hsy | 1999-09-09 |
| hsy | 1999-09-09 |
| hsy | 1989-09-09 |
| hsy | 1989-09-09 |
| hsy | 1989-09-09 |
| hong | 1997-07-08 |
| hong | 1997-07-08 |
| hong | 1997-07-08 |
+------+------------+
關(guān)于該查詢要注意的幾件事情:
- FROM子句列出兩個(gè)表妙痹,因?yàn)椴樵冃枰獜膬蓚€(gè)表提取信息铸史。
- 當(dāng)從多個(gè)表組合(聯(lián)結(jié))信息時(shí),你需要指定一個(gè)表中的記錄怎樣能匹配其它表的記錄怯伊。這很簡(jiǎn)單沛贪,因?yàn)樗鼈兌加幸粋€(gè)name列。查詢使用WHERE子句基于name值來(lái)匹配2個(gè)表中的記錄震贵。
- 因?yàn)閚ame列出現(xiàn)在兩個(gè)表中利赋,當(dāng)引用列時(shí),你一定要指定哪個(gè)表猩系。把表名附在列名前即可以實(shí)現(xiàn)媚送。
六、獲得數(shù)據(jù)庫(kù)和表的信息
你已經(jīng)見到了SHOW DATABASES寇甸,它列出由服務(wù)器管理的數(shù)據(jù)庫(kù)塘偎。為了找出當(dāng)前選擇了哪個(gè)數(shù)據(jù)庫(kù)疗涉,使用DATABASE( )函數(shù):
當(dāng)前使用數(shù)據(jù)庫(kù)
mysql> select database();
+------------+
| database() |
+------------+
| student |
+------------+
1 row in set (0.00 sec)
顯示表
為了找出當(dāng)前的數(shù)據(jù)庫(kù)包含什么表(例如,當(dāng)你不能確定一個(gè)表的名字)吟秩,使用這個(gè)命令:
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| event |
| student |
+-------------------+
2 rows in set (0.00 sec)
顯示表結(jié)構(gòu)
如果你想要知道一個(gè)表的結(jié)構(gòu)咱扣,可以使用DESCRIBE命令;它顯示表中每個(gè)列的信息:
mysql> describe event;
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| date | date | YES | | NULL | |
| type | varchar(15) | YES | | NULL | |
| remark | varchar(255) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
七涵防、 常用查詢的例子
下面是一些學(xué)習(xí)如何用MySQL解決一些常見問題的例子闹伪。
在一些例子中,使用數(shù)據(jù)庫(kù)表“shop”來(lái)儲(chǔ)存某個(gè)商人(經(jīng)銷商)的每件物品(物品號(hào))的價(jià)格壮池。假定每個(gè)商人對(duì)每項(xiàng)物品有一個(gè)固定價(jià)格偏瓤,那么(物品,商人)即為該記錄的主關(guān)鍵字椰憋。
創(chuàng)建表
mysql> create table shop(
-> article int(4)unsigned zerofill default'0000'not null,
-> dealer char(20) default''not null,
-> price double(16,2) default '0.00' not null,
-> primary key (article,dealer));
Query OK, 0 rows affected (0.04 sec)
插入數(shù)據(jù)
insert into shop values(1,'A',3.45),(1,'B',3.99),(2,'A',5.22),(2,'B',10.33),(3,'C',1.56),(3,'D',1.23);
Query OK, 6 rows affected (0.01 sec)
查詢數(shù)據(jù)
mysql> select * from shop;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | A | 3.45 |
| 0001 | B | 3.99 |
| 0002 | A | 5.22 |
| 0002 | B | 10.33 |
| 0003 | C | 1.56 |
| 0003 | D | 1.23 |
+---------+--------+-------+
6 rows in set (0.00 sec)
列的最大值
任務(wù):最大的物品號(hào)是什么厅克?”
mysql> select max(article) as article from shop ;
+---------+
| article |
+---------+
| 3 |
+---------+
1 row in set (0.00 sec)
擁有某個(gè)列的最大值的行
任務(wù):找出最貴物品的編號(hào)、銷售商和價(jià)格橙依。
mysql> select article ,dealer,price
-> from shop
-> where price=(select max(price) from shop);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0002 | B | 10.33 |
+---------+--------+-------+
1 row in set (0.02 sec)
另一種方案:解決方案是按價(jià)格降序排序所有行并用MySQL特定LIMIT子句只得到第一行:
mysql> select article ,dealer,price
-> from shop
-> order by price desc
-> limit 1;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0002 | B | 10.33 |
+---------+--------+-------+
1 row in set (0.00 sec)
列的最大值:按組
任務(wù):每項(xiàng)物品的的最高價(jià)格是多少证舟?
mysql> select article ,max(price) as price
-> from shop
-> group by article;
+---------+-------+
| article | price |
+---------+-------+
| 0001 | 3.99 |
| 0002 | 10.33 |
| 0003 | 1.56 |
+---------+-------+
3 rows in set (0.00 sec)
擁有某個(gè)字段的組間最大值的行
任務(wù):對(duì)每項(xiàng)物品,找出最貴價(jià)格的物品的經(jīng)銷商窗骑。
mysql> select article,dealer,price
-> from shop s1
-> where price=(select max(s2.price)
-> from shop s2
-> where s1.article=s2.article);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | B | 10.33 |
| 0003 | C | 1.56 |
+---------+--------+-------+
3 rows in set (0.00 sec)
使用用戶變量
例如褪储,要找出價(jià)格最高或最低的物品的,其方法是:
mysql> select @min_price:=min(price),@max_price:=max(price) from shop;
+------------------------+------------------------+
| @min_price:=min(price) | @max_price:=max(price) |
+------------------------+------------------------+
| 1.23 | 10.33 |
+------------------------+------------------------+
1 row in set (0.00 sec)
mysql> select * from shop where price= @min_price or price= @max_price;;
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0002 | B | 10.33 |
| 0003 | D | 1.23 |
+---------+--------+-------+
2 rows in set (0.00 sec)
使用外鍵
只是聯(lián)接兩個(gè)表時(shí)慧域,不需要外部關(guān)鍵字鲤竹。對(duì)于除InnoDB類型的表,當(dāng)使用REFERENCES tbl_name(col_name)子句定義列時(shí)可以使用外部關(guān)鍵字昔榴,該子句沒有實(shí)際的效果辛藻,只作為備忘錄或注釋來(lái)提醒,你目前正定義的列指向另一個(gè)表中的一個(gè)列互订。執(zhí)行該語(yǔ)句時(shí)吱肌,實(shí)現(xiàn)下面很重要:
· MySQL不執(zhí)行表tbl_name 中的動(dòng)作,例如作為你正定義的表中的行的動(dòng)作的響應(yīng)而刪除行仰禽;換句話說(shuō)氮墨,該句法不會(huì)致使ON DELETE或ON UPDATE行為(如果你在REFERENCES子句中寫入ON DELETE或ON UPDATE子句,將被忽略)吐葵。
創(chuàng)建表
CREATE TABLE person (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
name CHAR(60) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE shirt (
id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
style ENUM('t-shirt', 'polo', 'dress') NOT NULL,
color ENUM('red', 'blue', 'orange', 'white', 'black') NOT NULL,
owner SMALLINT UNSIGNED NOT NULL REFERENCES person(id),
PRIMARY KEY (id)
);
INSERT INTO person VALUES (NULL, 'Antonio Paz');
SELECT @last := LAST_INSERT_ID();
INSERT INTO shirt VALUES
(NULL, 'polo', 'blue', @last),
(NULL, 'dress', 'white', @last),
(NULL, 't-shirt', 'blue', @last);
INSERT INTO person VALUES (NULL, 'Lilliana Angelovska');
SELECT @last := LAST_INSERT_ID();
INSERT INTO shirt VALUES
(NULL, 'dress', 'orange', @last),
(NULL, 'polo', 'red', @last),
(NULL, 'dress', 'blue', @last),
(NULL, 't-shirt', 'white', @last);
mysql> select * from person;
+----+---------------------+
| id | name |
+----+---------------------+
| 1 | Antonio Paz |
| 2 | Lilliana Angelovska |
+----+---------------------+
2 rows in set (0.00 sec)
mysql> select * from shirt;
+----+---------+--------+-------+
| id | style | color | owner |
+----+---------+--------+-------+
| 1 | polo | blue | 1 |
| 2 | dress | white | 1 |
| 3 | t-shirt | blue | 1 |
| 4 | dress | orange | 2 |
| 5 | polo | red | 2 |
| 6 | dress | blue | 2 |
| 7 | t-shirt | white | 2 |
+----+---------+--------+-------+
7 rows in set (0.00 sec)
select * from person p,shirt s
-> where p.id=s.owner and p.name like 'Anton%'and s.color<>'white';
+----+-------------+----+---------+-------+-------+
| id | name | id | style | color | owner |
+----+-------------+----+---------+-------+-------+
| 1 | Antonio Paz | 1 | polo | blue | 1 |
| 1 | Antonio Paz | 3 | t-shirt | blue | 1 |
+----+-------------+----+---------+-------+-------+
2 rows in set (0.00 sec)
根據(jù)兩個(gè)鍵搜索
每個(gè)SELECT只搜索一個(gè)關(guān)鍵字规揪,可以進(jìn)行優(yōu)化:
SELECT field1_index, field2_index
FROM test_table WHERE field1_index = '1'
UNION
SELECT field1_index, field2_index
FROM test_table WHERE field2_index = '1';
根據(jù)天計(jì)算訪問量
CREATE TABLE t1 (year YEAR(4), month INT(2) UNSIGNED ZEROFILL,
day INT(2) UNSIGNED ZEROFILL);
INSERT INTO t1 VALUES(2000,1,1),(2000,1,20),(2000,1,30),(2000,2,2),
(2000,2,23),(2000,2,23);
mysql> select * from t1;
+------+-------+------+
| year | month | day |
+------+-------+------+
| 2000 | 01 | 01 |
| 2000 | 01 | 20 |
| 2000 | 01 | 30 |
| 2000 | 02 | 02 |
| 2000 | 02 | 23 |
| 2000 | 02 | 23 |
+------+-------+------+
示例表中含有代表用戶訪問網(wǎng)頁(yè)的年-月-日值∥虑停可以使用以下查詢來(lái)確定每個(gè)月的訪問天數(shù):
SELECT year,month,BIT_COUNT(BIT_OR(1<<day)) AS days FROM t1
GROUP BY year,month;
將返回:
+------+-------+------+
| year | month | days |
+------+-------+------+
| 2000 | 01 | 3 |
| 2000 | 02 | 2 |
+------+-------+------+
使用AUTO_INCREMENT
可以通過(guò)AUTO_INCREMENT屬性為新的行產(chǎn)生唯一的標(biāo)識(shí):
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO animals (name) VALUES
('dog'),('cat'),('penguin'),
('lax'),('whale'),('ostrich');
SELECT * FROM animals;
將返回:
+----+---------+
| id | name |
+----+---------+
| 1 | dog |
| 2 | cat |
| 3 | penguin |
| 4 | lax |
| 5 | whale |
| 6 | ostrich |
+----+---------+
CREATE TABLE`animals` (
`grp` ENUM('fish', 'mammal', 'bird') NOT NULL,
`id` MEDIUMINT NOT NULL AUTO_INCREMENT,
`name` CHAR(20) NOT NULL,
PRIMARY KEY (`id`, `grp`));
INSERT INTO animals (grp,name) VALUES
('mammal','dog'),('mammal','cat'),
('bird','penguin'),('fish','lax'),('mammal','whale'),
('bird','ostrich');
SELECT * FROM animals ORDER BY grp,id;
將返回
+--------+----+---------+
| grp | id | name |
+--------+----+---------+
| fish | 1 | lax |
| mammal | 1 | dog |
| mammal | 2 | cat |
| mammal | 3 | whale |
| bird | 1 | penguin |
| bird | 2 | ostrich |
+--------+----+---------+
MySql可視化工具M(jìn)ySQL Workbench使用教程
1. MySQL Workbench
MySQL Workbench 為數(shù)據(jù)庫(kù)管理員猛铅、程序開發(fā)者和系統(tǒng)規(guī)劃師提供可視化的Sql開發(fā)、數(shù)據(jù)庫(kù)建模凤藏、以及數(shù)據(jù)庫(kù)管理功能奸忽。
2.MySQL Workbench 的下載和安裝
(1)安裝最新MySql時(shí)堕伪,有是否安裝MySql Workbench的選項(xiàng),可選擇安裝栗菜。
(2)可以獨(dú)立安裝MySql Workbench欠雌。官方下載地址:http://dev.mysql.com/downloads/workbench/ 安裝很簡(jiǎn)單,基本就是一路Next疙筹。
3.MySQL Workbench的功能使用
功能界面:
分為三個(gè)主要功能模塊:Sql Development(Sql開發(fā) 相當(dāng)于Sql2000中的查詢分析器), Data Modeling(數(shù)據(jù)庫(kù)建模), Server Administration(服務(wù)器管理 相當(dāng)于Sql2000中的企業(yè)管理器)
(1) Sql Development的使用
對(duì)應(yīng)的操作分別是:Connection列表(供選擇已經(jīng)建好的數(shù)據(jù)庫(kù)連接)富俄,新建一個(gè)Connection,編輯數(shù)據(jù)庫(kù)表腌歉,編輯SQL腳本蛙酪,Connections管理
點(diǎn)擊New Connection 會(huì)彈出如下操作界面
輸入服務(wù)器的名稱齐苛,端口翘盖,用戶名,和密碼 即可凹蜂。
連接后的操作界面如下:
具體操作SQL2005 SQL2008中的差不多馍驯,這里不再描述。
(2) Data Modeling的使用
Workbench中的數(shù)據(jù)庫(kù)建模我還沒有用到 這里略過(guò) 以后用到了再補(bǔ)充上
(3)Server Administration的使用
對(duì)應(yīng)的功能分別是:服務(wù)器實(shí)例列表玛痊,新建一個(gè)服務(wù)實(shí)例汰瘫,數(shù)據(jù)庫(kù)的導(dǎo)入導(dǎo)出,安全管理擂煞,服務(wù)器列表管理
創(chuàng)建一個(gè)服務(wù)實(shí)例混弥,創(chuàng)建的操作和Sql Development中的創(chuàng)建Connection一樣 輸入服務(wù)器的名稱,端口对省,用戶名蝗拿,和密碼 即可。
創(chuàng)建進(jìn)入服務(wù)實(shí)例管理的功能界面如下:
Management中的功能主要有:
查看服務(wù)器狀態(tài)蒿涎,包括 連接數(shù)量哀托, CUP使用率等
開啟關(guān)閉服務(wù)器實(shí)例 可以開啟或關(guān)閉服務(wù)器實(shí)例,查看運(yùn)行日志
查看服務(wù)實(shí)例日志 包括存儲(chǔ)日志劳秋,錯(cuò)誤日志仓手,通知日志 等
Configuration 服務(wù)器配置 這里的功能我還沒有研究 略過(guò)
Security 服務(wù)實(shí)例安全 這里設(shè)置用戶權(quán)限,角色玻淑,架構(gòu) 和MS SQL的安全一樣
Data Export/Restore 數(shù)據(jù)庫(kù)的導(dǎo)入導(dǎo)出和恢復(fù)功能
數(shù)據(jù)導(dǎo)出的操作:
可以選擇要導(dǎo)出的數(shù)據(jù)庫(kù)和數(shù)據(jù)表嗽冒,已經(jīng)導(dǎo)出選項(xiàng)。這里的導(dǎo)出選項(xiàng)有 導(dǎo)入到一個(gè)文件夾中每個(gè)表對(duì)應(yīng)一個(gè)sql腳本文件還是所有表導(dǎo)入到一個(gè)sql文件中补履,是否丟棄存儲(chǔ)過(guò)程辛慰,是否丟棄Event定時(shí)器,是否清空數(shù)據(jù)
數(shù)據(jù)導(dǎo)入操作:
數(shù)據(jù)導(dǎo)入操作只有兩個(gè)選擇 一是導(dǎo)入一個(gè)文件夾中的所有Sql腳本 還是導(dǎo)入一個(gè)單獨(dú)的Sql腳文件 (分別對(duì)應(yīng)導(dǎo)出的兩個(gè)選項(xiàng))
參考:http://www.cnblogs.com/tiantianbyconan/archive/2012/07/08/2581684.html