MySQL基础代码示例

MySQL基础代码示例

  • 向emp表中插入数据

    `INSERT INTO emp(id,NAME,gender,birthday,salary,entry_date,RESUME) VALUE (1,’Tom’,’m’,’2005-1-1’,10000,’2016-10-29’,’good boy’)

INSERT INTO emp(id,NAME,gender,birthday,salary,entry_date,RESUME) VALUE (2,’Alice’,’f’,’2005-1-1’,10000,’2016-10-29’,’good girl’)`

  • 将所有员工薪水修改为5000元

    UPDATE emp SET salary = 50000

  • 将姓名为’Tom’的员工薪水修改为3000元。

    UPDATE emp SET salary = 3000 WHERE NAME = 'Tom'

  • 将姓名为’Alice’的员工薪水修改为4000元,性别改为m

    UPDATE emp SET salary = 4000,gender = 'm' WHERE NAME = 'Alice'

  • 将Alice的薪水在原有基础上增加1000元。

    UPDATE emp SET salary = salary+1000 WHERE NAME = 'Alice'

  • 删除表中名称为’Tom’的记录。

    DELETE FROM emp WHERE NAME = 'Tom'


  • 创建学生表

    CREATE TABLE stu(
    sid VARCHAR(6),
    sname VARCHAR(50),
    age INT,
    gender VARCHAR(20)
    )

  • 向学生表插入一些数据

    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', 5, 'female');
    INSERT INTO stu VALUES('S_1011', 'xxx', NULL, NULL);

  • 删除emp表

    DROP TABLE emp

  • 创建雇员表并插入数据

    CREATE 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);


  • 查询指定列

    SELECT sid 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 gender ='female'


  • 模糊查询*

    • 查询姓名由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 deptno FROM emp

  • 查看雇员的月薪与佣金之和

    SELECT sal+IFNULL(comm,0) FROM emp

  • 给列名添加别名

    SELECT sal+IFNULL(comm,0) AS total FROM emp

  • 排序 order by 列名 asc(默认) desc

    SELECT * FROM stu ORDER BY age

  • 查询所有雇员,按月薪降序排序,如果月薪相同时,按编号升序排序

    SELECT * FROM emp ORDER BY sal ,empno DESC

  • 查询emp表中记录数:

    SELECT COUNT(comm) FROM emp

  • 查询emp表中月薪大于2500的人数:

    SELECT COUNT(*) FROM emp WHERE sal>2500

  • 统计月薪与佣金之和大于2500元的人数:

    SELECT COUNT(*) 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)+SUM(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(*) 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

  • 查询5行记录,起始行从0开始

    SELECT *FROM emp LIMIT 0,5


  • 约束
  • -主键约束(方法1)

    CREATE TABLE ONE(
    id INT PRIMARY KEY,
    NAME VARCHAR(20)
    )

  • 主键约束(方法2)

    CREATE TABLE ONE(
    id INT ,
    NAME VARCHAR(20),
    PRIMARY KEY(id)
    )

  • 主键约束(方法3)

    CREATE TABLE ONE(
    id INT ,
    NAME VARCHAR(20)
    );
    ALTER TABLE ONE ADD CONSTRAINT a PRIMARY KEY(id);

  • 自增长约束与唯一约束

    CREATE TABLE ONE(
    id INT PRIMARY KEY AUTO_INCREMENT,
    NAME VARCHAR(20) UNIQUE
    );

* 接下来继续完善此文!*

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值