DDL: maintaining structure of database. (CREATE, DROP, ALTER,
RENAME)
DML: maintaining contents of database. (SELECT, INSERT, DELETE,
UPDATE)
sql language:
1578825616684
INSERT
1578900159995
注:字符为单引号
INSERT INTO tablename {column1,column2,…}values(exp1,exp2,…);
//插入
DELETE
1578900315795
UPDATE
1578900364614
CREATE
1578899962419
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
1578900950689
DROP的时候,先DROP有外键的表,最后DROP没有外键的表
ALTER
例如增加一列
1578901179360
RENAME
1578901685944
语法顺序与执行顺序
15788256741601578825736061
相当于就是 SELECT [DISTINCT] 的顺序换了一下。
Aggregate Functions
1596791792580
操作符
1596791382343
找最大值
例一:哪一个物品有最高的cost?
1596791476146
If two different items have highest price,
第一个query会把两个都返回,第二个query只会返回一个
例二:找出有最多空房间的楼层
http://sqlfiddle.com/#!9/492887/22
1597394612354
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);