阅 读
111 回 复
1
111 回 复
1
(楼顶)
数据库SQL语句
2021年11月01日(星期一)07:27 发布
0 楼(顶楼)|
回 复
一. DDL语句
1. 登录数据库(配置环境变量,或切换目录) mysql -u xxx -p xxx
mysql -u root; //u是user, mysql自带用户root
mysql -u root -p //p是密码, mysql默认没有密码, 要求密码的时候直接回车
2. 展示当前数据库 show databases
show databases; //注意单词是复数, 必须加分号! 否则会出现换行后mysql字样消失,系统会等待你继续输入下一条语句,直到加分号!
3. 创建数据库 create database
create database d1; //创建数据库, d1是你自己起的数据库名
show databases; //创建之后再展示, 就可以看到新的库名
4. 选择数据库 use
选择一个数据库后, 可以进行库内的表操作
use d1; //可以不加分号,输入 "use d1" 也可以
5. 删除数据库 drop database
drop database d1; //必须加分号
6. 查看数据库中的数据表(的列表) show tables
show tables; //要加分号. 这个命令的前提是 use d1; 选中一个库以后,show tables才显示这个库中个数据表
7. 创建数据表 create table
create table t1(UID int,name varchar(16)); //这个语句以use d1为基础,要先选中一个数据库,才能在这个库中创建表
8. 查看数据表表头的字段和约束信息
desc t1; //注意desc后面必须接当前数据库下存在的表的名称,t1
show create table t1\G //这里不用加分号,因为\G符号自带分号效果
9. 删除表
drop table t1; //注意drop也必须在use了一个库之后才可以用;
10. 修改表头字段类型 modify column
alter table t1 modify column name varchar(20); //操作(alter)表(table),表名是t1,然后修改t1表中名为name的表头字段的类型为varchar(20);
alter table t1 modify name varchar(20); //column单词可以不写
11. 增加表的字段 add column
alter table t1 add column Email varchar(50); //操作(alter)表(table),表名是t1,为t1增加(add)一栏表头(column),名称为Email,类型为varchar(30);
*表头的名称里不可以有符号-
12. 删除表的字段 drop column
alter table t1 drop column Email;
13. 修改表的字段名与字段类型 change column
alter table t1 change column Email email varchar(30); //注意,虽然是修改字段名命令,但是最后的类型竟然必须带上,且可以作修改
14. 修改字段排列顺序
alter table t1 modify name varchar(20) first; //这将把处于第二列的t1移动到第一列;
alter table t1 modify name varchar(20) after UID; //
15. 修改表名
alter table t1 rename to my_Table1;
二. DML语句:
1. 插入单条记录
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'); //可以在表名括号中不拘泥任何顺序直接指定某些列,设置值时按顺序写即可
2. 插入多条记录
insert into students values(3,'gsg','3@4'),(4,'t5h','f@g');
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;
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会自动进行子查询结果唯一性设置
子查询 - 带频次统计
先把迟到次数超过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';
未经编辑,原装一手