DB2 建表,添加字段,刪除字段,修改字段等常用操作


轉載:http://blog.sina.com.cn/s/blog_67aaf4440100v01p.html,稍作修改。

--創建數據庫
create database Etp;


--連接數據庫
connect to Etp;


--斷開連接
disconnect Etp;


--查看當前數據庫下有哪些表
list tables;

--刪除表
drop table studentinfo;

--創建表的同時添加約束方式1
create table studentinfo(
 stuNo int not null,
 stuName varchar(8) not null,
 stuAge int,
 stuTel char(8),
 constraint pk_stuNo primary key(stuNo),
 constraint un_stuName unique(stuName),
 constraint ch_stuAge check(stuAge >=0 and stuAge <150)
);

 

--創建表的同時添加約束方式2(建議用第二種,簡單便捷)
create table studentinfo(
 stuNo int not null primary key,
 stuName varchar(8) not null unique,
 stuAge int check(stuAge >=0 and stuAge <150),
 stuTel char(8)
);


--查看表結構
describe table studentinfo;

//字段的增刪查改
--新增表字段
alter table studentinfo add stubirth date;

alter table studentinfo add abc int;


--刪除字段
alter table studentinfo drop column abc;


--修改字段類型
alter table studentinfo alter column stutel set data type char(11);(顏色標記為,與SQL Server的區別)


--增加一個非空約束
alter table studentinfo alter column stutel set not null;(顏色標記為,與SQL Server的區別)

 


免責聲明!

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



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