DDL: maintaining structure of database. (CREATE, DROP, ALTER,
RENAME)
DML: maintaining contents of database. (SELECT, INSERT, DELETE,
UPDATE)
sql language:
INSERT
注:字符为单引号
INSERT INTO tablename {column1,column2,…}values(exp1,exp2,…);
//插入
DELETE
UPDATE
CREATE
CREATE的时候,先CREATE没有外键的表,最后CREATE有外键的表
假设我们有一个主键列为ID的表,表中有字段k,并且在k上有索引。这个表的建表语句是:
1 2 3 4 5 6
createtable T ( id intprimary key, k intnotnull, name varchar(16), index (k) ) ENGINE=InnoDB;
DROP
DROP的时候,先DROP有外键的表,最后DROP没有外键的表
ALTER
例如增加一列
RENAME
语法顺序与执行顺序
相当于就是 SELECT [DISTINCT] 的顺序换了一下。
Aggregate Functions
操作符
找最大值
例一:哪一个物品有最高的cost?
If two different items have highest price,
第一个query会把两个都返回,第二个query只会返回一个
例二:找出有最多空房间的楼层
http://sqlfiddle.com/#!9/492887/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14
SELECT blockfloor as "Floor", count(*) AS "No of available rooms" FROM room WHERE unavailable='0' GROUPBY blockfloor HAVINGcount(*) = (SELECTmax(zz) AS highest_total FROM ( SELECT blockfloor , count(*) AS zz FROM room WHERE unavailable='0' GROUPBY blockfloor ) AS t );
注:以下写法错误:
1 2 3 4 5 6 7 8 9
SELECT BlockFloor, COUNT(*) AS "maximum number of rooms available" FROM Room WHERE Unavailable=0 GROUPBY BlockFloor HAVINGCOUNT(*) = (SELECTMAX(COUNT(*)) FROM Room WHERE Unavailable =0 GROUPBY BlockFloor);