»  好孩子读书    数据库测试    1.DDL语句

楼主

:

这里显示昵称

(楼顶)
主 题
32
总 帖
117
积 分
750
20211101(星期)07:23 回复 10 楼 | 引 用
11. 增加表的字段 add column alter table t1 add column Email varchar(50); //操作(alter)表(table),表名是t1,为t1增加(add)一栏表头(column),名称为Email,类型为varchar(30); *表头的名称里不可以有符号-
主 题
32
总 帖
117
积 分
750
20211101(星期)07:23 回复 11 楼 | 引 用
12. 删除表的字段 drop column alter table t1 drop column Email;
主 题
32
总 帖
117
积 分
750
20211101(星期)07:24 回复 12 楼 | 引 用
13. 修改表的字段名与字段类型 change column alter table t1 change column Email email varchar(30); //注意,虽然是修改字段名命令,但是最后的类型竟然必须带上,且可以作修改
主 题
32
总 帖
117
积 分
750
20211101(星期)07:24 回复 13 楼 | 引 用
14. 修改字段排列顺序 alter table t1 modify name varchar(20) first; //这将把处于第二列的t1移动到第一列; alter table t1 modify name varchar(20) after UID; //
主 题
32
总 帖
117
积 分
750
20211101(星期)07:24 回复 14 楼 | 引 用
15. 修改表名 alter table t1 rename to my_Table1;
主 题
32
总 帖
117
积 分
750
20211101(星期)07:24 回复 15 楼 | 引 用
insert into my_Table1(UID,name,email) values(0,'郭德纲','guodegangguo@degang.org'); //mysql默认不支持中文,需要改编码 insert into my_Table1 values(0,'郭德纲','guodegangguo@degang.org'); //第一个括号可以不要,不要就按原列顺序赋值 insert into students(UID,name,email) values(0,'guodegang','guodegang@guodegang.org'); //字符串必须加引号,单双都可以 insert into students(email,name) values('qianweiqi@163.com','qianweiqi'); //可以在表名括号中不拘泥任何顺序直接指定某些列,设置值时按顺序写即可
主 题
32
总 帖
117
积 分
750
20211101(星期)07:24 回复 16 楼 | 引 用
2. 插入多条记录 insert into students values(3,'gsg','3@4'),(4,'t5h','f@g');
主 题
32
总 帖
117
积 分
750
20211101(星期)07:25 回复 17 楼 | 引 用
3. 更新(修改)记录中的某个字段值 update students set UID=1 where name="qianweiqi"; update students set email=6@6 where UID=1 or name="gsg"; 4. 更新(修改)多表记录中的某个字段值 update students,teachers set students.name=guoqilin,teachers.name=gaofeng where students.UID=2 and teachers.UID=3; 5. 删除单表中的记录(整条记录) delete from students where name="t5h"; 6. 删除多表中的记录(整条记录) 7. 查询一个表中的全部记录 select * from employee; 8. 查询一个表中,distinct字样后所有不完全一样的记录 select distinct salary from employee; //表示仅列出工资字段的全部数据,并合并所有相同的值 9. 算术条件查询(=,<,>,>=,<=,!=) select distinct distinct * from employee where UID>2; select distinct distinct * from employee where UID=1 or UID=5;
主 题
32
总 帖
117
积 分
750
20211101(星期)07:25 回复 18 楼 | 引 用
10. 排序 select * from employee order by salary asc; //按salary升序,默认按升序asc select * from employee order by UID desc; //按UID降序 select * from employee order by salary desc,id asc; 先按salary降序,若有相同,再按id升序 11. 限制 select * from employee order by UID desc limit 3; //按UID降序后,只取前3条 select * from employee order by UID desc limit 3,2; //从第3条(从0开始算)开始取,总共取2条(这个还是2条) 12. 聚合(数学函数) sum: select sum(salary) from employee; count: select count(*) from employee; max: select max(salary) from employee; min: select min(UID) from employee; group by: select department,sum(salary) from employee group by department; //把相同的部门打包后,每个部门分别加和 with rollup: select sum(salary) from employee group by department with rollup; //分类统计以后再加和? 貌似意义不大 having: 对函数聚合后的集合进行条件过滤 select department,sum(salary) from employee group by department having sum(salary)>900; //这... 多结果输出: select sum(salary),max(UID),min(UID),count(*) from employee; 13. 表的内连接(多个表中匹配某条件字段的交集) where a.b=c.d select * from employee,employee_record where employee.UID=employee_record.UID; select employee.UID,employee.name,employee.salary,employee.department,employee_record.RID,employee_record.record from employee,employee_record where employee.UID=employee_record.UID; 13.1 给查询出来的结果集中的字段指定别名 select employee.UID a,employee_record.UID b from employee,employee_record where employee.UID=employee_record.UID; 13.2 给查询出来的结果集中的表指定别名 select e1.UID a,er.UID b from employee e1,employee_record er where e1.UID=er.UID; //一旦给from后的列名起了别名,则当前语句中列名都要用新名字 14.1 表的外连接 - 左连接(多个表中根据匹配条件得到并集, 且完整显示表集合中左边表的全部) 语法: [select 记录数据 from [表1] left join [表2] on [两个表中某列数据匹配条件] select * from employee left join employee_record on employee.UID=employee_record.UID; 14.2 表的右连接 - 右连接 (多个表中根据匹配条件得到并集, 且完整显示表集合中右边表的全部) select * from employee_record right join employee on employee.UID=employee_record.UID; 15. 子查询 in, not in, exists, not exists 15.1 存在性检测: in 解释: 在in后的查询结果集合里,找出存在UID,然后根据这些UID在employee主表里去找哪些有. 注意in后语句只能返回一个字段) select * from employee where UID in(select distinct UID from employee_late); //与下句等价 select * from employee where UID in(select UID from employee_late); //与上句等价,因为in会自动进行子查询结果唯一性设置
主 题
32
总 帖
117
积 分
750
20211101(星期)07:25 回复 19 楼 | 引 用
子查询 - 带频次统计 先把迟到次数超过2次的员工UID揪出来: select UID,count(*) from employee_late group by UID having count(*)>2; //给e_late表中,根据同一个UID出现的次数进行加和统计,并列出次数>2的记录 然后再把这个结果作为中间集,再在中间集里列出这些UID的详细信息,不过由于中间集只能有一个column,所以上述语句改为: select UID from employee_late group by UID having count(*)>2; //这样,中间集里就只有一列数据:UID了 最后,把这些UID的详细信息列出来 select * from employee where UID in(select UID from employee_late group by UID having count(*)>2); 15.2 判断记录存在与否 exists 解释: 若子语句查询到的记录存在则返回true(哪怕选出的字段值是null,只要记录存在,仍为true, 若不存在则返回false) select * from employee where exists(select * from employee_late where employee.UID=employee_late.UID); //这个叫做相关子查询,就是括号中的子句执行需要用到父句中的结果: //先从父句中取出一条记录,让这条记录的UID去匹配子句中的late.UID,若成功,就把匹配到记录按照父句中的列格式放入结果集 //并不是每个人都迟到过,所以会查询出所有迟到过至少一次的每一个UID,并把它们的基本信息列出来 16. 记录联合 union, union all select * from employee_record union select * from employee_late; //右表的字段名称会损毁,上下连成一个表,全部使用左表列名,会自动去除重复 select UID from employee union select LID from employee_late; //这样就会排除重复的1-10里的数字,总共只显示10条 select UID from employee union all select LID from employee_late; //加上all以后,就不会自动排重,有多少,全部列出来,上下叠罗汉 **. 数据库授权命令 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON db.* TO 'david'@'192.58.197.0/255.255.255.0';
array(1) { ["PHPSESSID"]=> string(64) "cc9e98f561d3f3953601e38b870282637e590fc2cf6fc1a6048e87b3a732d5f5" }