博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL 的相关语句(增删改查)(SQLyog软件实现)
阅读量:5320 次
发布时间:2019-06-14

本文共 7999 字,大约阅读时间需要 26 分钟。

-- 创建雇员表:empCREATE TABLE emp(    empno        INT,        -- 员工编号    ename        VARCHAR(50),    -- 员工姓名    job        VARCHAR(50),    -- 员工工作    mgr        INT,        -- 领导编号    hiredate    DATE,        -- 入职日期    sal        DECIMAL(7,2),    -- 月薪    comm        DECIMAL(7,2),    -- 奖金    deptno        INT        -- 部分编号) ;-- 添加数据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-04-19',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-05-23',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,'1982-01-23',1300,NULL,10);-- 创建学生信息表:stuCREATE TABLE stu(    sid    CHAR(6),    -- 学生学号    sname    VARCHAR(50),    -- 学生姓名    age    INT,        -- 学生年龄    gender    VARCHAR(50)    -- 学生性别);-- 添加数据INSERT INTO stu VALUES('S_1001', 'liuYi', 35, 'male');INSERT INTO stu VALUES('S_1002', 'chenEr', 15, 'female');INSERT INTO stu VALUES('S_1003', 'zhangSan', 95, 'male');INSERT INTO stu VALUES('S_1004', 'liSi', 65, 'female');INSERT INTO stu VALUES('S_1005', 'wangWu', 55, 'male');INSERT INTO stu VALUES('S_1006', 'zhaoLiu', 75, 'female');INSERT INTO stu VALUES('S_1007', 'sunQi', 25, 'male');INSERT INTO stu VALUES('S_1008', 'zhouBa', 45, 'female');INSERT INTO stu VALUES('S_1009', 'wuJiu', 85, 'male');INSERT INTO stu VALUES('S_1010', 'zhengShi', 50, 'female');INSERT INTO stu VALUES('S_1011', 'xxx', NULL, NULL);-- 创建部分表:deptCREATE TABLE dept(    deptno    INT,        -- 部分编码    dname    VARCHAR(14),    -- 部分名称    loc    VARCHAR(13)    -- 部分所在地点);-- 添加数据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');-- 查询所有列SELECT * FROM stu;-- 删除数据DELETE FROM stu WHERE sid = 'sunQi' ;-- 查询指定列SELECT sname,age FROM stu;-- 查询性别为女,并且年龄为50的记录SELECT * FROM stu WHERE gender='female' AND age=50;-- 查询学号为S_1001,或者姓名为liSi的记录SELECT * FROM stu WHERE sid='s_1001' OR sname='lisi';-- 查询学号为S_1001,S_1002,S_1003的记录SELECT * FROM stu WHERE sid IN ('s_1001','s_1002','s_1003' );-- 查询学号不是S_1001,S_1002,S_1003的记录SELECT * FROM stu WHERE sid NOT IN ('s_1001','s_1002','s_1003' );-- 查询年龄为null的记录SELECT * FROM stu WHERE age IS NULL;-- 查询年龄在20到40之间的学生记录SELECT * FROM stu WHERE age BETWEEN 20 AND 40; -- 或者SELECT * FROM stu WHERE age>=20 AND age<=40;-- 查询性别非男的学生记录SELECT * FROM stu WHERE gender !='male'; -- 或者SELECT * FROM stu WHERE gender <>'male';-- 查询姓名不为null的学生记录SELECT * FROM stu WHERE sname IS NOT NULL; -- 或者SELECT * FROM stu WHERE NOT sname IS NULL;-- 查询姓名由5个字母构成的学生记录SELECT * FROM stu WHERE sname LIKE '_____';-- 查询姓名由5个字母构成,并且第5个字母为“i”的学生记录SELECT * FROM stu WHERE sname LIKE '____i';-- 查询姓名以“z”开头的学生记录SELECT * FROM stu WHERE sname LIKE 'z%';-- 查询姓名中第2个字母为“i”的学生记录SELECT * FROM stu WHERE sname LIKE '_i%';-- 查询姓名中包含“a”字母的学生记录SELECT * FROM stu WHERE sname LIKE '%a%';-- 去除重复记录SELECT DISTINCT * FROM stu;-- 查询所有列SELECT * FROM emp;-- 查看雇员的月薪与佣金之和SELECT *,sal+comm FROM emp; -- 或者SELECT *,sal+IFNULL(comm,0) FROM emp;-- 给列名添加别名SELECT *, sal+IFNULL(comm,0) AS total FROM emp; -- 或者SELECT *,sal+IFNULL(comm,0) total FROM emp;-- 查询所有学生记录,按年龄升序排序SELECT * FROM stu ORDER BY age;SELECT * FROM stu ORDER BY age ASC;-- 查询所有学生记录,按年龄降序排序SELECT * FROM stu ORDER BY age DESC;-- 查询所有雇员,按月薪降序排序,如果月薪相同时,按编号升序排序SELECT * FROM emp ORDER BY sal DESC,empno ASC;-- 查询emp表中记录数SELECT COUNT(*) AS cnt FROM emp;-- 查询emp表中有佣金的人数SELECT COUNT(comm) AS cnt FROM emp;-- 查询emp表中月薪大于2500的人数SELECT COUNT(*) AS cnt FROM emp WHERE sal > 2500;-- 统计月薪与佣金之和大于2500元的人数SELECT COUNT(*) AS cnt FROM emp WHERE sal+IFNULL(comm,0) > 2500;-- 查询有佣金的人数,以及有领导的人数SELECT COUNT(comm), COUNT(mgr) FROM emp;-- 查询所有雇员月薪和SELECT SUM(sal) FROM emp;-- 查询所有雇员月薪和,以及所有雇员佣金和SELECT SUM(sal),SUM(comm) FROM emp;-- 查询所有雇员月薪+佣金和SELECT SUM(sal+IFNULL(comm,0)) FROM emp;-- 统计所有员工平均工资SELECT AVG(sal) FROM emp;-- 查询最高工资和最低工资SELECT MAX(sal),MIN(sal) FROM emp;-- 查询每个部门的部门编号和每个部门的工资和SELECT deptno, SUM(sal) FROM emp GROUP BY deptno;-- 查询每个部门的部门编号以及每个部门的人数SELECT deptno, COUNT(*) AS cnt FROM emp GROUP BY deptno;-- 查询每个部门的部门编号以及每个部门工资大于1500的人数SELECT deptno, COUNT(*) FROM emp WHERE sal>1500 GROUP BY deptno;-- 查询工资总和大于9000的部门编号以及工资和SELECT deptno, SUM(sal) FROM emp GROUP BY deptno HAVING SUM(sal) > 9000;-- -- 查询所有列SELECT * FROM dept;-- 创建学生信息表 studentCREATE TABLE student(    sid         INT,        -- 学号    sname         VARCHAR(20),    -- 姓名    age        INT,        -- 年龄    scores        INT,        -- 成绩    gender        VARCHAR(6),    -- 性别    birthday    DATE        -- 出生日期);-- 添加数据INSERT INTO student(sid,sname,age,scores,gender) VALUES (0001,'张华',23,49,'女');INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0007,'孙萌',20,59,'女','1997-02-24');INSERT INTO student(sid,sname,age,scores,gender) VALUES (0002,'李四',28,69,'男');INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0009,'吴笑清',22,68,'女','1994-11-02');INSERT INTO student(sid,sname,age,scores,gender) VALUES (0003,'王小花',26,29,'女');INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0004,'赵六',22,85,'男','1990-12-17');INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0005,'钱雨',21,87,'女','1992-03-30');INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0006,'李四',25,83,'男','1996-05-13');INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (0008,'周董',27,99,'男','1995-07-23');INSERT INTO student(sid,sname,age,scores,gender,birthday) VALUES (00010,'郑源',27,79,'男','1994-08-23');-- 查询所有列SELECT * FROM student;-- 删除表 studentDROP TABLE student;-- 去除重复记录SELECT DISTINCT * FROM student;-- 显示mydb下的所有表USE mydb; SHOW TABLES;-- -- 创建表:定义列之后独立指定主键CREATE TABLE stu1(    sid        CHAR(6),    sname    VARCHAR(20),    age        INT,    gender    VARCHAR(10),    PRIMARY KEY(sid));-- 创建表:指定主键且自增长CREATE TABLE stu11(    sid    INT(6) PRIMARY KEY AUTO_INCREMENT,    sname    VARCHAR(20),    age    INT,    gender    VARCHAR(10));-- 添加数据(表:stu1或者stu11)INSERT INTO stu11(sname,age,gender) VALUE ('张三',23,'男');INSERT INTO stu11(sname,age,gender) VALUE ('李四',24,'男');INSERT INTO stu11(sname,age,gender) VALUE ('李思思',26,'女');INSERT INTO stu11(sname,age,gender) VALUE ('景甜',27,'女');INSERT INTO stu11(sname,age,gender) VALUE ('刘亦菲',29,'女');INSERT INTO stu11(sname,age,gender) VALUE ('刘诗诗',25,'女');-- 删除数据DELETE FROM stu11 WHERE sname='刘诗诗';-- 查询所有列SELECT * FROM stu11;-- 修改表时指定主键ALTER TABLE stu ADD PRIMARY KEY(sid);-- 修改表时设置主键自增长ALTER TABLE stu11 CHANGE sid sid INT AUTO_INCREMENT;-- 修改表时删除主键自增长ALTER TABLE stu11 CHANGE sid sid INT;-- 删除主键(只是删除主键约束,而不会删除主键列)ALTER TABLE stu DROP PRIMARY KEY;-- -- 创建用户信息表 user,并指定主键且自增长CREATE TABLE USER(    uid        INT PRIMARY KEY AUTO_INCREMENT,    username    VARCHAR(32),    userpassword    VARCHAR(32)    );-- 查询所有列SELECT * FROM USER;-- 删除表 DROP TABLE USER;-- 添加数据INSERT INTO USER(username,userpassword) VALUE ('zhangsan','123');INSERT INTO USER(username,userpassword) VALUE ('lisi','1234');INSERT INTO USER(username,userpassword) VALUE ('wangwu','234');INSERT INTO USER(username,userpassword) VALUE ('zhaoliu','1234');INSERT INTO USER(username,userpassword) VALUE ('sisi','234');-- 删除数据DELETE FROM USER WHERE username='lisi';

 

转载于:https://www.cnblogs.com/rongsnow/p/5152611.html

你可能感兴趣的文章
进阶之路(基础篇) - 012 Arduino IDE 添加DHT11传感器第三方库的方法
查看>>
详谈js面向对象 javascript oop,持续更新
查看>>
Javaweb Tomcat 项目部署方式
查看>>
文字半透明显示在图片上
查看>>
express简单原理
查看>>
ubuntu安装spark on yarn
查看>>
linux网络 (一):网络配置
查看>>
基础练习 十进制转十六进制
查看>>
关于这次软件以及pda终端的培训
查看>>
react 生命周期
查看>>
jQuery上传插件Uploadify 3.2在.NET下的详细例子
查看>>
05. Java基础之异常
查看>>
spring11----基于Schema的AOP
查看>>
解决input框自动填充为黄色的问题
查看>>
音视频基础知识(一)
查看>>
JAVA⑤
查看>>
CyclicBarrier的使用
查看>>
Intent 传值和 Bundle传值的区别
查看>>
thinkphp的select和find的区别
查看>>
小程序开发笔记
查看>>