1.創建和管理表(DDL):
1.創建表:
CRAETE TABLE 表名(
字段名 數據類型(長度) 約束條件,
...........
);
2.刪除表
drop table +表名;
3.修改表結構:
(1)刪除表中字段:
alter table +表名 drop column +列名;
(2)添加字段:
alter table 表名 add 要添加的字段名 數據類型(長度);
(3)解除表約束:
先找到表的約束名稱,執行: select * from user_constraints; 其中 CONSTRAINT_NAME 爲表的約束名稱 然後刪除約束: alter table 表名 drop constraint 約束名 cascade; mysql中刪除約束的語句是: 刪除主鍵約束: alter table 表名 drop primary key; 刪除外鍵約束: alter table 表名 drop foreign key 外鍵(區分大小寫);
操作完以後,自動 commit;
alter table 表名 drop constraint 約束名
主鍵(PRIMARY KEY)
外鍵(FOREIGN KEY)
唯一性(UNIQUE)
非空(NOT NULL)
默認值(DEFAULT)
2.約束條件:
主鍵 約束: contraint 表名_列名_pk primary key:
唯一性約束: contraint 表名_列名_uk unique:
非空約束: contraint 表名_列名_n not null
3.表操作:
增添一條記錄: insert into [表名](要添加數據的字段) values(對應字段值);
修改一條記錄: update [表名] set 要修改的字段=值,... where ....
刪除一條記錄: delete [表名] where ....
查詢一條記錄: select .... from … where ….
4.分組函數:group by
group by + group_concat()
(1) group_concat(字段名)可以作爲一個輸出字段來使用,
(2) 表示分組之後,根據分組結果,使用group_concat()來放置每一組的某字段的值的集合
select sex,group_concat(name) from employee group by sex;
+------+----------------------------+
| sex | group_concat(name) |
+------+-----------------------------+
| 女 | 李四 |
+-----+--------------------|
| 男 | 張三,王五,Aric |
+------+-----------------------------+
group by + 集合函數
(1) 通過group_concat()的啓發,我們既然可以統計出每個分組的某字段的值的集合,那麼我們也可以通過集合函數來對這個"值的集合"做一些操作
select sex,group_concat(age) from employee group by sex;
+------+-------------------+
| sex | group_concat(age) |
+------+-------------------+
| 女 | 24 |
| 男 | 26,25,15 |
+------+-------------------+
分別統計性別爲男/女的人年齡平均值
select sex,avg(age) from employee group by sex;
+------+----------+
| sex | avg(age) |
+------+----------+
| 女 | 24.0000 |
| 男 | 22.0000 |
+------+----------+
分別統計性別爲男/女的人的個數
select sex,count(sex) from employee group by sex;
+------+------------+
| sex | count(sex) |
+------+------------+
| 女 | 1 |
| 男 | 3 |
+------+------------+
group by + having
(1) having 條件表達式:用來分組查詢後指定一些條件來輸出查詢結果
(2) having作用和where一樣,但having只能用於group by
select sex,count(sex) from employee group by sex having count(sex)>2;
+------+------------+
| sex | count(sex) |
+------+------------+
| 男 | 3 |
+------+------------+
group by + with rollup
(1) with rollup的作用是:在最後新增一行,來記錄當前列裏所有記錄的總和
select sex,count(age) from employee group by sex with rollup;
+------+------------+
| sex | count(age) |
+------+------------+
| 女 | 1 |
| 男 | 3 |
| NULL | 4 |
+------+------------+
select sex,group_concat(age) from employee group by sex with rollup;
+------+-------------------+
| sex | group_concat(age) |
+------+-------------------+
| 女 | 24 |
| 男 | 26,25,15 |
| NULL | 24,26,25,15 |
+------+-------------------+
排序: order by 字段名 asc(升序)/desc(降序)
5.數據庫常用函數:
二、聚合函數(常用於GROUP BY從句的SELECT查詢中)
1.AVG(col)返回指定列的平均值
SUM函數的語法是: SELECT AVG(expression ) FROM 表名 WHERE 字段名;
2.COUNT(col)返回指定列中非NULL值的個數
COUNT函數的語法是: SELECT COUNT(expression ) FROM tables WHERE predicates;
3.MIN(col)返回指定列的最小值
MIN函數的語法是: SELECT MIN(expression ) FROM tables WHERE predicates;
4.MAX(col)返回指定列的最大值
MAX函數的語法是: SELECT MAX(expression ) FROM tables WHERE predicates;
5.SUM(col)返回指定列的所有值之和
SUM函數的語法是: SELECT SUM(expression ) FROM tables WHERE predicates;