最近在学习数据库理论,顺便把SQL学习一下,下面列出常用的SQL命令 (全部在MySQL上测试通过)。
1. 创建表
CREATE TABLE Student (Sno CHAR(5) NOT NULL UNIQUE, Sname CHAR(20), Ssex CHAR(2), Sage INT, Sdept CHAR(15));
CREATE TABLE course(Cno CHAR(5) NOT NULL UNIQUE, Cname CHAR(30), Cpno CHAR(5), Ccredit SMALLINT);
CREATE TABLE SC(Sno char(5) NOT NULL, Cno char(5) NOT NULL, Grade SMALLINT NOT NULL);
1.5 增加外键
mysql> ALTER TABLE course ADD CONSTRAINT fk_pno
-> FOREIGN KEY (Cpno)
-> REFERENCES course(Cno);
mysql> ALTER TABLE sc ADD CONSTRAINT fk_sno
-> FOREIGN KEY(Sno)
-> REFERENCES student(Sno);
mysql> ALTER TABLE sc ADD CONSTRAINT fk_cno
-> FOREIGN KEY(Cno)
-> REFERENCES Course(Cno);
2. 改变表
ALTER TABLE Student ADD Scome DATE;
ALTER TABLE Student MODIFY Sage SMALLINT;
3. 删除表
DROP TABLE Student
4. 插入信息
INSERT INTO Student VALUES ("95001", "李勇", "男", 20, "CS");
INSERT INTO Student VALUES ("95002", "刘晨", "女", 19, "IS");
INSERT INTO Student VALUES ("95003", "王名", "女", 18, "MA");
INSERT INTO Student VALUES ("95004", "张立", "男", 19, "IS");
INSERT INTO course VALUES ("6", "数据处理", NULL, 2);
INSERT INTO courses VALUES ("2", "数学", NULL, 2);
INSERT INTO course VALUES ("7", "PASCAL语言", "6", 4);
INSERT INTO course VALUES ("4", "操作系统", "6", 3);
INSERT INTO course VALUES ("5", "数据结构", "7", 3);
INSERT INTO course VALUES ("1", "数据库", "5", 4);
INSERT INTO course VALUES ("3", "信息系统", "1", 4);
INSERT INTO sc VALUES("95001", "1", 92);
INSERT INTO sc VALUES("95001", "2", 85);
INSERT INTO sc VALUES("95001", "3", 88);
INSERT INTO sc VALUES("95002", "2", 90);
INSERT INTO sc VALUES("95002", "3", 80);
5. 建立索引
CREATE UNIQUE INDEX Stusno ON Student(Sno);
CREATE UNIQUE INDEX Coucno ON course(Cno);
CREATE UNIQUE INDEX SCno ON SC(Sno ASC, Cno DESC);
6. 删除索引
DROP INDEX Stusno on Student;
7. 查询
7.1 单表查询
7.1.1 对列
select Sno, Sname from student;
select Sno, 2009-Sage from student;
select * from student;
select Sname NAME, 'Year of Birth:' BIRTH, 2009-Sage BIRTHDAY, LOWER(Sdept) DEPARTMENT from student;
7.1.2 对行
select Sname from student where Sdept='CS';
select Sname, Sage from student where Sage<20;
select * from SC where grade>85;
select Sname, Sdept, Sage from student where Sage between 20 and 23;
select Sname, Ssex from student where Sdept not in ('CS', 'MA');
select * from student where Sname like '刘%';
select * from course where Cpno is NULL;
select Sname, Ssex from student where Sdept='IS' and Sage=19;
7.1.3 排序
select * from student Order by Sdept, Sage DESC;
7.1.4 使用函数
select COUNT(*) from student;
select COUNT(DISTINCT Sno) from SC;
select AVG(GRADE) from SC where Cno='1';
select MAX(GRADE) from SC where Cno='2';
7.1.5 对查询结果分组
select Cno, COUNT(Sno) from SC Group by Cno;
select Cno, COUNT(Sno) from SC Group by Cno having COUNT(*) <2;
7.2 连接查询
7.2.1 等值非等值查询
select student.*, SC.* from student, sc where student.Sno=sc.Sno;
7.2.2 自身连接
select first.Cno, Second.Cpno from course first, course second where first.Cpno=second.Cno and second.Cpno is not null;
7.2.3 外连接
select Student.Sno, Sname, Ssex, Sage, Sdept, Cno, Grade from Student left join sc on sc.Sno=student.Sno;
7.3 复合条件连接
select Student.Sno, Sname,Cname, Grade from student, course, sc where sc.Sno=Student.Sno AND sc.Cno=Course.Cno;