-- ======================== 第三天 =========================== CREATE DATABASE php0408 CHARSET utf8 ;CREATE TABLE php0408.student ( sid INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, sname VARCHAR(18) NOT NULL, sex ENUM(‘male‘,‘female‘) NOT NULL DEFAULT ‘female‘, cid INT UNSIGNED NOT NULL ) ENGINE=MYISAM ;CREATE TABLE php0408.class ( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, cname VARCHAR(10) NOT NULL ) ENGINE=MYISAM ;USE php0408 ;SHOW TABLES ;INSERT INTO php0408.`class` VALUES(NULL,‘php0306‘),(NULL,‘php0420‘),(NULL,‘php1101‘) ;SELECT * FROM php0408.`class` ;INSERT INTO student VALUES(NULL,‘小花‘,1,8) ;SELECT * FROM student ;DROP TABLE student ;DROP TABLE class ;CREATE TABLE php0408.student ( sid INT UNSIGNED PRIMARY KEY AUTO_INCREMENT, sname VARCHAR(18) NOT NULL, sex ENUM(‘male‘,‘female‘) NOT NULL DEFAULT ‘female‘, cid INT UNSIGNED NOT NULL, CONSTRAINT FOREIGN KEY (cid) REFERENCES class(id) ON DELETE RESTRICT ON UPDATE CASCADE ) ENGINE=INNODB ;CREATE TABLE php0408.class ( id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, cname VARCHAR(10) NOT NULL ) ENGINE=INNODB ;SHOW CREATE TABLE student #创建外键 ;ALTER TABLE student ADD CONSTRAINT `student_cid_class_id` FOREIGN KEY (cid) REFERENCES class(id) #删除外键,要使用外键的名字 ;ALTER TABLE student DROP FOREIGN KEY `student_cid_class_id` ;SELECT * FROM student ;UPDATE student SET cid=1 WHERE sid=1 ;SELECT * FROM class # 附表创建记录时,必须保证外键字段数据在主表中存在 ;INSERT INTO student VALUES(NULL,‘小芳‘,2,8) # 删除主表主键的时候,会检查外键约束,如果别的表依赖该记录,则不能删除 ;DELETE FROM class WHERE id=1 #可以设置主表记录发生变化的时候,关联表做什么操作:比如修改主表记录,关联表相应修改,删除主表记录,关联表置空 ;ALTER TABLE student ADD CONSTRAINT `student_cid_class_id` FOREIGN KEY (cid) REFERENCES class(id) ON DELETE SET NULL ON UPDATE CASCADE ;UPDATE class SET id=0 WHERE id=1 ;DELETE FROM class WHERE id=0 ;CREATE TABLE nf1( NAME CHAR(18), sex CHAR(1), class CHAR(10), room CHAR(10), days INT, dksj CHAR(20) ) ;DESC nf1 ;ALTER TABLE nf1 CHANGE COLUMN dksj `start` DATETIME ;ALTER TABLE nf1 ADD COLUMN `end` DATETIME ;INSERT INTO nf1 VALUES(‘四哥‘,‘男‘,‘php0408‘,‘603‘,20,‘2015-04-08‘,‘2015-05-08‘) ;SELECT * FROM nf1 ;CREATE TABLE nf2( NAME CHAR(18), sex CHAR(1), class CHAR(10), room CHAR(10), days INT, START DATETIME, END DATETIME, PRIMARY KEY (NAME,class) ) #补充timestamp知识 ;CREATE TABLE php0408.tm( c1 TIMESTAMP DEFAULT ‘2015-05-06 10:00:00‘, c2 TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ;INSERT INTO php0408.`tm` (c2) VALUES (NULL) ;SELECT * FROM tm ;UPDATE tm SET c3 =1 WHERE c3 IS NULL # 插入时出现主键约束 ;INSERT INTO student VALUES(1,‘小花‘,2,2) ;SELECT * FROM student #当插入的数据出现主键冲突时,设定该记录的数据 ;INSERT INTO student VALUES(1,‘小花‘,2,2) ON DUPLICATE KEY UPDATE sname=‘小王‘ ;CREATE TABLE stu ( sname VARCHAR(18), cid INT UNSIGNED ) ;SELECT * FROM stu #将其它表中的数据插入到当前表 ;INSERT INTO stu SELECT sname,cid FROM student WHERE sid<2 ;SELECT 1 ;SELECT NOW() ;SELECT CURRENT_TIMESTAMP #给要展示的字段加一个别名 ;SELECT sname AS `姓名` FROM php0408.student #as也可以省略 ;SELECT sname `姓名` FROM php0408.student ;SELECT 1+1 AS rst ;SELECT RAND() ;SELECT * FROM student ;SELECT * FROM class ;SELECT * FROM student,class,tm #由于两张表中都有sname和cid字段,所以 #为了确定sname来源于student表cid来源于stu表,需要使用表名.字段名的形式 ;SELECT student.sname,stu.`cid` FROM student,stu #给表起别名 ;SELECT t1.sex,t1.sname,t2.`cid` FROM student AS t1,stu AS t2 #要求从两张表中取出各自的sname,为了在php中避免键名一样,数组元素覆盖,要给字段起别名 ;SELECT t1.sname AS sname1,t2.sname AS sname2 FROM student AS t1,stu AS t2 #有时候为了使sql语句看起来完整,会使用表作为数据来源,但是有些数据无需表的参与,就是用dual虚拟表 ;SELECT 1,NOW() FROM DUAL #where 从 from 所指定的数据来源中,对每条记录进行一次判断,利用条件表达式! ;SELECT * FROM student WHERE FALSE ;SELECT * FROM class WHERE id <>2 #带有php的 ;SELECT * FROM class WHERE cname LIKE ‘%php%‘ #带有11的 ;SELECT * FROM class WHERE cname LIKE ‘%11%‘ #查询所有以java开头的 ;SELECT * FROM class WHERE cname LIKE ‘java%‘ #列出id是1 或 2 或 3的记录 ;SELECT * FROM class WHERE id IN (1,2,3) #列出id不是1 2 3的记录 ;SELECT * FROM class WHERE id NOT IN (1,2,3) #列出id在2和3之间的,闭合区间 ;SELECT * FROM class WHERE id BETWEEN 2 AND 3 # 列出时间在2015/04/30 10:00:00到2015-5-8 0:0:0之间的记录 ;SELECT * FROM tm WHERE c1 BETWEEN ‘2015/04/30 10:00:00‘ AND ‘2015-5-8‘ ;SELECT NULL-0,NULL+0,NULL,NULL*0,NULL+1,NULL=1,NULL=NULL ;CREATE TABLE php0408.nul( c1 INT NULL, c2 CHAR(8) NULL ) ;INSERT INTO php0408.`nul` VALUES(1,NULL),(2,‘2‘),(3,3) #由于null和任何数据进行运算都是null,转换为布尔值结果是false #所以为了列出字段为null的记录,必须使用is null ;SELECT * FROM nul WHERE c2 IS NULL #以下两条是相同的 ;SELECT * FROM nul WHERE c2 IS NOT NULL ;SELECT * FROM nul WHERE NOT c2 IS NULL #取出所有的c1为1并且c2为2的记录 ;SELECT * FROM nul WHERE c1=1 AND c2=2 #取出所有的c1为1或者c2为2的记录 ;SELECT * FROM nul WHERE c1=1 OR c2=2 #运算符优先级,可以使用()提升优先级 ;SELECT * FROM nul WHERE c1=1 AND (c2=2 OR c1=3) #使用group by进行分组 ;SELECT *,COUNT(*) FROM stu GROUP BY cid #如果count里面是一个具体的字段,将会过滤该字段为null的记录 ;SELECT *,COUNT(c2) FROM nul GROUP BY c1 #将每一个组里面的cid字段求和展示出来 ;SELECT *,COUNT(cid),SUM(cid) FROM stu GROUP BY cid #取出某个字段最大值 ;SELECT *,MAX(sid) FROM student GROUP BY cid #取出某个字段最小值 ;SELECT *,MIN(sid) FROM student ;SELECT *,MAX(sid) FROM student ;SELECT *,AVG(sid) FROM student GROUP BY cid #将分组后每个记录的姓名 学号展示出来 ;SELECT *,GROUP_CONCAT(sname,‘-‘,sid) FROM student GROUP BY cid #可以在分组后使用排序,asc和desc(降序) ;SELECT cid,COUNT(*) FROM student GROUP BY cid DESC #根据两个字段进行分组,并且对结果集先按照cid降序排,接着如果cid相同,按照性别升序排 ;SELECT cid,COUNT(*),sex FROM student GROUP BY cid DESC,sex ASC ;SELECT cid,COUNT(*) FROM student GROUP BY cid WITH ROLLUP ;SELECT cid,COUNT(*),sex FROM student GROUP BY cid DESC,sex ASC WITH ROLLUP #求出班级人数超过2个的班级 ;SELECT cid,COUNT(*) AS num FROM student GROUP BY cid HAVING num>2 #排序,先按照班级号升序,再按照学号降序 ;SELECT * FROM student ORDER BY cid ASC,sid DESC #随机排 ;SELECT * FROM student ORDER BY RAND() #limit用于从结果集中取得几条数据,limit受前面子句的影响 ;SELECT * FROM student WHERE sid<4 LIMIT 5,2
时间: 2024-10-29 00:35:02