DB2 建表,添加字段,删除字段,修改字段等常用操作
阅读原文时间:2023年07月12日阅读:2

转载: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的区别)