├── .gitattributes ├── LIMIT与子选择简单插入.sql ├── README.md ├── online.py ├── test_.txt ├── workbench测试.sql ├── 主键约束.sql ├── 增删约束.sql ├── 外键约束.sql ├── 子查询(1).sql ├── 子查询(2).sql ├── 子查询-分类.sql ├── 子查询-连接.sql ├── 数据更新.sql ├── 数据查询.sql ├── 数据查询2.sql ├── 权限修改(增删).sql ├── 生成视图.sql └── 练习.sql /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /LIMIT与子选择简单插入.sql: -------------------------------------------------------------------------------- 1 | 完整插入语句如下: 2 | 3 | INSERT INTO table_names 4 | SELECT [All|Distinct] [as 别名] ... 5 | From <表名> [as 别名]... | ([As] <别名> 9 | [Where + 条件表达式(包括:比较,IN,EXISTS,AND,LIKE等) 10 | [Group by <列名> [Having]] *所选列必须在Group或聚集中 11 | [Order by <列名> [ASC|DESC] 12 | ; 13 | 14 | 练习 15 | select * from student; 16 | 17 | Create table Student_2( 18 | Sno int unsigned primary key, 19 | Sname varchar(20) not null, 20 | Sex tinyint unsigned default 1, 21 | Sage smallint unsigned not null, 22 | Sdept varchar(4) not null default 'CS' 23 | ); 24 | 25 | alter table Student_2 change column Sno Student_2.Sno int 26 | unsigned; 27 | show columns from Student_2; 28 | show columns from SC; 29 | 30 | create table SC( 31 | SC.Sno int unsigned, 32 | Cno tinyint unsigned not null, 33 | Grade smallint unsigned unique 34 | ); 35 | 36 | insert into Student_2 values( 37 | '201215121','李勇','1','20',"CS"); 38 | insert into Student_2 values( 39 | '201215122','刘晨','0','19',"CS"); 40 | 41 | insert into SC values('201215121',"1","92"); 42 | insert into SC values('201215121',"2","85"); 43 | insert into SC values('201215121',"3","88"); 44 | insert into SC values('201215122',"2","90"); 45 | insert into SC values('201215122',"3","80"); 46 | 47 | delete from Student_2 where Sno="201215122"; 48 | #insert into Student_2(Sdept) values("CS"); 49 | 50 | alter table SC drop primary key; 51 | 52 | Select * from Student_2; 53 | 54 | Select Student_2.*,SC.* 55 | From Student_2,SC Where Student_2.Sno=SC.Sno; 56 | 57 | 自然连接 58 | Select Student.Sno,Sname,Sex,Sage,Sdept,Cno,Grade 59 | From Student,SC 60 | Where Student.Sno=SC.Sno; 61 | /* 62 | Drop table Student; 63 | Alter table Student_2 rename to Student; 64 | */ 65 | 查询选了2号并且成绩大于90分的 66 | Select Student.Sno,Sname 67 | From Student,SC where Student.Sno=SC.Sno And SC.Cno='2' 68 | And SC.Grade >= 90; 69 | 70 | 自身连接(为同一张表赋不同名称) 71 | Insert into Student values( 72 | '201215123','王敏','0','18',"MA"); 73 | Insert into Student values( 74 | '201215125','张立','1','19',"IS"); 75 | 76 | 外连接 77 | Select student.Sno,Sname,Sex,Sage,Sdept,Cno,Grade 78 | From Student right outer join SC on(student.Sno=SC.Sno); 79 | 80 | 嵌套查询 81 | Select Student.sno,Sname from student 82 | where Sno in 83 | (Select Sno from SC where 84 | Cno='2') 85 | order by Sno DESC; 86 | 87 | 1.不相关子查询 88 | 查询与刘晨所在系的同学(分为AB两种实现) 89 | a.嵌套查询 90 | Select Student.Sno,Sname,Sdept 91 | From Student where Sdept in 92 | (Select Sdept from Student where 93 | Sname="刘晨"); 94 | 95 | b.自身连接 96 | Select gg.Sno,gg.Sname,gg.sdept 97 | From Student gg,student mm 98 | where mm.Sname = "刘晨" and mm.Sdept = gg.sdept; 99 | 100 | 2.相关子查询 101 | 102 | 找出每个学生超过他课程平均分的课程号 103 | Select Sno,Cno From SC x 104 | where Grade >= ( 105 | Select Avg(Grade) From SC y where y.sno = x.sno); 106 | /* 107 | Select x.Sno,x.cno From SC x,Sc y 108 | where x.sno = y.sno 109 | group by cno 110 | having avg(y.grade) > 88; 111 | */ 112 | **相关子查询会自动遍历内外,不相关则一次求解 113 | 114 | 3.Any,All谓词与聚集函数,IN运算符的关系 115 | 116 | Select sname,sage from Student 117 | where sage < any #等价于IN 118 | (select max(sage) from student where sdept="CS" 119 | ) 120 | And sdept <> "CS"; 121 | 122 | Select sname,sage from student 123 | where sage < #MIN聚集函数等价于ALL 124 | (select min(sage) from student 125 | where sdept="CS") 126 | And sdept != "CS"; 127 | 128 | *注意,EXISTS返回布尔值,父查询根据真假判断是否选择 129 | 130 | 4.基于派生表的查询 131 | 132 | Select sno,cno from sc 133 | (select sno, avg(grade) from sc group by sno) as 134 | avg_sc(avg_sno,avg_grade) 135 | where sc.sno = avg_sc.avg_sno and 136 | sc.grade >= avg_sc.avg_grade; -------------------------------------------------------------------------------- /权限修改(增删).sql: -------------------------------------------------------------------------------- 1 | #测试数据库安全操作 2 | 3 | use students_id; 4 | 5 | show columns from student; 6 | SELECT * FROM students_id.student; 7 | 8 | revoke select on table 9 | student 10 | from root@localhost; 11 | 12 | 方法一 13 | SELECT DISTINCT CONCAT 14 | ('User: ''',user,'''@''',host,''';') 15 | AS query FROM mysql.user; 16 | 17 | 方法二 18 | select * from mysql.user; 19 | 20 | /查看USER表的结构: desc mysql.user; 21 | 22 | 插入: 23 | insert into mysql.user(host,user) values 24 | ("localhost","test"); 25 | 26 | #show create table; 27 | 28 | 查看用户的权限: 29 | Show grants for root@localhost; 30 | 31 | 对属性列授权: 32 | Grant update(sno), select, insert 33 | on table Student #多个属性不能同时授予多张表 34 | to root@localhost 35 | with grant option; 36 | 37 | grant all privileges 38 | on table student,sc 39 | to root@localhost,mysql.sys@localhost; #!无法多张表授权 40 | 41 | 删除权限: 42 | 43 | Revoke insert on table 44 | student from root@localhost #没有CASCADE和restrict; 45 | 46 | DELETE FROM mysql.user WHERE User="mysql.sys" and 47 | Host="localhost"; 48 | 49 | 创建用户: 50 | 51 | Create user if not exists "test2"; 52 | 53 | grant all privileges on *.* to 54 | testuser@localhost identified by "123456"; 55 | 56 | select * from mysql.user; 57 | 58 | 更改新用户权限: 59 | grant update(sno) on 60 | table student to test2; 61 | 62 | show grants for 'test2'@'%'; 63 | 64 | 给新用户设置密码: 65 | grant all privileges on *.* to test2 66 | identified by "12345" 67 | with grant option; #允许新用户将权限授予他人 68 | 69 | UPDATE mysql.user SET user= 'root' 70 | WHERE user='new_password'; 71 | #如上,权限可以用GRANT语句操作,也可以直接使用UPDATE 72 | #将USER表当做一张表来操作 73 | 74 | -------------------------------------------------------------------------------- /生成视图.sql: -------------------------------------------------------------------------------- 1 | use students_id; 2 | 3 | #创建视图 4 | 5 | CREATE VIEW MA_student 6 | AS 7 | Select sno,sname,sage 8 | from student 9 | where sdept = 'MA' 10 | with check option; 11 | 12 | Select * from IS_student; 13 | Select * from IS_S13; 14 | 15 | Drop view CS_student restrict; 16 | 17 | #创建基于多个表的视图 18 | 19 | Create view IS_S13(S1,S2,S3) 20 | AS 21 | SELECT student.sno,IS_student.sname,student.sage 22 | from student,IS_student 23 | Where sdept = 'IS' AND 24 | student.sno = IS_student.sno; 25 | #这种视图可以创建于一个基本表和另一个视图上 26 | #视图也是一种表,作为虚拟表而存在于数据字典中 27 | 28 | Select * from 29 | (Select sno, sage 30 | from sc 31 | group by sno) 32 | AS 33 | IS_student(sno,sage) 34 | where sage != 0; 35 | 36 | 对于update,有with check option, 37 | 要保证update后,数据要被视图查询出来 38 | 39 | 对于delete,有无with check option都一样 40 | 41 | 对于insert,有with check option, 42 | 要保证insert后,数据要被视图查询出来 43 | 44 | 对于没有where子句的视图,使用with check option是多余的 45 | 46 | 创建视图的完整方法: 47 | CREATE [OR REPLACE] [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] 48 | DEFINER = `root`@`localhost` 49 | SQL SECURITY DEFINER 50 | VIEW view_name [(column_list)] 51 | AS select_statement 52 | [WITH [CASCADED | LOCAL] CHECK OPTION] 53 | 54 | 55 | 括号中的OR REPLACE关键字是可选的。 56 | 如果当前数据库中已经存在指定名称的视图时,没有该关键字, 57 | 将会提示错误信息;如果使用了OR REPLACE关键字,则当前正在创建的视图会覆盖掉原来同名的视图。 58 | [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}] 59 | ALGORITHM子句表示使用何种算法来处理视图。此外,它并不属于标准SQL的一部分,而是MySQL对标准SQL进行的功能扩展。 60 | 61 | ALGORITHM可以设置三个值:MERGE、TEMPTABLE或UNDEFINED。如果没有ALGORITHM子句,则默认值为UNDEFINED(未定义的)。 62 | (对于MERGE,会将引用视图的语句的文本与视图定义合并起来,使得视图定义的某一部分取代语句的对应部分。 63 | 对于TEMPTABLE,视图的结果将被置于临时表中,然后使用它执行语句。 64 | 对于UNDEFINED,MySQL将选择所要使用的算法。如果可能,它倾向于MERGE而不是TEMPTABLE,这是因为MERGE通常更有效, 65 | ) 66 | 67 | 而且如果使用了临时表,视图是不可更新的。 68 | 之所以提供TEMPTABLE选项,是因为TEMPTABLE在创建临时表之后、并在完成语句处理之前,能够释放基表上的锁定。 69 | 与MERGE算法相比,锁定释放的速度更快,这样,使用视图的其他客户端不会被屏蔽过长时间。 70 | 此外,MERGE算法要求视图中的行和基表中的行具有一对一的关系。如果视图包含聚合函数 71 | (SUM(), MIN(), MAX(), COUNT()等)、 72 | DISTINCT、GROUP BY、HAVING、UNION或UNION ALL、 73 | 没有基表的引用文字值(例如:SELECT 'hello';) 74 | 等结构中的任何一种,将失去一对一的关系,此时必须使用临时表取而代之。 -------------------------------------------------------------------------------- /练习.sql: -------------------------------------------------------------------------------- 1 | SHOW columns FROM provinces1; 2 | select * from provinces1; 3 | ALTER table provinces1 ADD password VARCHAR(32) 4 | NOT NULL AFTER ID; 5 | alter table provinces1 ADD school varchar(10) 6 | NOT NULL FIRST; 7 | 8 | DELETE FROM provinces1 WHERE age = 21; 9 | alter table provinces1 DROP COLUMN username; 10 | 11 | INSERT INTO provinces1(school, password, Pname, age) 12 | VALUES("XISU", 23322, "Beijing", 21); 13 | 14 | TINYINT unsigned 15 | NOT NULL DEFAULT 10; --------------------------------------------------------------------------------