一、设置默认值
create table emp3(emp_id int primary key auto_increment,name varchaar(30),address varchar(30) default "Unknown");address设置默认值为Unknown
insert into emp3(name) values("admin");选择列插入
insert into emp3 values(default,"lodlu",default,default);完列插入
二、添加数据
insert into emp values(0,'张三',1000);完成插入(当存在自增长列时,可以使用default,null,0来占位)
insert into emp(name,salary) values('张三',1000);指定列插入
三、更新数据
更新的表不能在set和where中使用子查询
update后面可以做任意的查询
Oracle:update emp3 e set e.address = (select address from emp3 where emp_id = 1) where e.emp_id = 2;
MySQL: update emp3 e,(select address from emp3 where emp_id = 1)t set e.address = t.address where e.emp_id = 2;
MySQL: update emp3 e set e.address =(select t.address from (select * from emp3)t where t.emp_id = 2) where e.emp_id = 1;
四、删除数据
delete emp from emp where emp_id = 1;删除指定行
delete from emp;删除全部数据
truncate table emp;清空全部数据
truncate是整体清空不做服务器log效率较高,delete是逐条删除做服务器log效率较低。
truncate重置自增长,delete不重置
五、事务处理
start transaction关闭事务自动提交
commit/rollback