一、DDL和DML的差別
DDL和DML都是資料庫語言,DDL是資料庫模式定義語言(Data Definition Language),主要包括建立、修改資料庫表。DML是資料操縱語言(Data Manipulation Language),主要包括insert 、update、delete等。
二、DDL語句
1.建立資料庫表
要指明表的名稱、列的名稱、列的資料類型、列的寬度、是否有預設值等。
create table stu( name char(8), salary number(5) default 0, content char(4 char), hiredate date );
//name為8個位元組,content為4個字。
在現有表的基礎上建立表
create table t1 as select ename name,sal salary from emp;//可以給列改别名
create table t2 (c1,c2,c3)as select ename,empno,sal from emp where 9=1;
//不想要資料,隻建立表結構,c1,c2,c3是給列起的别名
注:表的命名規則
(1)标準ASCII碼描述;
(2)必須以字母開頭;
(3)不能是保留字;
(4)可以包含大小寫字母、數字、$、#;
(5)不能和所屬使用者的其它對象重名。千萬不要使用漢語做表和列的名稱,因為漢語是 ASCII 碼所不能描述的,ORACLE 的核心是 ASCII 編寫的,你使用漢語隻是一時痛快,後患無窮。
2.查詢目前使用者所擁有的表
select object_name,object_type from user_objects;
//user_objects目前使用者所擁有的所有對象,不包含你建立的public對象
select table_name from user_tables;
//user_tables目前使用者所擁有的表,擁有表的一切權利
select * from tab; //tab是目前使用者擁有的表和視圖
3.修改表資訊
修改表的結構
如果列為null,可以随便修改列的類型和寬度
如果有資料,修改會受到限制,但不會破壞資料
如果不改變類型,隻改變寬度的話,加大寬度是可以的
alter table t1 modify(name char(12));
alter table t1 modify(name number(12));//如果列為null,可以改變列的類型
修改表的名稱
rename t1 to t_1; //必須是表的owner才可以修改表的名稱
修改列的名稱
alter table t4 rename column c1 to name;
添加表注釋
comment on table emp is 'employee table'; //添加注釋 select comments from user_tab_comments where table_name = 'emp'; //查詢表注釋
添加列注釋
comment on column emp.sal is '員工工資'; //添加列注釋 select comments from user_col_comments where tab_name='emp' and column_name='sal'; //查詢列注釋
丢棄表
drop table t2; //此語句并沒有将表真的删除,隻是改了名稱
show recyclebin;//顯示資源回收筒的資訊
select * from user_recyclebin; //查詢該使用者的資源回收筒資訊
将資源回收筒的表還原
flashback table t2 to before drop; //還原t2
flashback table t2 to before drop rename to tt2; //還原表的同時修改表的名稱
清空資源回收筒内指定的表
purge table t2; //清空資源回收筒的t2表 purge recyclebin; //徹底地删除資源回收筒裡的表
三、DML語句
1.insert 語句
insert into t1(c1,c2,c3) values(v1,v2,v3); // 列的個數和資料類型要比對
insert into t1(c2) values(sysdate); //插入目前日期
隐式插入null
在插入中沒有列出的列,就會被插入null,如果該列有default值,那麼就插入預設值
insert into dept(deptno) values(50);//其中dname,loc沒有說明,都為null
顯示插入null值
明确的寫出該列的值為null
insert into dept values(60,null,null); insert into dept(loc,dname,deptno) values(null,null,70);
日期
當插入的列為日期的時候,最好強制轉化為日期類型,預設的轉換在環境變化的時候會報錯
insert into t3(hiredate) values(to_date('080599','mmddrr'));
字元串
字元串大小寫敏感
insert into t2(c1) values ('BEIJNG'); insert into t2(c1) values('beijing');
子查詢插入
不加values關鍵字,一次可以插入多行,列的類型和位置要比對
insert into d1 select * from dept;
insert into emp2 select * from emp where deptno=30;
2.update語句
修改表中的資料
update emp set sal=sal+1;
update emp set sal=2000 where empno=7900;
update emp set comm=null where deptno=30;
用子查詢來更新
update emp2 set dname=(select dname from dept where dept.deptno=emp2.deptno);
給列設定指定值
create table t1(c1 number(2) default 10,c2 char(10) default 'bj');
update emp2 set empno=default;
3.delete删除行
delete t1; // 隻删除表t1中的所有行資料,表還在 delete t1 where sal > 2000; // 将符合條件的行删除
4.Merge語句
Merge是update和insert的結合體,有就做update,沒有就做insert,
Merge into解決用B表跟新A表資料,如果A表中沒有,則把B表的資料插入A表
文法:
MERGE INTO [your table-name] [rename your table here] USING ( [write your query here] )[rename your query-sql and using just like a table] ON ([conditional expression here] AND [...]...) WHEN MATHED THEN [here you can execute some update sql or something else ] WHEN NOT MATHED THEN [execute something else here ! ]
eg:
merge into t_a a using (select FP0,FP1,FP2,FP3,REDE from t_b) b on (a.fp0=b.fp0 and a.fp1=b.fp1 and a.fp2=b.fp2 and a.fp3=b.fp3) when matched then update set a.rede=b.rede when not matched then insert (a.fp0,a.fp1,a.fp2,a.fp3,a.org_severity,a.rede, a.event_time,a.int_id) values (b.fp0,b.fp1,b.fp2,b.fp3,b.REDE,b.redefine_severity,sysdate,7777778);
//利用表 t_b跟新表t_a的b.redefine_severity,
條件是a.fp0=b.fp0 and a.fp1=b.fp1 and a.fp2=b.fp2 and a.fp3=b.fp3,
如果tfa_alarm_act_nms表中沒有該條件的資料就插入。如果資料量很大,此sql效率非常高