标签:des style blog color 使用 io 数据 for
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是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
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
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
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
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.
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 后面跟的必须是子查询。
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语句: 1. 删除表中的所有数据,保留表结构,不能使用where子句 2. 是DDL语句,不是DML语句,一旦删除,不能回退 SQL> truncate table e ; 表被截断。 SQL> select * from e; 未选定行 SQL> roll 回退已完成。 SQL> select * from e; 未选定行
使用DML语句【weber出品必属精品】,布布扣,bubuko.com
标签:des style blog color 使用 io 数据 for
原文地址:http://www.cnblogs.com/yaoweber/p/3925538.html