与oracle不同点:
1、STR_TO_DATE('2017-10-19', '%Y-%m-%d') 函数不同
2、UPDATE t_emptest b SET b.SAL = (SELECT a.SAL FROM t_emptest a WHERE ENAME='BLAKE') WHERE ENAME='JONES';-- 否则报错, Table 'b' is specified twice, both as a target for 'UPDATE' and as a separate source for data
3、update也不支持(**,**)=(**,**)
-- 增删改查
-- 插入
-- 插入数据到dept表
SELECT * FROM t_emptest;
SELECT * FROM t_depttest;
insert into t_depttest (deptno,Dname,loc) VALUES(50,'mrg','Beijing');
INSERT INTO t_emptest(empno,ename,hiredate,sal,deptno)
-- 注意有个时间转换函数 VALUES('9005','lucy',STR_TO_DATE('2017-10-19', '%Y-%m-%d'),3000,20);
-- 修改数据
Single-table语法:
UPDATE [LOW_PRIORITY] [IGNORE] tbl_name
SET col_name1=expr1 [, col_name2=expr2 ...]
[WHERE where_definition]
[ORDER BY ...]
[LIMIT row_count]
-- 修改雇员编号90057839 的工资为1991 UPDATE t_emptest SET sal=9000 WHERE EMPNO=9005 OR EMPNO=7839; -- 有个或
-- 修改雇员编号77*开头的雇员的工资为原来的两倍,佣金为500 UPDATE t_emptest SET sal = sal*2,COMM=500 WHERE EMPNO LIKE '77%'; -- 有个模糊匹配
-- 修改JONES的工资为BLAKE的工资 UPDATE t_emptest b SET b.SAL = (SELECT * from (SELECT a.SAL FROM t_emptest a WHERE ENAME='BLAKE') as T) WHERE ENAME='JONES'; -- 需要另外保存一个表
--- 上面这个需要特别注意
-- 有问题,执行报错
UPDATE t_emptest b SET b.SAL = (SELECT a.SAL FROM t_emptest a WHERE ENAME='BLAKE') WHERE ENAME='JONES';-- 否则报错, Table 'b' is specified twice, both as a target for 'UPDATE' and as a separate source for data
-- 修改雇员lucy的入职时间
UPDATE t_emptest SET HIREDATE=STR_TO_DATE('2017-01-02','%Y-%m-%d') WHERE EMPNO=9000;
-- 修改lucy,empn=9000的补助和工资,与CLARK的工资补助相同
-- 先查询CLARK的工资补助
-- 然后将lucy的工资与补助更新为CLARK的工资补助
SELECT sal,COMM FROM t_emptest WHERE ENAME='CLARK';
-- update不支持(**,**)=(**,**)
-- UPDATE t_emptest SET (sal,comm) = (9800,500) WHERE ENAME = 'lucy' AND EMPNO=9000; -- 错误的
UPDATE t_emptest SET sal=(SELECT * from (SELECT sal FROM t_emptest WHERE ENAME='CLARK') AS S1),comm=(SELECT * FROM (SELECT comm FROM t_emptest WHERE ENAME='CLARK') AS S2) WHERE ENAME = 'lucy' AND EMPNO=9000;
-- 删除数据
-- DELETE 删除整行数据 DELETE FROM t_emptest WHERE empno like '900_'; -- _匹配一个字符,%匹配任意字符