一、基本增刪改查
二疹启、創(chuàng)建索引的幾種方式
三古程、單表查詢
四、連表查詢
一喊崖、基本增刪改查
-
1.數(shù)據(jù)庫的增刪改查
show databases;
---> 查看當前都有哪些數(shù)據(jù)庫挣磨。
create database 數(shù)據(jù)庫名;
---> 創(chuàng)建數(shù)據(jù)庫(charset utf8雇逞,這個不寫,為默認編碼)趋急。
show create database 數(shù)據(jù)庫名喝峦;
---> 查看創(chuàng)建的數(shù)據(jù)庫。
select database()呜达;
---> 查看當前所在的庫的位置(在哪個庫里)谣蠢。
use 數(shù)據(jù)庫名;
---> 選擇數(shù)據(jù)庫查近。
drop database 數(shù)據(jù)庫名眉踱;
---> 刪除數(shù)據(jù)庫。
alter database 數(shù)據(jù)庫名 charset gbk霜威;
---> 修改數(shù)據(jù)庫的編碼谈喳。
-
2.表的增刪改查
create table t1(id int,name char(10))戈泼;
--->(engine=innodb婿禽; 不寫默認為innodb引擎),創(chuàng)建表。
insert into t1 values (1大猛,'hehe'),(2,'haha')扭倾;
---> 向表中插入記錄。
select * from t1挽绩;/ select id膛壹,name from t1;
---> 查詢表內(nèi)容唉堪。
desc t1模聋;
---> 查看表結(jié)構(gòu)。
show tables唠亚;
---> 查看當前數(shù)據(jù)庫有哪些表链方。
show create table t1;
---> 查看表的詳細結(jié)構(gòu)趾撵。
show create table t1\G(不帶分號)
---> 也是查看表的詳細結(jié)構(gòu)侄柔,有一丟丟不同。
create table b1 select * from t1(db1.t1)占调;
---> 復制t1表暂题,為b1(可在本數(shù)據(jù)庫或其他數(shù)據(jù)庫復制)。
create table b2 select * from db1.t1 where 1>5 / false究珊;
---> 只復制表結(jié)構(gòu)薪者,不要數(shù)據(jù)。
create table b3 like db1.t1剿涮;
---> 也是只復制表結(jié)構(gòu)言津,不要數(shù)據(jù)攻人。
drop table 表名;
---> 刪除表悬槽。
alter table b1 add name char(11);
---> 增加單個字段怀吻。
alter table b1 add (age int,sex char(11));
---> 增加多個字段;
alter table t1 drop id初婆;
---> 刪除單個字段蓬坡。(不能全部刪除,起碼留一個)磅叛。
alter table b1 drop name,drop age;
---> 刪除多個字段屑咳。
alter table t1 modify name char(6);
---> 修改表中name字段的char屬性弊琴。
alter table t1 change name NAME char(7)兆龙;
---> 修改表中name字段為NAME字段。
update db1.t1 set name='zhangsan';
---> 修改t1中的所有name字段都是‘zhangsan’敲董。
update db1.t1 set name='zhangsan' where id=2;
---> 修改t1中id為2的name字段中紫皇,為‘zhangsan’。
delete from t1;
---> 清空t1表的內(nèi)容腋寨。
delete from t1 where id=2;
---> 清空t1表中id為2的內(nèi)容坝橡。
二、創(chuàng)建索引的幾種方式
1.唯一索引:
mysql> create table t1 (id int,name varchar(10),unique(name));
Query OK, 0 rows affected (0.35 sec)
mysql> desc t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
2.唯一索引方式2:
mysql> create table t5(id int,name varchar(10));
Query OK, 0 rows affected (0.34 sec)
mysql> alter table t5 add unique(id);
Query OK, 0 rows affected (0.27 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t5;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
3.聯(lián)合唯一索引:
mysql> create table t2(id int,name varchar(10),addr varchar(20),unique(name,addr));
Query OK, 0 rows affected (0.34 sec)
mysql> desc t2;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(10) | YES | MUL | NULL | |
| addr | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4.主鍵索引:
mysql> create table t3(id int,name varchar(10),primary key (id));
Query OK, 0 rows affected (0.34 sec)
mysql> desc t3;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
5.主鍵索引方式2:
mysql> create table t4(id int,name varchar(10));
Query OK, 0 rows affected (0.45 sec)
mysql> alter table t4 add primary key (id);
Query OK, 0 rows affected (0.64 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t4;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
6.普通索引:
mysql> create table t6 (id int,name varchar(10));
Query OK, 0 rows affected (0.40 sec)
mysql> alter table t6 add index id_index (id);
Query OK, 0 rows affected (0.28 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t6;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | MUL | NULL | |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
三精置、單表查詢
-
關(guān)鍵字及執(zhí)行優(yōu)先級
from 找到表;
-
where 拿著where指定的約束條件锣杂,去表中取出一條條記錄脂倦;
子句中可以使用(都有哪些約束條件):
1,比較運算符:>元莫、<赖阻、>=、<=踱蠢、!= 2火欧,between 80 and 100:值在80到100之間 3,in(80,90,100):值在括號里 4茎截,like‘haha_’(%苇侵,_是通配符,和正則差不多) like 'hehe%'/ like 'hehe_'/ like '_hehe_'/ like '%hehe%' %任意多 _就一個 5企锌,邏輯運算符:在多個條件直接可以使用邏輯運算符and or not
例:
單條件查詢: select id,name from employee where id > 5; 多條件查詢: select name from employee where post='teacher' and salary>10000; 關(guān)鍵字查詢 between and: select name,salary from employee where salary between 10000 and 20000; 關(guān)鍵字in集合查詢: select name,salary from employee where salary=3000 or salary=4000 or salary=9000; select name,salary from employee where salary in/not in (3000,3500,4000,9000); 關(guān)鍵字like模糊查詢: select * from employee where name like 'zhang%' ; select age from employee where name like 'zhang_';
-
group by 將取出的記錄按grout by()榆浓,進行分組;
分組指的是:將所有記錄按照某個相同字段進行歸類撕攒。在這個類里進行篩選取值陡鹃。 可按照任意字段分組烘浦,但分組完畢后只能查看該字段,如果想查看組內(nèi)信息萍鲸,需要借助聚合函數(shù)闷叉。 聚合函數(shù):max()求最大值 min()求最小值 avg()求平均值 sum() 求和 count() 求總個數(shù) group_concat(name) 分組后name都有誰 必須使用group by才能使用group_concat()函數(shù),將所有的name值連接
例:
每個部門有多少個員工 select post,count(id) from employee group by post; 報錯:不行脊阴。select * from employee where avg(age) > 30; 正常:可以握侧。select * from employee where age > 20 group by post having max(salary) > 10000; 正常:可以。select sum(salary) from employee WHERE depart_id=3;
-
having 將分組的結(jié)果進行having過濾(二次篩選)蹬叭;
where發(fā)生在group by之前藕咏,having發(fā)生在group by之后,可使用聚合函數(shù)秽五。
例: 1.select depart_id,count(id) from employee group by depart_id having depart_id = 3; 2.select depart_id,count(id) from employee group by depart_id having count(id)>7; 3.select max(salary) 最大工資 from employee where id>2 group by depart_id having count(id)>3;
報錯:不行孽查。select post,group_concat(name),count(1) from employee group by post where count(1)<2; 原因:group by 后接 where 報錯,應(yīng)該接 having坦喘。 正常:可以盲再。select post,group_concat(name),count(1) from employee group by post having count(1)<2; group_concat(name):顯示都是誰;
having和where語法上是一樣的瓣铣。
select * from employee where id>15; select * from employee having id>15;
但是having和where不一樣的地方在于以下幾點4鹋蟆!棠笑!
1.where和having的區(qū)別 1. Where 是一個約束聲明梦碗,使用Where約束來自數(shù)據(jù)庫的數(shù)據(jù),Where是在結(jié)果返回之前起作用的 (先找到表蓖救,按照where的約束條件洪规,從表文件中取出數(shù)據(jù)),Where中不能使用聚合函數(shù) 2. Having是一個過濾聲明循捺,是在查詢返回結(jié)果集以后對查詢結(jié)果進行的過濾操作 (先找到表斩例,按照where的約束條件,從表文件中取出數(shù)據(jù)从橘,然后group by分組念赶, 如果沒有g(shù)roup by則所有記錄整體為一組,然后執(zhí)行聚合函數(shù)恰力,然后使用having對聚合的結(jié)果進行過濾)整陌, 在Having中可以使用聚合函數(shù)驾窟。 3. where的優(yōu)先級比having的優(yōu)先級高 4. having可以放到group by之后蔚龙,而where只能放到group by 之前毅整。
select 執(zhí)行select,選擇結(jié)果;
-
order by 將結(jié)果按條件排序翻具;
按單列排序 升序:SELECT * FROM employee ORDER BY age; 升序:SELECT * FROM employee ORDER BY age ASC; 降序:SELECT * FROM employee ORDER BY age DESC; 按多列排序 先按照age升序排序履怯,如果年紀相同,則按照id降序 SELECT * from employee ORDER BY age ASC, id DESC;
-
limit 限制結(jié)果的顯示條數(shù)裆泳。
select * from employee order by salary desc limit 0,5 從第0開始叹洲,即先查詢出第一條,然后包含這一條在內(nèi)往后查5條 SELECT * FROM employee ORDER BY salary DESC LIMIT 5,5; 從第5開始工禾,即先查詢出第6條运提,然后包含這一條在內(nèi)往后查5條 limit 0,5 / limit 5 起始位置闻葵,查詢個數(shù)民泵。 起始位置默認從0開始,從0往后算(按索引就是0也算槽畔,按第幾個就是0不算)栈妆。
四、連表查詢
-
1.認識笛卡爾積厢钧,將兩張表強連接到一起
mysql> select * from department;
+------+--------------+
| id | name |
+------+--------------+
| 200 | 技術(shù) |
| 201 | 人力資源 | 表一
| 202 | 銷售 |
| 203 | 運營 |
+------+--------------+
4 rows in set (0.08 sec)
mysql> select * from employee;
+----+----------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+----------+--------+------+--------+
| 1 | aaaaa | male | 18 | 200 |
| 2 | bbbbb | female | 48 | 201 |
| 3 | ccccc | male | 38 | 201 | 表二
| 4 | xxxxx | female | 28 | 202 |
| 5 | yyyyy | male | 18 | 200 |
| 6 | zzzzz | female | 18 | 204 |
+----+----------+--------+------+--------+
6 rows in set (0.00 sec)
mysql> select * from employee,department;
+----+----------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+----+----------+--------+------+--------+------+--------------+
| 1 | aaaaa | male | 18 | 200 | 200 | 技術(shù) |
| 1 | aaaaa | male | 18 | 200 | 201 | 人力資源 |
| 1 | aaaaa | male | 18 | 200 | 202 | 銷售 |
| 1 | aaaaa | male | 18 | 200 | 203 | 運營 |
| 2 | bbbbb | female | 48 | 201 | 200 | 技術(shù) |
| 2 | bbbbb | female | 48 | 201 | 201 | 人力資源 |
| 2 | bbbbb | female | 48 | 201 | 202 | 銷售 |
| 2 | bbbbb | female | 48 | 201 | 203 | 運營 |
| 3 | ccccc | male | 38 | 201 | 200 | 技術(shù) |
| 3 | ccccc | male | 38 | 201 | 201 | 人力資源 |
| 3 | ccccc | male | 38 | 201 | 202 | 銷售 | 迪
| 3 | ccccc | male | 38 | 201 | 203 | 運營 |
| 4 | xxxxx | female | 28 | 202 | 200 | 技術(shù) | 卡
| 4 | xxxxx | female | 28 | 202 | 201 | 人力資源 |
| 4 | xxxxx | female | 28 | 202 | 202 | 銷售 | 爾
| 4 | xxxxx | female | 28 | 202 | 203 | 運營 |
| 5 | yyyyy | male | 18 | 200 | 200 | 技術(shù) | 積
| 5 | yyyyy | male | 18 | 200 | 201 | 人力資源 |
| 5 | yyyyy | male | 18 | 200 | 202 | 銷售 |
| 5 | yyyyy | male | 18 | 200 | 203 | 運營 |
| 6 | zzzzz | female | 18 | 204 | 200 | 技術(shù) |
| 6 | zzzzz | female | 18 | 204 | 201 | 人力資源 |
| 6 | zzzzz | female | 18 | 204 | 202 | 銷售 |
| 6 | zzzzz | female | 18 | 204 | 203 | 運營 |
+----+----------+--------+------+--------+------+--------------+
24 rows in set (0.00 sec)
通過上面發(fā)現(xiàn)鳞尔,有許多數(shù)據(jù)是無效的,只有employee的dep_id
=department 的 id
那一列是真實有效的數(shù)據(jù)早直。所以引出了下文↓
寥假。
-
2.inner join內(nèi)連接
mysql> select employee.id, employee.name, employee.age, employee.sex, department.name from employee inner join department on employee.dep_id = department.id;
+----+---------+------+--------+--------------+
| id | name | age | sex | name |
+----+---------+------+--------+--------------+
| 1 | aaaaa | 18 | male | 技術(shù) |
| 2 | bbbbb | 48 | female | 人力資源 | 內(nèi)連接
| 3 | ccccc | 38 | male | 人力資源 |
| 4 | xxxxx | 28 | female | 銷售 |
| 5 | yyyyy | 18 | male | 技術(shù) |
+----+---------+------+--------+--------------+
5 rows in set (0.00 sec)
-
3.左連接(employee 在左)
mysql> select * from employee left join department on employee.dep_id=department.id;
+----+----------+--------+------+--------+------+--------------+
| id | name | sex | age | dep_id | id | name |
+----+----------+--------+------+--------+------+--------------+
| 1 | aaaaa | male | 18 | 200 | 200 | 技術(shù) |
| 5 | yyyyy | male | 18 | 200 | 200 | 技術(shù) |
| 2 | bbbbb | female | 48 | 201 | 201 | 人力資源 |
| 3 | ccccc | male | 38 | 201 | 201 | 人力資源 |
| 4 | xxxxx | female | 28 | 202 | 202 | 銷售 |
| 6 | zzzzz | female | 18 | 204 | NULL | NULL |
+----+----------+--------+------+--------+------+--------------+
6 rows in set (0.00 sec)
-
4.左連接(department在左)
mysql> select * from department left join employee on employee.dep_id=department.id;
+------+--------------+------+---------+--------+------+--------+
| id | name | id | name | sex | age | dep_id |
+------+--------------+------+---------+--------+------+--------+
| 200 | 技術(shù) | 1 | aaaaa | male | 18 | 200 |
| 201 | 人力資源 | 2 | bbbbb | female | 48 | 201 |
| 201 | 人力資源 | 3 | ccccc | male | 38 | 201 |
| 202 | 銷售 | 4 | xxxxx | female | 28 | 202 |
| 200 | 技術(shù) | 5 | yyyyy | male | 18 | 200 |
| 203 | 運營 | NULL | NULL | NULL | NULL | NULL |
+------+--------------+------+---------+--------+------+--------+
6 rows in set (0.00 sec)
-
5.符合條件連接查詢
示例1:以內(nèi)連接的方式查詢employee和department表,并且employee表中的age字段值必須大于25霞扬,
即找出年齡大于25歲的員工以及員工所在的部門
select employee.name,department.name from employee inner join department
on employee.dep_id = department.id
where age > 25;
示例2:以內(nèi)連接的方式查詢employee和department表糕韧,并且以age字段的升序方式顯示。
select employee.id,employee.name,employee.age,department.name from employee,department
where employee.dep_id = department.id
and age > 25
order by age asc;
-
6.子查詢
子查詢是將一個查詢語句嵌套在另一個查詢語句中喻圃。
內(nèi)層查詢語句的查詢結(jié)果兔沃,可以為外層查詢語句提供查詢條件。
子查詢中可以包含:in级及,not in,any额衙,all饮焦,exists,not exists等關(guān)鍵字窍侧。-
帶in關(guān)鍵字的子查詢:
查詢平均年齡在25歲以上的部門名: select id,name from department where id in (select dep_id from employee group by dep_id having avg(age) > 25); 查看技術(shù)部員工姓名: select name from employee where dep_id in (select id from department where name='技術(shù)'); 查看不足1人的部門名: select name from department where id not in (select dep_id from employee group by dep_id);
-
帶exists關(guān)鍵字的子查詢:
EXISTS關(guān)字鍵字表示存在县踢。在使用EXISTS關(guān)鍵字時,內(nèi)層查詢語句不返回查詢的記錄伟件。而是返回一個真假值硼啤。 True或False 當返回True時,外層查詢語句將進行查詢斧账;當返回值為False時谴返,外層查詢語句不進行查詢 department表中存在dept_id=203煞肾,Ture select * from employee where exists (select id from department where id=200); department表中存在dept_id=205,F(xiàn)alse select * from employee where exists (select id from department where id=204);
-
還可以包含比較運算符:=嗓袱,籍救!=,>渠抹,<等蝙昙。
查詢大于所有人平均年齡的員工名與年齡 select name,age from employee where age > (select avg(age) from employee); 查詢大于部門內(nèi)平均年齡的員工名、年齡 思路: (1)先對員工表(employee)中的人員分組(group by)梧却,查詢出dep_id以及平均年齡奇颠。 (2)將查出的結(jié)果作為臨時表,再對根據(jù)臨時表的dep_id和employee的dep_id作為篩選條件將employee表和臨時表進行內(nèi)連接。 (3)最后再將employee員工的年齡是大于平均年齡的員工名字和年齡篩選放航。 select t1.name,t1.age from employee as t1 inner join (select dep_id,avg(age) as avg_age from employee group by dep_id) as t2 on t1.dep_id = t2.dep_id where t1.age > t2.avg_age;
-