使用DML语句【weber出品必属精品】
2014-08-20 21:26 yaoweber 阅读(413) 评论(0) 编辑 收藏 举报-
DML语句包含以下语法:
INSERT:往一个表中增加新行
DELETE:从一个表中删除掉现有的行
UPDATE:更改一个表中现有的行
-
INSERT语句
语法:INSERT INTO TABLE(COLUMN1,COLUMN2,....) VALUES(VAL1,VAL2,...)
使用这种方法只能一次插入一行数据插入包含每一个列值的新行,按缺省顺序列出表中所有的列值.
- 创建带有结构的空表
SQL> create table t as select * from emp where 1=2;---1是不会等于2的,给了这个假条件是使得创建的表只有emp里面的表结构,而没有数据 Table created. SQL> select count(*) from e; COUNT(*) ---------- 0 SQL> desc e 名称 是否为空? 类型 ----------------------------------------- -------- ---------------------------- EMPNO NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2)
- 插入一条数据,日期值和字符值要用单引号括起来
SQL>insert into t values(7369,' SMITH', ' CLERK' ,' 7902',' 17-DEC-80',800,null,20); 1 row created. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 7369 SMITH CLERK 7902 17-DEC-80 800 20 SQL>commit; Commit complete.
- 指定某列进行插值
SQL> insert into t (empno,ename) values(1111,'ywb'); 1 row created. SQL> commit ; Commit complete. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 7369 SMITH CLERK 7902 17-DEC-80 800 20 1111 ywb
- truncate表,删除表的数据,不删除结构,drop才是一锅端
---Truncate是DDL语言,和其他DDL语言一样,他将进行隐式提交。
SQL> truncate table t; Table truncated. SQL> select * from t; no rows selected SQL> rollback; Rollback complete. SQL> select * from t; no rows selected
- 修改某列的默认值alter table t modify column default value;
SQL> alter table t modify sal default '100'; Table altered. SQL> insert into t (empno,ename) values(1112,'ywb'); 1 row created. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 1111 ywb 1112 ywb 100
- 插入特殊的值:例如日期---sysdate,自动获取日期
SQL> insert into t(empno,ename,hiredate) values (1113,'ywb',SYSDATE); 1 row created. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 1111 ywb 1112 ywb 100 1113 ywb 20-AUG-14 100
- 日期对格式比较敏感:虽然插入是安装yyyy-mm-dd格式,但是显示又是另一种格式
SQL> insert into e values(7521,'WARD','SALESMAN',7698,to_date('2014-08-19','yyyy-mm-dd'),1250,500,30); 已创建 1 行。 SQL> commit; 提交完成。 SQL> select * from e; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- -------------- ----- ---------- ------ 7369 SMITH CLERK 7902 17-12月-80 800 20 7499 ALLEN SALESMAN 7698 19-8月 -14 1600 300 20 7521 WARD SALESMAN 7698 19-8月 -14 1250 500 30
- 从另一个表中拷贝行:
创建带有子查询的 INSERT语句
不要使用 VALUES 子句
SQL> insert into t select * from emp where empno=7369; 1 row created. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 1111 ywb 1112 ywb 100 1113 ywb 20-AUG-14 100 7369 SMITH CLERK 7902 17-DEC-80 800 20
- 子查询中的列要与INSERT子句中的列相匹配,不然会报错
SQL> insert into t (empno,ename,job,sal) select empno,ename,job,sal from emp where empno=7499; -------------------注意:这里没有加关键字:values---- 1 row created. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 1111 ywb 1112 ywb 100 1113 ywb 20-AUG-14 100 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 1600 ---插入过多的值--- SQL> insert into t (empno,ename,job,sal) select * from emp where empno=7499; insert into t (empno,ename,job,sal) select * from emp where empno=7499 * ERROR at line 1: ORA-00913: too many values
- 替代变量插入值:
[oracle@oracle~]$ cd /u01 vi 1.sql写入如下内容: insert into t values(&DEPTNO,'&DNAME','&LOC'); ---注意插入字符需要加引号 在sqlplus中执行如下操作: SQL>@/u01/1.sql;
SQL> @/u01/1.sql; Enter value for empno: 110 Enter value for ename: yeb2 Enter value for sal: 120 old 1: insert into t(empno,ename,sal) values(&EMPNO,'&ENAME','&SAL') new 1: insert into t(empno,ename,sal) values(110,'yeb2','120') 1 row created. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 1111 ywb 1112 ywb 100 1113 ywb 20-AUG-14 100 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 1600 110 yeb2 120 6 rows selected.
- UPDATE 语句
SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 7369 SMITH CLERK 7902 17-DEC-80 800 20 SQL> update t set ename='ywb' where empno=7369; 1 row updated. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 7369 ywb CLERK 7902 17-DEC-80 800 20
- 同时更改多行多列:
SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 7369 ywb SALESMAN 7902 17-DEC-80 1600 20 7488 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 SQL> update t set (ename,job)=(select ename,job from emp where empno=7499) where empno=7369 or empno=7488; 2 rows updated. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 7369 ALLEN SALESMAN 7902 17-DEC-80 1600 20 7488 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 这里尤其要注意,set(xx,xx)=(xx,xx)后面如果跟的是值则会报错: SQL> update t set (job,sal)=('haha',120) where empno=7369 or empno=7499; update t set (job,sal)=('haha',120) where empno=7369 or empno=7499 * ERROR at line 1: ORA-01767: UPDATE ... SET expression must be a subquery 后面跟的必须是子查询。
- DELETE语句:从表中删去现存的行
SQL> delete from t where empno=7488; 1 row deleted. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 7369 ALLEN SALESMAN 7902 17-DEC-80 1600 20
- 参照另一个表来删除行:
(注:‘SCOTT’要大写)
SQL> delete t where empno in(select empno from emp where ename='SCOTT'); 1 row deleted. SQL> select * from t; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- ------ --------- ----- --------- ----- ----- ------ 7369 ALLEN SALESMAN 7902 17-DEC-80 1600 20
- TRUNCATE语句:
TRUNCATE语句: 1. 删除表中的所有数据,保留表结构,不能使用where子句 2. 是DDL语句,不是DML语句,一旦删除,不能回退 SQL> truncate table e ; 表被截断。 SQL> select * from e; 未选定行 SQL> roll 回退已完成。 SQL> select * from e; 未选定行