练习1:
/*部门表:部门编号,部门名,部门地址*/
create TABLE DEPT(
DEPT_number int primary key,
DEPT_name varchar(18),
DEPT_address varchar(15)
);
INSERT INTO DEPT VALUES (10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES (30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES (40,'OPERATIONS','BOSTON');
#表已经运行建好后,对表的任何操作只能在表外操作。alter table DEPT modify column DEPT_id int primary key;
#-------------------------------------------------------------------
/*员工表:员工编号,员工姓名,员工工作,员工直属领导编号,入职时间,工资,奖金,部门ID,对应部门表的外键*/
CREATE TABLE EMP (
EMP_ID int,
EMP_name VARCHAR(10),
EMP_work VARCHAR(10),
emp_leader SMALLINT,
emp_date date,
emp_salary float(6,2),
emp_reward int,
DEPT_number int,
FOREIGN key(DEPT_number) REFERENCES DEPT (DEPT_number)#注意单词,没弄清楚外键
);
INSERT INTO EMP VALUES(7369,'SMITH','CLERK',7902,"1980-12-17",800,NULL,20);
INSERT INTO EMP VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);
INSERT INTO EMP VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);
INSERT INTO EMP VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);
INSERT INTO EMP VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);
INSERT INTO EMP VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);
INSERT INTO EMP VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);
INSERT INTO EMP VALUES(7788,'SCOTT','ANALYST',7566,'1987-07-03',3000,NULL,20);
INSERT INTO EMP VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO EMP VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);
INSERT INTO EMP VALUES(7876,'ADAMS','CLERK',7788,'1987-07-13',1100,NULL,20);
INSERT INTO EMP VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);
INSERT INTO EMP VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);
INSERT INTO EMP VALUES(7934,'MILLER','CLERK',7782,'1981-01-23',1300,NULL,10);
#--------------------------------------------------------------
/* 工资等级表:等级,最低工资,最高工资*/
CREATE TABLE SALGRADE(
SAL_LEVE CHAR(2),
sal_low float(6.2),
SAL_hig float(7.2)
);
alter table salgrade modify column SAL_LEVE SMALLINT;
INSERT INTO SALGRADE VALUES (1,700,1200);
INSERT INTO SALGRADE VALUES (2,1201,1400);
INSERT INTO SALGRADE VALUES (3,1401,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);
#------------------查询练习----------------
1、查找部门30中员工的详细信息。
SELECT*from dept where dept_number=30;
2、找出从事clerk工作的员工的编号、姓名、部门号。
select EMP_ID,EMP_name,DEPT_number from EMP where emp_work='clerk';
3、检索出奖金多于基本工资的员工信息。
select*from emp where emp_salary<emp_reward;
4、检索出奖金多于基本工资60%的员工信息。
select*from emp where emp_reward>(emp_salary*0.6);
5、找出10部门的经理、20部门的职员 的员工信息。
select*from emp where (DEPT_number=10 and emp_work='MANAGER')or(DEPT_number=20 and emp_work='clerk');
6、找出10部门的经理、20部门的职员 或者既不是经理也不是职员但是工资高于2000元的员工信息。
select*from emp where (DEPT_number=10 and emp_work='MANAGER')or(DEPT_number=20 and emp_work='clerk') or (emp_work!='manager' and emp_work!='clerk');
7、找出获得奖金的员工的工作。
select EMP_name,EMP_work from emp where emp_reward!=0;
8、找出奖金少于100或者没有获得奖金的员工的信息。
select*from emp where emp_reward<100 or emp_reward=0;
9、找出姓名以A、B、S开始的员工信息。
select*from emp where EMP_name LIKE'A%' OR 'B%' OR 'C%';
10、找到名字长度为6个字符的员工信息。
SELECT*FROM EMP WHERE CHAR_LENGTH(EMP_name)=6;
11、名字中不包含R字符的员工信息。
SELECT*from emp where emp_name not like'%R%';
12、返回员工的详细信息并按姓名排序。
select*from empt ORDER BY emp_name; #//where后面判断的是条件即布尔值,此处的排序是一个方法!!
13、返回员工的信息并按工作降序工资升序排列。
select*from emp ORDER BY emp_reward ASC;
14、计算员工的日薪(按30天)。
select emp_salary/30 from emp;
#//归纳总结此用法//
15、找出姓名中包含A的员工信息。
select*from emp where EMP_work='clerk' and emp_name LIKE'%A%';
练习2:
一.商品销售记录表
id 商品编号 销售日期 销售数量 商品单价 销售总金额 销售员工
1 xsl001 2013/12/2 124 134.5 16678 张三
2 xsl002 2013/12/2 50 80 4000 李四
3 xsl003 2013/12/5 66 55 3630 张三
4 xsl001 2013/11/20 10 134.5 1345 张三
5 xsl001 2013/11/2 20 134.5 2690 王五
6 xsl002 2013/11/5 30 80 2400 张三
7 xsl002 2013/11/9 23 80 1840 王五
8 xsl003 2013/12/11 10 55 550 李四
9 xsl003 2013/12/12 50 55 2750 王五
10 xsl004 2013/11/30 45 100 4500 张三
(1)创建商品销售记录表
create table shop_xs(
xs_id int primary key auto_increment,
xs_number char(6),
xs_date date,
xs_amount int,
xs_price float(4,1)
xs_total float(4,1),
xs_name varchar(4)
);
(2)插入数据
shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl001','2013-12-02',124,134.5,16678,'张三');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl002','2013-12-02',50,80,4000,'李四');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl003','2013-12-05',66,55,3630,'张三');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl001','2013-11-20',10,134.5,1345,'张三');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl001','2013-11-02',20,134.5,2690,'王五');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl002','2013-11-05',30,80,2400,'张三');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl002','2013-11-09',23,80,1840,'王五');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl003','2013-12-11',10,55,550,'李四');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl003','2013-12-12',50,55,2750,'王五');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl004','2013-11-30',45,100,4500,'张三');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl003','2013-12-12',50,55,2750,'王七');
INSERT shop_xs(xs_number,xs_date,xs_amount,xs_price,xs_total,xs_name)VALUES('xsl004','2013-11-30',45,100,4500,'张七');
完成下列sql语句
1.查询张三的所有销售记录
select * from shop_xs where xs_name="张三";
2.查询张三12月份的销售记录
select * from shop_xs where xs_name="张三" and xs_date like "%-12-%";
3.查询销售总金额大于2000 的12月份销售记录
select * from shop_xs where xs_total > 2000 and xs_date like "%-12-%";
4.查询前10条销售记录
select * from shop_xs limit 0,10;
select * from shop_xs where xs_id <=10 limit 10;
5.查询前10条销售记录中商品编号xsl001的记录
select * from shop_xs where xs_number='xs1001';
6.查询销售数量大于20 销售人员为李四的记录
select * from shop_xs where xs_amount > 20 and xs_name='李四';
7.查询前5条 销售人员为王五的记录,只显示 商品编号 销售总金额 销售人员 这些列(要求列名用中文别名显出)
SELECT DISTINCT xs_number ,xs_total ,xs_name FROM shop_xs WHERE xs_name='王五' LIMIT 5;
8.查询2013年11月20日之后 2013年12月10日之前的记录
select * from shop_xs where xs_date between '2013-12-10' and '2013-12-10';
练习3:
1) 使用前面创建的学生表:student:stuId,stuName,age,address,email,birthday,sex,phone
create table student(
stuId smallint,
stuName varchar(20),
age smallint,
address varchar(20),
email varchar(20),
birthday date,
sex char(1),
phone varchar(11)
);
insert into student values(1,"小一",18,"四川成都","[email protected]",20181001,"男",15883376931),
(2,"小二",17,"四川绵阳","[email protected]",20181002,"女",15883376932),
(3,"小三",19,"四川乐山","[email protected]",20181003,"男",15883376933),
(4,"小四",19,"四川宜宾","[email protected]",20181004,"女",15883376934),
(5,"小五",17,"四川德阳","[email protected]",20181005,"男",15883376935),
(6,"小六",18,"四川成都","[email protected]",20181006,"女",15883376936),
(7,"小七",17,"四川成都","[email protected]",20181007,"男",15883376937),
(8,"小八",21,"四川乐山","[email protected]",20181008,"女",15883376938),
(9,"小九",22,"四川广安","[email protected]",20181009,"男",15883376939),
(10,"小十",23,"四川成都","[email protected]",20181010,"女",15883376977)
;
--------------------------------------------------------------------
2) 修改学生表的结构,添加一列信息,学历
alter table student add column education varchar(4);
--------------------------------------------------------------------
3) 修改学生表的结构,删除一列信息,家庭住址
alter table student drop column address;
--------------------------------------------------------------------
4) 向学生表添加如下信息:
学号 姓名 年龄 性别 联系电话 学历 出生日期
1 A张三 22 男 123456 小学 1993-09-09
2 B李四 21 男 119 中学 1994-09-01
3 C王五 23 男 150 高中 1992-04-22
4 D赵六 18 女 120 大学 1995-01-28
5 E孙七 17 女 911 大专 1996-01-28
6 C郑八 24 男 12580 中专 1990-01-28
insert into student(stuId,stuName,age,sex,education,birthday) values
(1,'A张三',22,'男','123456','小学','1993-09-09'),
(2,'B李四',21,'男','119','中学','1994-09-01'),
(3,'C王五',23,'男','150','高中','1992-04-22'),
(4,'D赵六',18,'女','120','大学','1995-01-28'),
(5,'E孙七',17,'女','911','大专','1996-01-28'),
(6,'C郑八',24,'男','12580','中专','1990-01-28');
--------------------------------------------------------------------
5) 修改学生表的数据,将电话号码以11开头的学员的学历改为“大专”
update student set education='大专' where phone like '11%';
--------------------------------------------------------------------
6) 删除学生表的数据,姓名以C开头,性别为‘男'的记录删除
delete from student where stuName like 'C%' and sex='男';
--------------------------------------------------------------------
7) 将所有年龄小于22岁的,学历为“大专”的学生的电话删除
delete phone from student where age<22 and education='大专';
--------------------------------------------------------------------
8) 修改C开头,并且学历为高中的学生出生日期为2013-09-18
update student set birthday='2013-09-18' where stuName like 'C%' and education='高中';
--------------------------------------------------------------------
9) 备份当前修改完成的表到t_student_bak表中
create table t_student_bak like student;
insert into t_student_bak select * from student;
--------------------------------------------------------------------
10) 删除出生日期在(1990年-1992年,包括1990以及1992年)的学生信息
delete from student where birthday like '1990-%-%' or '1991-%-%' or '1992-%-%';
--------------------------------------------------------------------
11) 添加一名未知电话的同学“ccf”
insert into student(stuName) values('ccf');
--------------------------------------------------------------------
12) 修改ccf同学的出生年月为1024-08-08
update student set birthday='1024-08-08' where stuName='ccf';