9、操纵数据

九、操纵数据

数据操纵语言:
1、向表添加新行:add new rows to a table
2、更新表现有的行:modify existing rows in a table
3、从表中删除现有行:remove existing rows from a table

insert语法:
插入新行 inserting new rows
1、列表中的值的顺序与insert语句列表中列顺序一致或与表中顺序一致
2、在insert语句列出列是可选的
3、字符和日期的需要单引号引起来

SQL> insert into dept(deptno,dname,loc) values(50,'public','xian');

插入空值:null或者''
插入特定值:sysdate
时间插入法:to_date('feb 3, 1999','mon dd, yyyy')
替代变量插入:&
从其他表复制:不使用关键字values,列中的个数,类型,顺序必须与子查询相匹配,子查询中的所有行被插入到行中
insert into emp.exit (ename,sal,comm)
select ename,sal,comm from emp;

更改表中数据:changing data in a table
修改存在数据用update

update emp set deptno=10 where deptno=20;
修改指定行用where,缺少where修改所有行
指定set column_name=null会更新列为空值
例:
update emp set job=(select job from emp where empno=10),
sal=(select sal from emp where empno=10) 
where empno=20;
将员工号为20的人的薪水和工作与10号相同

update子查询语句更新的行记录来自另一个表
update copy_emp set deptno=(select deptno from emp where empno=100)
where job=(select job from emp where empno=200);

删除行记录:remove a row from table
删除指定行用where语句
delete from dept where deptno=10;
缺少where语句删除所有行
delete from copy_emp;

truncate语句:
1、从表中删除所有行,保留表结构
2、DDL语句,不容易恢复
truncate table emp_copy;

事务开始与结束:database transactions :start and end

保存点:savepoint对当前事物做个保存点
rollback to savepoint 回到指定保存点

猜你喜欢

转载自blog.csdn.net/weixin_43403578/article/details/89281729