一、DML陳述句(資料操作陳述句)
1.添加資料到表中
insert into 表名(欄位名) values(值)
注:1)使用values關鍵字只能插入一條資料
2)注意完整性約束
3)插入的值必須與欄位的順序一樣
4)可以給表中的所有欄位插入值,也可以給表中插入子表中的資料
5)先插入父表的資料再插入子表的資料
6)可以跟子查詢,同時插入多條資料
//向s_stu插入一條資料
insert into s_stu
values(1,'tom',null,null,null,null);
insert into s_stu
values(2,'tom',null,null,'f',null);
insert into s_stu
values(3,'lucky',18,'18625034564','m',41); insert into s_stu(id,name,age)
values(4,'jack',18);
insert into s_stu(id,name)
select id,last_name
from s_emp
where id>5;
2.跟新表中的資料 update update 表名 set 欄位名1=值1,欄位名2=值2......... [where 條件] 1)不加where會更新學生表中的所有當前欄位的資訊
更新學生表中的age=20
update s_stu
set age=20; 2)加where 更新表中的某些資料 更新滿足where條件的資料
更新學生表中的id大于20的age為99
update s_stu
set age=99
where id>20;
3)注意完整性約束
更新學生表中的id=10的age為50,phone為1111
update s_stu
set age=50,phone='1111'
where id=10;
3.洗掉資料 delete [from] 表名
[where 條件]
1)加where 洗掉表中的某些資料
洗掉滿足where條件的資料
//洗掉id=10的學生資訊
delete from s_stu
where id=10;
2)不加where,洗掉表中的所有資料,表結構還在
3)先洗掉子表的資料,再洗掉父表的資料
4.事務
在同一個操作單元中的一系列操作,這些操作要么同時成功 要么同時失敗 并且這些操作是原子單位,是不可再分的
回滾事務:rollback
提交事務:commit
(1)事務什么時候開始:
1)一個事物結束代表另外一個事物開始
2)打開終端,一個新的事務就開始了
(2)事務什么時候結束:
1)正常情況結束
a.執行commit
b.執行rollback
c.執行DDL,DCL命令會自動提交
d.執行DML陳述句不會自動提交(需要手動提交或回滾)
e:exit 會自動提交
2)非正常結束
a.點擊退出按鈕
b.系統錯誤 (3)事務四大特性:ACID 原子性:事務是一個原子,事務不可再分
一致性:事務操作前和事務操作后的資料要保持一致性
隔離性:一個事務是看不見另外另一個事務的資料
持久性:將事務的資料持久化的報訊保存到磁盤上
insert into.....
savepoint a
update....
savepoint b
insert into....
delete
rollback to a/b
//設定a,b保存點 回滾到想要留下的操作
二、改變表(alter table,rename...) 1.添加新的欄位
alter table 表名
add (欄位名 資料型別 默認值 約束
,欄位名,,,,,,,,,)
向s_stu添加comments
alter table s_stu
add comments varchar2(100);
2.洗掉表中的欄位
alter table 表名
drop column 欄位名;
洗掉comments
alter table s_stu
drop column comments;
3.修改表中的欄位
alter table 表名
modify(欄位名 資料型別 默認值 約束 );
修改s_stu的dept_id約束改為not null
alter table s_stu
modify dept_id number(7) not null;
注意:值要注意一些情況(約束 資料型別 )
修改:資料型別(值都為null),約束(是否符合約束),資料型別的長度(長度是否能放下表中的數 據)
4.增加約束(表級約束,所以不能增加非空約束)
alter table 表名
add 取約束名字 約束型別(欄位);
5.洗掉約束
alter table 表名
drop constraint 約束名字
6.使約束失效 ---->約束還在 只是不起作用
alter table 表名
disable constraint 約束名;
7.使約束生效 ---->判斷存在的值是否符合約束
alter table 表名
enable constraint 約束名; 8.洗掉表
drop table 表名;
a.洗掉表中的資料 以及表結構;
b.是DDL 不能回滾
c.drop可以洗掉任意物件
delete from 表名;
a.洗掉表中的資料 不會洗掉表結構;
b.是DML 可以回滾
c.洗掉資料效率低
truncate table 表名;
a.洗掉表中的資料 不會洗掉表結構;
b.是DDL 不能回滾
c.truncate只能洗掉表
d.洗掉效率高 9.重命名rename
rename 舊名字 to 新名字 ; 10.重命名(欄位的名字)
alter table 表名
rename column 舊欄位名 to 新欄位名
11.給表增加注釋
comment on table 表名
is '注釋的內容';
comment on table s_stu
is 'This is a Student info';
//查看表的注釋
select comments
from all_tab_comments
where table_name='TEST';
//表中的列加注釋
comment on column test.id
is '序列號';
//查看列的注釋
select comments
from user_col_comments
where table_name='TEST';
用戶表:用戶通過create table 創建的表
需要用戶自己維護 自己增刪改操作
資料字典表:資料庫創建的表,資料庫自己維護,資料庫自己增刪改操作,
eg:user_constraint
//查詢s_stu表的注釋
select comment
from all_tab_comments
where table_name='S_STU';//表名大寫
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/1155.html
標籤:Oracle
上一篇:Oracle資料庫備份和恢復
