Oracle操作语句之DDL语句
生活随笔
收集整理的這篇文章主要介紹了
Oracle操作语句之DDL语句
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
DDL:數(shù)據(jù)庫定義語言,可以自動(dòng)提交事物。(create alter drop rename truncate comment)
數(shù)據(jù)庫三大范式
第一范式:列中的值不能再分割 第二范式:在滿足第一范式的基礎(chǔ)上,設(shè)計(jì)的表中的每個(gè)列都要依賴于主鍵列。 第三范式:在滿足第二范式的基礎(chǔ)上,所有的列都必須直接依賴于主鍵列,不能間接依賴于主鍵列(即不能發(fā)生依賴傳遞)建表語句格式: create table 表名(列名1 數(shù)據(jù)類型 列級(jí)約束,列名2 數(shù)據(jù)類型 列級(jí)約束,列名3 數(shù)據(jù)類型 列級(jí)約束,............)或者 create table 表名(列名1 數(shù)據(jù)類型 列級(jí)約束,列名2 數(shù)據(jù)類型 列級(jí)約束,列名3 數(shù)據(jù)類型 列級(jí)約束,.............表級(jí)約束1,表級(jí)約束2,..............)比如 create table teacher(id number,name varchar2(20) not null,email varchar2(100),gender char,constraint tea_id_pk primary key(id),constraint tea_emmail_ck unique(email),constraint tea_gender_ck check(gender in('f','m')));約束:維持兩張表之間主外鍵關(guān)系的關(guān)鍵
oracle數(shù)據(jù)庫中的五種約束:
主鍵約束:用來唯一標(biāo)示表中的一個(gè)列,一個(gè)表中的主鍵約束只能有一個(gè),但是可以在一個(gè)主鍵約束中包含多個(gè)列,也稱為聯(lián)合約束。 primary key 外鍵約束:用來約束兩個(gè)表中列之間的關(guān)系。 foreign key 唯一約束:用來唯一標(biāo)示表中的列。與主鍵約束不同的是,在一個(gè)數(shù)據(jù)表中可以有多個(gè)唯一約束。 unique 檢查約束:用來約束表中列的輸入值得范圍,比如在輸入性別時(shí),要求數(shù)據(jù)庫中只能輸入男或者女,就可以使用檢查約束來約束該列。 check 非空約束:約束該列一定要輸入值。not null(只能寫成行級(jí)約束,不能寫成表級(jí)約束。)聯(lián)合主鍵的表級(jí)聲明 create table teacher(id number,pro number,constraint teacher_id_pro_pk primary key(id,pro)) 聯(lián)合外鍵的表級(jí)聲明: create table order(id number primary key,price number not null,teacher_id number,pro_id number,constraint order_cus_id_pro_fk foreign key (teacher_id,pro_id) references teacher(id,pro)) 使用語句創(chuàng)建一張和s_dept一樣結(jié)構(gòu)的表: create table test1as select * from s_dept;(此時(shí)test1中有s_dept的結(jié)構(gòu)和所有的數(shù)據(jù)信息。)
或者
create table test2as select * from s_deptwhere 1>1;(此時(shí)test2中有s_dept的結(jié)構(gòu),但沒有任何的數(shù)據(jù)信息。)
修改表的名字:(rename ...to...)
格式:rename old_name to new_name; 例如:rename test to mytest;(將test表的名字改為mytest)修改表中某列的名字:格式:alter table table_name rename column old_name to new_name 例如:alter table test rename column name to myname;(將test表中列為name的列改為myname)修改表中某列的數(shù)據(jù)類型: 格式:alter table table_name modify(列名 新的數(shù)據(jù)類型) 例如:alter table test modify (name (varchar2(500)));(修改test表中name列的數(shù)據(jù)類型)向表中添加一列:(alter,add)
格式:alter table table_name add 添加列的名字 添加列的類型 例如:alter table test add birthday date;(在test表中添加birthday列 數(shù)據(jù)類型設(shè)置為date)向表中刪除某一列:(alter,drop column) 格式:alter table table_name drop column 要?jiǎng)h除的列名 例如:alter table test drop column name;(刪除test表中的name列) 向表中某列添加列的表級(jí)約束:(aletr,add,constraint)
格式:alter table table_name add constraint 約束名(unique , not null等)(添加約束的列名); 例如:alter table test add constraint test_name_un unique(name);(給test表中的name列添加唯一性約束)刪除表中的某一列的表級(jí)約束
格式:alter table table_name drop constraint 約束名 例如:alter table test drop constraint test_name_un;(刪除test表中的test_name_un約束)讓約束失效(此時(shí)必須知道約束的名字)
格式:alter table table_name disable constraint 約束名; 例如:aletr table test disable constraint test_name_un;(讓test表中的test_name_un約束失效) 讓失效的約束再次生效。 格式:alter table table_name enable constraint 約束名; 例如:aletr table test enable constraint test_name_un;(讓test表中的test_name_un約束再次生效) 刪除表中的數(shù)據(jù),不需要提交,默認(rèn)已經(jīng)提交,并且不能回滾。 格式:truncate table table_name 例如:truncate table test;(刪除test表中的數(shù)據(jù)) 給表加注釋,會(huì)自動(dòng)提交事物
格式: comment on table table_name is '添加的注釋內(nèi)容' 例如:comment on table test is 'ok';(給test表添加注釋)查看表中的注釋,表的名字要大寫 格式:select * from user_tab_comments where table_name='表的名字'; 例如:select * from user_tab_comments where table_name='TEST';(查看test表中的注釋) 給表中的列加注釋,會(huì)自動(dòng)提交事物 格式:comment on column table_name.列名 is '添加的注釋內(nèi)容'; 例如:comment on column test.name is 'good';(給test表的name列添加注釋)查看表中列的注釋,表的名字要大寫
格式:select * from user_col_comments where comments is null and table_name='表的名字'; 例如: select * from user_col_comments where comments is null and table_name='TEST';(查看test表中的列的注釋)
總結(jié)
以上是生活随笔為你收集整理的Oracle操作语句之DDL语句的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: macOS Monterey下找到桌面图
- 下一篇: 基于功率分析的侧信道攻击简介