sqlyog如何增删改查
程序员文章站
2023-12-29 15:12:52
-- 创建数据库employeeCREATE DATABASE employee ;SHOW DATABASES ;USE employee ;-- 创建dept表CREATE TABLE dept(deptno INT(10) PRIMARY KEY,dname VARCHAR(20),loc VARCHAR (20))-- 查询dept表SELECT * FROM dept;-- 创建emp表CREATE TABLE `emp` ( `empno` INT(10) P...
下面是一道完整的 sqlyog 增删改查的练习, 顺着做下去,可以迅速掌握。
1. 创建部门表dept,并插入数据:
2. 创建emp员工表,并插入数据:
sql 代码如下:(先自己写,有问题再看下面的答案)
-- 创建数据库employee
CREATE DATABASE employee ;
SHOW DATABASES ;
USE employee ;
-- 创建dept表
CREATE TABLE dept(
deptno INT(10) PRIMARY KEY,
dname VARCHAR(20),
loc VARCHAR (20)
)
-- 查询dept表
SELECT * FROM dept;
-- 创建emp表
CREATE TABLE `emp` (
`empno` INT(10) PRIMARY KEY,
`ename` VARCHAR(20) DEFAULT NULL,
`job` VARCHAR(20) DEFAULT NULL,
`mgr` INT(10) DEFAULT NULL,
`hiredate` VARCHAR(20) DEFAULT NULL,
`sal` DOUBLE(6,2) DEFAULT NULL,
`comm` DOUBLE(6,2) DEFAULT NULL,
`deptno` INT(10) REFERENCES dept (deptno)
) ENGINE=INNODB DEFAULT CHARSET=utf8
-- 删除表
DROP TABLE emp;
-- 插入数据dept
INSERT INTO dept VALUES (1,"技术部","北京西单11号楼1101") ;
INSERT INTO dept VALUES (2,"销售部","北京西单11号楼1101") ;
INSERT INTO dept VALUES (3,"管理部","北京西单11号楼1101") ;
-- 插入数据 emp
INSERT INTO emp VALUES (1001,"张超","架构师",1003,"2020-12-03",3000,3000,1);
INSERT INTO emp VALUES (1002,"谭博","销售",1003,"2020-11-03",4000,4000,2);
INSERT INTO emp VALUES (1003,"石闯","总裁",1003,"2020-02-03",5000,5000,3);
INSERT INTO emp VALUES (1004,"张三","保洁",1003,"2020-02-03",1000,0,2);
INSERT INTO emp VALUES (1005,"李四","前台",1003,"2020-02-03",2000,1000,2);
-- 验证表是否插入了数据
SELECT * FROM emp;
-- 1. 在emp表中查询出所有记录的姓名、部门编号、薪水,并且列名要显示为中文。
SELECT * FROM emp;
-- 2. 在emp表中查询出薪水大于1500的记录,并且按照薪水的降序排列.
SELECT * FROM emp WHERE sal>1500;
-- 3.在emp表中查询出comm字段为3000的记录。
SELECT * FROM emp WHERE comm=3000;
-- 4. emp 表里查询有几个部门的记录(distinct去重)
SELECT DISTINCT deptno FROM emp;
-- 5.在emp中查询部门编号为1/2的记录(in关键字)
SELECT * FROM emp WHERE deptno IN(1,2);
-- 6. emp表查询姓张的记录
SELECT * FROM emp WHERE ename LIKE "张%";
-- 7. 显示所员工姓名
SELECT ename FROM emp GROUP BY ename;
-- 8. 查询emp*有多少条记录?
SELECT COUNT(*) FROM emp;
-- 9. 查询emp表中出每个部门的部门薪水之和、平均薪水(薪水=薪金+佣金)
SELECT SUM(sal) AS "薪金和", SUM(comm) AS "佣金和", SUM(sal)+SUM(comm) AS "总薪水" , (SUM(sal)+SUM(comm))/COUNT(*) AS "平均薪资" FROM emp;
-- 10.更改emp表里张超的职业为产品经理
UPDATE emp SET job="产品经理" WHERE empno = 1;
- 如何求薪金和 ,平均薪资?
本文地址:https://blog.csdn.net/SoULikeMe/article/details/110557960