数据库第二节 sql表格创建及内容插入和查询
2018-05-04 16:23 隔壁吃瓜群众 阅读(750) 评论(0) 编辑 收藏 举报CREATE TABLE employee(
empno INT,
ename VARCHAR(10),
job VARCHAR(10),
mgr INT,
hiredate DATE,
sai DOUBLE,
commn DOUBLE,
deptno INT
)
1.创建一个员工表,内容有:
1.员工编号 empno int(数字类型)
2.员工姓名 ename varchar(字段类型)
3.员工岗位 job varchar(字段类型)
4.员工上级主管编号 mgr varchar(字段类型)
5.入职时间 hiredate date(字段类型)
6.员工工资 sai DOUBLE(数字浮点类型)
7.员工奖金 commn DOUBLE(数字浮点类型)
8.员工编号 deptno INT(数字类型)
2.插入员工资料
insert into(没插入的如:commn项目显示为null)values为显示选项:顺位为员工编号、姓名、上级编号、奖金(有些为插入为null)、工资、和项目编号
insert into employee(empno,ename,job,mgr,hiredate,sai,deptno) VALUES(1001,'甘宁','文员',1013,'2000-12-17',8000.00,20);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,commn,deptno) VALUES(1002,'黛绮丝','销售员',1006,'2001-02-20',16000.00,3000,30);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,commn,deptno) VALUES(1003,'殷天正','销售员',1006,'2001-02-22',12500.00,5000,30);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,deptno) VALUES(1004,'刘备','经理',1009,'2001-04-02',29750.00,30);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,commn,deptno) VALUES(1005,'谢逊','销售员',1006,'2001-09-28',12500.00,14000,30);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,deptno) VALUES(1006,'关羽','经理',1009,'2001-05-01',28500.00,30);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,deptno) VALUES(1007,'张飞','经理',1009,'2001-09-01',24500.00,10);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,deptno) VALUES(1008,'诸葛亮','分析师',1009,'2001-05-01',28500.00,20);
INSERT INTO employee(empno,ename,job,hiredate,sai,deptno) VALUES(1009,'曾阿牛','董事长','2001-05-01',28500.00,10);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,commn,deptno) VALUES(1010,'韦一笑','销售员',1006,'2001-09-08',15000.00,0.00,30);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,deptno) VALUES(1011,'周泰','文员',1008,'2007-05-23',11000.00,20);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,deptno) VALUES(1012,'程普','文员',1006,'2001-12-03',9500.00,30);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,deptno) VALUES(1013,'庞统','分析师',1004,'2001-12-03',30000.00,20);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,deptno) VALUES(1014,'黄盖','文员',1007,'2001-01-23',13000.00,10);
INSERT INTO employee(empno,ename,job,mgr,hiredate,sai,commn,deptno) VALUES(1015,'谢逊','销售员',1001,'2013-05-01',80000.00,50000,50);
查询内容
SELECT * FROM employee WHERE deptno=30;/*查询所有编号为30*/
SELECT ename,empno,deptno FROM employee WHERE job='销售员';/*销售员姓名和编号*/
SELECT *FROM employee WHERE commn>sai;/*奖金高于工资*/
SELECT *FROM employee WHERE commn>sai*0.16;/*奖金高于工资*/
SELECT* FROM employee WHERE(deptno=10 AND job='经理')OR(deptno=20 AND job='销售员');/*编号为10和编号为20的经理和销售员*/
SELECT * FROM employee WHERE(deptno=10 AND job='经理')OR(deptno=20 AND job='销售员')OR(job<>'经理' AND job<>'销售员' AND sai>20000);/*编号为10和20的经理和销售员还有既不是经理也不是销售员的工资大于2000的人*/
SELECT * FROM employee WHERE(commn<1000)OR(commn=NULL);/*奖金低于1000或者奖金为空*/
SELECT * FROM employee WHERE ename LIKE '___';/*查询由三个人名字组成的员工(暂时无法实现)*/
SELECT * FROM employee WHERE hiredate LIKE '2000%';/*员工2000年入职的表*/
SELECT * FROM employee ORDER BY empno ASC;/*员工详细资料升序排序*/
SELECT * FROM employee ORDER BY sai DESC,hiredate ASC;/*工资降序入职年限相同升序*/
SELECT * FROM employee WHERE ename LIKE '周_';/*姓周的两个字员工的名字*/
SELECT * FROM employee WHERE ename LIKE '张%';/*姓张的所有经理*/
SELECT job,COUNT(*) FROM employee GROUP BY job;/*该部门有多少个岗位*/
SELECT job,COUNT(*) FROM employee GROUP BY job HAVING COUNT(*)>3; /*哪个部门大于3个人*/
以上16项为查询最为详细的条件以及最为全面的学习内容。