-
向emp表中插入數(shù)據(jù)
`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的薪水在原有基礎(chǔ)上增加1000元。
UPDATE emp SET salary = salary+1000 WHERE NAME = 'Alice'
-
刪除表中名稱為’Tom’的記錄茁影。
DELETE FROM emp WHERE NAME = 'Tom'
-
創(chuàng)建學(xué)生表
CREATE TABLE stu( sid VARCHAR(6), sname VARCHAR(50), age INT, gender VARCHAR(20) )
-
向?qū)W生表插入一些數(shù)據(jù)
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
-
創(chuàng)建雇員表并插入數(shù)據(jù)
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
-
查詢學(xué)號為S_1001豹储,或者姓名為liSi的記錄
SELECT * FROM stu WHERE sid = 's_1001' OR sname = 'lisi'
-
查詢學(xué)號為S_1001,S_1002,S_1003的記錄
SELECT * FROM stu WHERE sid IN('S_1001','S_1002','S_1003')
-
查詢學(xué)號不是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之間的學(xué)生記錄
SELECT * FROM stu WHERE age BETWEEN 20 AND 40
-
查詢性別非男的學(xué)生記錄
SELECT * FROM stu WHERE gender ='female'
- 模糊查詢*
-
查詢姓名由5個字母構(gòu)成的學(xué)生記錄
SELECT * FROM stu WHERE sname LIKE '_____'
-
查詢姓名由5個字母構(gòu)成轰异,并且第5個字母為“i”的學(xué)生記錄
SELECT * FROM stu WHERE sname LIKE '____i'
-
查詢姓名以“z”開頭的學(xué)生記錄
SELECT * FROM stu WHERE sname LIKE 'z%'
-
查詢姓名中第2個字母為“i”的學(xué)生記錄
SELECT * FROM stu WHERE sname LIKE '_i%'
-
查詢姓名中包含“a”字母的學(xué)生記錄
SELECT * FROM stu WHERE sname LIKE '%a%'
字段控制查詢
-
去除重復(fù)記錄
SELECT DISTINCT deptno FROM emp
-
查看雇員的月薪與傭金之和
SELECT sal+IFNULL(comm,0) FROM emp
-
給列名添加別名
SELECT sal+IFNULL(comm,0) AS total FROM emp
-
排序 order by 列名 asc(默認(rèn)) desc
SELECT * FROM stu ORDER BY age
-
查詢所有雇員岖沛,按月薪降序排序,如果月薪相同時搭独,按編號升序排序
SELECT * FROM emp ORDER BY sal ,empno DESC
-
查詢emp表中記錄數(shù):
SELECT COUNT(comm) FROM emp
-
查詢emp表中月薪大于2500的人數(shù):
SELECT COUNT(*) FROM emp WHERE sal>2500
-
統(tǒng)計月薪與傭金之和大于2500元的人數(shù):
SELECT COUNT(*) FROM emp WHERE ((sal + IFNULL(comm,0)))>2500
-
查詢有傭金的人數(shù)婴削,有領(lǐng)導(dǎo)的人數(shù):
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
-
統(tǒng)計所有員工平均工資
SELECT AVG(sal) FROM emp
-
查詢最高工資和最低工資:
SELECT MAX(sal),MIN(sal) FROM emp
-
查詢每個部門的部門編號和每個部門的工資和:
SELECT deptno,SUM(sal) FROM emp GROUP BY deptno
-
查詢每個部門的部門編號以及每個部門的人數(shù):
SELECT deptno,COUNT(*) FROM emp GROUP BY deptno
-
查詢每個部門的部門編號以及每個部門工資大于1500的人數(shù)
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 );
** 接下來繼續(xù)完善此文馆蠕!**