1. 程式人生 > >Oracle操作語句之DDL語句

Oracle操作語句之DDL語句

DDL:資料庫定義語言,可以自動提交事物。(create alter drop rename truncate comment)

資料庫三大正規化

第一正規化:列中的值不能再分割
第二正規化:在滿足第一正規化的基礎上,設計的表中的每個列都要依賴於主鍵列。
第三正規化:在滿足第二正規化的基礎上,所有的列都必須直接依賴於主鍵列,不能間接依賴於主鍵列(即不能發生依賴傳遞)
建表語句格式:
    create table 表名(
     列名1 資料型別 列級約束,
     列名2 資料型別 列級約束,
     列名3 資料型別 列級約束,
    ............
    )
或者
    create table 表名(
     列名1 資料型別 列級約束,
     列名2 資料型別 列級約束,
     列名3 資料型別 列級約束,
     .............
     表級約束1,
     表級約束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'))
      );


約束:維持兩張表之間主外來鍵關係的關鍵

oracle資料庫中的五種約束:

主鍵約束:用來唯一標示表中的一個列,一個表中的主鍵約束只能有一個,但是可以在一個主鍵約束中包含多個列,也稱為聯合約束。
primary key
外來鍵約束:用來約束兩個表中列之間的關係。
foreign key
唯一約束:用來唯一標示表中的列。與主鍵約束不同的是,在一個數據表中可以有多個唯一約束。
unique
檢查約束:用來約束表中列的輸入值得範圍,比如在輸入性別時,要求資料庫中只能輸入男或者女,就可以使用檢查約束來約束該列。
check
非空約束:約束該列一定要輸入值。not null(只能寫成行級約束,不能寫成表級約束。)

聯合主鍵的表級宣告
create table teacher(
	 id number,
	 pro number,
	 constraint teacher_id_pro_pk primary key(id,pro)
	)
聯合外來鍵的表級宣告:
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)
       )
使用語句建立一張和s_dept一樣結構的表:
create table test1
    as 
    select * from s_dept;(此時test1中有s_dept的結構和所有的資料資訊。)

或者

create table test2
    as 
    select * from s_dept
    where 1>1;(此時test2中有s_dept的結構,但沒有任何的資料資訊。)
*************************表結構的修改(alter drop rename truncate comment的使用):

修改表的名字:(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)
修改表中某列的資料型別:
格式:alter table table_name modify(列名 新的資料型別)
例如:alter table test modify (name (varchar2(500)));(修改test表中name列的資料型別)
向表中新增一列:(alter,add)
格式:alter table table_name add 新增列的名字  新增列的型別
例如:alter table test add birthday date;(在test表中新增birthday列 資料型別設定為date)
向表中刪除某一列:(alter,drop column)
格式:alter table table_name drop column 要刪除的列名
例如:alter table test drop column name;(刪除test表中的name列)
向表中某列新增列的表級約束:(aletr,add,constraint)
格式:alter table table_name add constraint 約束名(unique , not null等)(新增約束的列名);
例如:alter table test add constraint test_name_un unique(name);(給test表中的name列新增唯一性約束)
刪除表中的某一列的表級約束
格式:alter table table_name drop constraint 約束名
例如:alter table test drop constraint test_name_un;(刪除test表中的test_name_un約束)
讓約束失效(此時必須知道約束的名字)
格式: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約束再次生效)
刪除表中的資料,不需要提交,預設已經提交,並且不能回滾。
格式:truncate table table_name
例如:truncate table test;(刪除test表中的資料)
給表加註釋,會自動提交事物
格式: comment on table table_name  is '新增的註釋內容'
例如: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表中的註釋)
給表中的列加註釋,會自動提交事物
格式:comment on column table_name.列名 is '新增的註釋內容';
例如: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表中的列的註釋)