第一次培训mysql,还是比较满意的,自己整理了下,还是算是比较用心的。
后期可以专门总结下。
明白了个道理,你会的不一定能说出来。
创建数据表employee和dept。
CREATE TABLE dept
(
d_no INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
d_name VARCHAR(50),
d_location VARCHAR(100)
);
由于employee表dept_no依赖于父表dept的主键d_no,因此需要先创建dept表,然后创建employee表。
CREATE TABLE employee
(
e_no INT NOT NULL PRIMARY KEY,
e_name VARCHAR(100) NOT NULL,
e_gender CHAR(2) NOT NULL,
dept_no INT NOT NULL,
e_job VARCHAR(100) NOT NULL,
e_salary SMALLINT NOT NULL,
hireDate DATE,
CONSTRAINT dno_fk FOREIGN KEY(dept_no)
REFERENCES dept(d_no)
);
将指定记录分别插入两个表中。
向dept表中插入数据,SQL语句如下:
INSERT INTO dept
VALUES (10, 'ACCOUNTING', 'ShangHai'),
(20, 'RESEARCH ', 'BeiJing '),
(30, 'SALES ', 'ShenZhen '),
(40, 'OPERATIONS ', 'FuJian ');
向employee表中插入数据,SQL语句如下:
INSERT INTO employee
VALUES (1001, 'SMITH', 'm',20, 'CLERK',800,'2005-11-12'),
(1002, 'ALLEN', 'f',30, 'SALESMAN', 1600,'2003-05-12'),
(1003, 'WARD', 'f',30, 'SALESMAN', 1250,'2003-05-12'),
(1004, 'JONES', 'm',20, 'MANAGER', 2975,'1998-05-18'),
(1005, 'MARTIN', 'm',30, 'SALESMAN', 1250,'2001-06-12'),
(1006, 'BLAKE', 'f',30, 'MANAGER', 2850,'1997-02-15'),
(1007, 'CLARK', 'm',10, 'MANAGER', 2450,'2002-09-12'),
(1008, 'SCOTT', 'm',20, 'ANALYST', 3000,'2003-05-12'),
(1009, 'KING', 'f',10, 'PRESIDENT', 5000,'1995-01-01'),
(1010, 'TURNER', 'f',30, 'SALESMAN', 1500,'1997-10-12'),
(1011, 'ADAMS', 'm',20, 'CLERK', 1100,'1999-10-05'),
(1012, 'JAMES', 'm',30, 'CLERK', 950,'2008-06-15');
1.在employee表中,查询所有记录的e_no、e_name和e_salary字段值。
select e_no,e_name,e_salary from employee;
2.在e_no等于10和mployee表中,查询dept20的所有记录。sinqur de sarbatoir
select * from employee where dept_no =10 or dept_no = 20;
3.在employee表中,查询工资范围在800~2500之间的员工信息。
select * from employee where e_salary >800 and e_salary <= 2500;
4.在employee表中,查询部门编号为20的部门中的员工信息。
select * from employee where dept_no=20;
5.在employee表中,查询每个部门最高工资的员工信息。
select dept_no, max(e_salary) from employee group by dept_no;
6.查询员工BLAKE所在部门和部门所在地。
select d_no, d_location from dept where d_no=
(select dept_no from employee where e_name='BLAKE');
7.使用连接查询,查询所有员工的部门和部门信息。
select e_no, e_name, dept_no,d_name,d_location
from employee, dept where dept.d_no=employee.dept_no;
8.在employee表中,计算每个部门各有多少名员工。
select dept_no, count(*) from employee group by dept_no;
9.在employee表中,计算不同类型职工的总工资数。
select e_job, sum(e_salary) from employee group by e_job;
10.在employee表中,计算不同部门的平均工资。
select dept_no, avg(e_salary) from employee group by dept_no;
11.在employee表中,查询工资低于1500的员工信息。
select * from employee where e_salary < 1500;
12.在employee表中,将查询记录先按部门编号由高到低排列,再按员工工资由高到低排列。
select e_name,dept_no, e_salary
from employee order by dept_no desc, e_salary desc;
13.在employee表中,查询员工姓名以字母’A’或’S’开头的员工的信息。
select * from employee wherew e_name regexp '^[as]';
自连接sql语句片段
CREATE TABLE `zilianjie` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`lid` smallint(6) DEFAULT NULL,
`sex` char(2) DEFAULT NULL,
`salary` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
insert into `zilianjie`(`id`,`name`,`lid`,`sex`,`salary`) values (1,'pengjinfu',0,'女',10000),(2,'zhaofeng',1,'女',8000),(3,'chenhaiyang',2,'男',3000),(4,'pangwei',2,'男',7000),(5,'mahefei',0,'男',8000),(6,'zhaoxu',5,'男',7000),(7,'zhangxigong',0,'男',9000),(8,'lixin',7,'男',5000),(9,'tianjiacheng',7,'男',4000);
SELECT a1.name 姓名,IF(a2.name IS NOT NULL,a2.name,'最高领导') 领导 FROM zilianjie a1
LEFT JOIN zilianjie a2
ON a1.lid = a2.id
CREATE TABLE `a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sid` int(11) NOT NULL,
`type` char(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `a` (`id`, `sid`, `type`) VALUES (1, 1, 'a');INSERT INTO `a` (`id`, `sid`, `type`) VALUES (2, 1, 'b');INSERT INTO `a` (`id`, `sid`, `type`) VALUES (3, 2, 'c');INSERT INTO `a` (`id`, `sid`, `type`) VALUES (4, 3, 'd');CREATE TABLE `b` (
`sid` int(11) NOT NULL,
`remark` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;INSERT INTO `b` (`sid`, `remark`) VALUES (1, 'A');INSERT INTO `b` (`sid`, `remark`) VALUES (2, 'B');INSERT INTO `b` (`sid`, `remark`) VALUES (3, 'C');INSERT INTO `b` (`sid`, `remark`) VALUES (4, 'D');