修改數據庫表結構


准備一張Students表,表結構為:

  Students(SId,SName,SAge)

1.更改表中某一屬性的類型

  alter table Students alter column SAge smallint

  alter table Students alter cloumn SName varchar(20)

----------------20120917---------------------

     PS:ORACLE就是個坑爹的貨 ORACLE中 修改列屬性的語法不同

     alter table tanleName modify(columnName columnType)

     這么寫就對了

    PS+1:oracle中 拼接字符串請使用||

    如:update tableName columnName = columnName+addStr (這是在sqlServer中)

 oracle中 updata tableName = column || addStr

2.添加列

  alter table Students add SAdd varchar(50)//為Students表添加sadd(地址)屬性,

3.null/not null (空/非空約束)

  只能在定義或添加列時定義

  alter table Students add SAdd varchar(50)  not null //缺省值是null

4.default(默認值約束)

  在添加列時定義默認值

  alter table Students add SAdd varchar(50) default '上海市'

  單獨添加默認值

  alter table Students add constraint def default '上海市' for SAdd

5.unique(唯一約束)

  在添加列時定義唯一約束

  alter table StUdents add SIdCard int(18) constraint un unique //原表記錄數據條數必須≤1

  單獨添加唯一約束

  alter table Students add constraint un unique(SIdCard) //必須保證該屬性在已有數據中是唯一的

 6.check(檢查約束)

  在添加列是定義檢查約束

  alter table Students add SSex int(1) constraint ck1 check(SSex = '0' or SSex = '1') //ck1為約束名

  單獨添加檢查約束

  alter table Students add constraint ck2 check(SAge > 0 and SAge < 100)

7.刪除約束

  alter table Students drop ck1 

8.刪除列

  alter table Students drop column SAdd //要刪除列是必須先將該列的所有約束刪除

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM