create database mydb;
drop database mydb;
创建一张学生表,字段包含sid(学生编号) int类型,sname(学生姓名) 字符串类型,长度最大为20。
添加字段card(身份证号),varchar类型,长度为18。
# 1
CREATE TABLE student(
sid int,
sname varchar(20)
);
# 2
alter table student add card varchar(18);
创建数据库 OASystem
创建表UserInfo
向表中添加一个新列 CardID varchar(18)
删除表中Password列
删除表
列名 | 数据类型(精度范围) | 空/非空 | 约束条件 | 其他说明 |
---|---|---|---|---|
UserId | int | 非空 | 主键 | 用户唯一标识 |
UserName | varchar(50) | 非空 | 真实姓名 | |
PassWord | varchar(50) | 非空 | 密码 | |
DepartId | int | 非空 | 所在部门 | |
Gender | int | 非空 | 性别 |
# 1
create database OASystem;
use OASystem;
# 2
create table UserInfo(
# 用户唯一标识
UserID int not null primary key,
# 真实姓名
UserName varchar(50) not null,
# 密码
Password varchar(50) not null,
# 所有部门
DepartId int not null,
# 性别
Gender int not null
);
# 3
alter table UserInfo add CardID varchar(18);
# 4
alter table UserInfo drop Password;
# 5
drop table UserInfo;
创建student表,要求如下
字段 | 类型 | 长度 | 备注 |
---|---|---|---|
sid | int | 主键,自增 | |
sname | varchar | 20 | 不为空 |
age | int |
向该表插入一条数据:
编号:1 姓名:张三 年龄:20
再次向该表插入数据:
编号:2 姓名:李四
批量向该表插入三条数据:
编号 | 姓名 | 年龄 |
---|---|---|
3 | 王五 | 43 |
4 | 赵六 | 34 |
5 | 郑七 | 30 |
修改张三的年龄为23
修改编号为2的学生年龄为25。
删除id为5的用户的信息。
# 1
CREATE TABLE student(
sid int PRIMARY KEY auto_increment,
sname VARCHAR(20) not null,
age int
);
# 2
INSERT INTO student(sid,sname,age) VALUES(1,'张三',20);
# 3
INSERT INTO student(sid,sname) VALUES(2,'李四');
# 4
INSERT INTO student(sid,sname,age) VALUES(3,'王五',43),(4,'赵六',34),(5,'郑七',30);
# 5
UPDATE student set age = 23 WHERE sname = '张三';
# 6
UPDATE student set age = 25 WHERE sid = 5;
# 7
DELETE FROM student WHERE sid = 5;
CREATE TABLE news(
newsID int PRIMARY KEY auto_increment,
newsTitle VARCHAR(50),
editer VARCHAR(50),
upDateTime DATE,
content VARCHAR(500)
);
# 1
INSERT INTO news(newsID,newsTitle,editer,upDateTime,content)VALUES(1,'最帅的人','李玲','2018-5-8','java'),(2,'第一强','刘强','2019-8-1','体育');
# 2
UPDATE news set content = '体育爱好者' WHERE content = '体育';
# 3
delete from news2 where year(upDateTime) != year(now());
# 4
SELECT * FROM news GROUP BY upDateTime desc;
# 5
SELECT * FROM news WHERE editer = '李玲' and INSTR(content,'java');
# 6
SELECT * FROM news WHERE INSTR(newsTitle,'最') or INSTR(newsTitle,'第一');
# 7
drop table news;
某公司的产品销售数据库(ProductSales)有产品表(Product)和销售情况表(Sales),产品表为主表,销售情况表为子表,表结构和存储的数据如下:
编写SQL语句实现以下的要求:
查询出所有商品的总销售金额;
查询出单笔销售数量大于15的客户名称、购买数量、销售价格信息;
查询不同客户的客户名和销售总金额;
查询购买过商品“IBM 350笔记本”的商品名称、客户名称、购买数量;
把客户“清华同方”名称更新为“TSINGHUA TONGFANG”。
主表:Product
SalesID | ProductID | ClientName | ProductNumber | SalesPrice |
---|---|---|---|---|
1 | 2 | 清华同方 | 10 | 4500 |
2 | 1 | 清华同方 | 25 | 1800 |
3 | 3 | 联想集团 | 10 | 11000 |
4 | 2 | 联想集团 | 30 | 4500 |
5 | 1 | 联想集团 | 20 | 1800 |
6 | 3 | 北大方正 | 40 | 10000 |
7 | 3 | 诺基亚 | 20 | 10500 |
子表:Sales
ProductID | ProductName | Price |
---|---|---|
1 | HP1200打印机 | 2000 |
2 | LX360兼容机 | 4800 |
3 | IBM 350笔记本 | 11000 |
4 | BM 360笔记本 | 12000 |
# 1.查询出所有商品的总销售金额;
select sum(SalesPrice) from sales;
# 2.查询出单笔销售数量大于15的客户名称、购买数量、销售价格信息;
select ClientName,ProductNumber,SalesPrice from sales where ProductNumber > 15;
# 3.查询不同客户的客户名和销售总金额;
select ClientName,sum(SalesPrice) from sales GROUP BY ClientName;
# 4.查询购买过商品“IBM 350笔记本”的商品名称、客户名称、购买数量;
select ProductName,ClientName,ProductNumber from product,sales where sales.ProductID = product.ProductID and product.ProductID =3;
# 5.把客户“清华同方”名称更新为“TSINGHUA TONGFANG”。
update sales set ClientName = 'TSINGHUA TONGFANG' where ClientName = '清华同方';
将评论时间是当前月的评论信息,评论人追加上教授两个字
修改新闻id为1,3,5的评论,将评论时间改为今天
查询编著新闻最多的两位编者,及其编著数量
查询每天的新闻数量
按评论时间降序显示1号新闻的评论信息
获取评论数最多的评论人
获取每条新闻的新闻标题,创建时间和评价数
获取没有评论的新闻信息
获取所有的评论人(去除重复列)
删除没有评论的新闻
News表
中文字段名 | 英文字段名 | 数据类型 | 备注 |
---|---|---|---|
新闻id | newsID | int | 主键 |
新闻标题 | newsTitle | varchar | N |
新闻编者 | editer | varchar | N |
创建时间 | createTime | Date | N |
新闻内容 | content | varchar | N |
Message表
中文字段名 | 英文字段名 | 数据类型 | 备注 |
---|---|---|---|
评论编号 | Msgid | Int | 主键 |
评论时间 | MsgTime | Datetime | |
评论人 | MsgWriter | Varchar | |
新闻 id | NewsId | Int | 外键 |
# News表
create table news
(
newsid int primary key,
newstitle varchar(20) not null,
editer varchar(20) not null,
createtime datetime,
CONTENTS varchar(500)
)
insert into news values(1,'庆祝建军90周年','张兰','2017-8-1','庆祝建军90周年');
insert into news values(2,'25省明确社会抚养费标准','蒋欣','2017-8-2','25省明确社会抚养费标准');
insert into news values(3,'以房养老被骗1','高晓江','2017-7-20','以房养老被骗');
insert into news values(7,'以房养老被骗2','高晓江','2017-7-20','以房养老被骗');
insert into news values(4,'留学回国人数超250万1','雷建江','2017-7-3','留学回国人数超250万');
insert into news values(5,'留学回国人数超250万2','雷建江','2017-7-3','留学回国人数超250万');
insert into news values(6,'留学回国人数超250万3','雷建江','2017-7-3','留学回国人数超250万');
# Message表
create table message
(
msgid int auto_increment primary key,
msgtime datetime,
msgwriter varchar(20),
newsid int
)
insert into message values(null,'2017-8-1','张三',1);
insert into message values(null,'2017-8-2','张三',2);
insert into message values(null,'2017-7-21','张三',3);
insert into message values(null,'2017-7-4','张三',4);
insert into message values(null,'2017-8-1','李四',1);
insert into message values(null,'2017-8-2','李四',2);
insert into message values(null,'2017-7-21','王五',3);
insert into message values(null,'2017-7-4','赵六',4);
insert into message values(null,'2017-8-3','赵六',1);
insert into message values(null,'2017-8-3','赵六',3);
select * from message;
# 1.将评论时间是当前月的评论信息,评论人追加上教授两个字
update message set msgwriter = concat(msgwriter,'教授') where month(msgtime) = month(now());
# 2.修改新闻id为1,3,5的评论,将评论时间改为今天
update message set msgtime = sysdate() where newsid = 1 or newsid = 3 or newsid = 5;
# 3.查询编著新闻最多的两位编者,及其编著数量
select editer,count(newsid) from news GROUP BY editer ORDER BY COUNT(newsid) desc limit 2;
# 4.查询每天的新闻数量
select createtime,count(newsid) FROM news GROUP BY createtime;
# 5.按评论时间降序显示1号新闻的评论信息
select * from message where newsid = 1 ORDER BY msgtime desc;
# 6.获取评论数最多的评论人
select msgwriter from message GROUP BY msgwriter ORDER BY count(msgwriter) desc limit 1;
# 7.获取每条新闻的新闻标题,创建时间和评价数
select newstitle,createtime,n.counts from news inner join (select news.newsid,count(msgid) counts from news,message where news.newsid = message.newsid GROUP BY news.newsid) n on news.newsid = n.newsid;
# 8.获取没有评论的新闻信息
select CONTENTS from news where not EXISTS(select newsid from message where news.newsid = message.newsid);
# 9.获取所有的评论人(去除重复列)
select DISTINCT msgwriter from message;
#10.删除没有评论的新闻
delete from news where newsid not in (select newsid from message);
# 1
select dept.dname,count(emp.ename) from emp,dept where emp.deptno = dept.deptno GROUP BY dept.dname having count(emp.ename) >3;
# 2
select * FROM emp where sal > (select sal FROM emp where ename = 'jones');
# 3
select e1.ename,e2.ename from emp e1,emp e2 WHERE e1.mgr = e2.empno;
# 4
select emp.*,dept.dname from emp ,dept where sal >(select avg(sal) from emp where deptno = 30) and emp.deptno != 30;
# 5
select deptno,dname,loc from dept where deptno =(select deptno from emp group by deptno order by sum(sal) limit 1);
# 6
select ename,dname from emp,dept where dept.deptno=(select deptno from emp where empno = 7369) and empno = 7369;
# 7*
select * from emp e1 where sal >
(select avg(sal) from emp e2 where e2.deptno = e1.deptno);
# 8
select * from emp where sal >(select avg(sal) from emp where deptno = 30) and deptno != 30;
# 9
delimiter//;
create procedure sel_ename(deno int )
begin
select ename from emp where deptno = deno;
end //;
call sel_ename(10) ;
# 10
delimiter //;
create function func_dname(eno int)
returns varchar(20)
DETERMINISTIC
begin
DECLARE dn varchar(20);
select dname into dn from dept where deptno =
(select deptno from emp where empno = eno);
return dn;
end;
select func_dname(7788);
# 1
SELECT * FROM emp;
# 2
SELECT empno,ename FROM emp;
# 3
SELECT * FROM emp WHERE TIMESTAMPDIFF(YEAR,hiredate,NOW()) >= 35;
# 4
SELECT * FROM emp WHERE sal BETWEEN 2000 and 3000;
# 5
UPDATE emp set comm = comm+100 WHERE comm <= 100;
# 6
SELECT DISTINCT job FROM emp ;
# 7
SELECT * FROM emp WHERE ename LIKE '%s%';
# 8
SELECT * FROM emp GROUP BY sal LIMIT 3;
# 9
SELECT AVG(sal) FROM emp WHERE deptno = 30 ;
# 10
SELECT deptno,AVG(sal) FROM emp GROUP BY deptno HAVING AVG(sal)>=2000;
# 11
SELECT empno,AVG(sal) FROM emp GROUP BY deptno HAVING AVG(sal)>1000;
# 12
SELECT * FROM emp WHERE TIMESTAMPDIFF(YEAR,hiredate,NOW()) > 35;
# 13
SELECT * FROM emp WHERE MONTH(DATE_ADD(hiredate,INTERVAL 2 day))-MONTH(hiredate)=1;
# 1查询薪水比"smith"高的员工信息
select * from emp where sal >(select sal from emp where ename = 'Smith');
# 2查询"ACCOUNT"部门下所有员工及其薪水等级
select emp.*,grade from emp,dept,salgrade where emp.deptno = dept.deptno and dname = 'ACCOUNTING' and sal BETWEEN losal and hisal;
# 3查询入职时间早于上级领导的所有员工的信息
select a.* from emp a where hiredate < (select hiredate from emp b where a.mgr = b.empno);
# 4查询薪水高于30号部门并且不在30号部门的所有员工信息及薪水
select * from emp where sal >(select avg(sal) from emp where deptno = 30 ) and deptno != 30;
# 5查询平均工资最低的部门信息(只统计领导信息)
select dname from dept where deptno = (select deptno from emp where empno in (select mgr from emp) group by deptno order by avg(sal) limit 0,1);
# 6查询不是领导的员工信息
select * from emp where ename not in(select e2.ename from emp e1,emp e2 where e1.mgr = e2.empno);
# 7查询薪水最高的员工信息(并列时都展示)
select * from emp where sal = (select max(sal) from emp);
# 8查询最后入职的5名员工的信息
select * from emp ORDER BY hiredate desc limit 5;
# 9查询薪水大于所在部门平均工资的员工信息
select * from emp e1 where sal >(select avg(sal) from emp e2 where e2.deptno = e1.deptno);
# 10查询"SALES"部门的平均薪水等级
select DISTINCT grade from salgrade where (select avg(sal) from emp,dept where dname = 'SALES'and emp.deptno = dept.deptno) BETWEEN losal and hisal;
create view view_emp as select * from emp inner join dept using(deptno) with check option;
create index num on emp(empno);
delimiter //;
create trigger tri_emp
before delete
on emp for each row
begin
insert into emp_bak values(old.empno,old.ename,old.job,old.mgr,old.hiredate,old.sal,old.comm,old.deptno);
end //;
drop trigger tri_emp;
delete from emp where empno = 7499;
sql:dept、emp、product、sales
链接:https://pan.baidu.com/s/1AjFSOg5bdnoKI71OAKusVA
提取码:y86a