DB2中SQL基本語句的操作
完全轉載自:http://blog.sina.com.cn/s/blog_67aaf4440100v01p.html
--創建數據庫
create database Etp;
--連接數據庫
connect to Etp;
--斷開連接
disconnect Etp;
--查看當前數據庫下有哪些表
list tables;
--建表
create table studentInfo(
stuno char(5) not null,
stuname varchar(8),
stubirth date
);
--查看表結構
describe table studentinfo;
--新增表字段
alter table studentinfo add stutel int;
alter table studentinfo add abc int;
--修改字段類型
alter table studentinfo alter column stutel set data type
char(11);
--刪除字段
alter table studentinfo drop column abc;
--增加一個非空約束
alter table studentinfo alter column stuname set not null;
--重構表
reorg table studentinfo;
--增加一個唯一約束
alter table studentinfo alter column stutel set not null;
alter table studentinfo add constraint un_stutel
unique(stutel);
--添加檢查約束
alter table studentinfo add column stuAge int;
alter table studentinfo add constraint ch_stuAge check(stuAge
> 0 and stuAge <150);
--添加主鍵約束
alter table studentinfo add constraint pk_stuno primary
key(stuno);
--刪除表
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)
);
--添加主外鍵
--新增班級表
create table classInfo(
classId int not null primary key,
className varchar(20)
);
--建表的同時添加外鍵
create table studentinfo(
stuNo int not null,
stuName varchar(8) not null,
stuBirth date not null,
stuAge int,
stuTel char(8),
fclassId int,
stuBirth date not null,
constraint pk_stuNo primary key(stuNo),
constraint un_stuName unique(stuName),
constraint ch_stuAge check(stuAge
>=0 and stuAge <150),
constraint fk_fcalssId foreign key(fclassid)
references classInfo(classId)
);
-- 自增
create table studentinfo(
stuNo int not null generated always as
identity(start with 1 ,increment by 1),
stuName varchar(8) not null,
stuAge int,
stuTel char(8),
fclassId int,
stuBirth date not null,
constraint pk_stuNo primary key(stuNo),
constraint un_stuName unique(stuName),
constraint ch_stuAge check(stuAge
>=0 and stuAge <150),
constraint fk_fcalssId foreign key(fclassid)
references classInfo(classId)
);
--先建表再添加外鍵
alter table studentinfo add constraint fk_classId foreign
key(fclassid) references classInfo(classId);
--從系統表中查詢約束名
select constname, tabname, refkeyname, reftabname, colcount,
deleterule, updaterule from syscat.references;
--插入
insert into classinfo values(1,‘ETP-1‘);
insert into studentInfo
values(1,‘xy‘,20,‘12345‘,1,‘1990-02-28‘);
--不是全部插入則需要寫列名
insert into studentinfo(stuNo,stuName,stuTel)
values(2,‘wj‘,‘111‘);
-- 有自增長的列要寫清楚列名
insert into studentinfo(stuName,stuAge,stuTel,fclassid,stuBirth)
values(‘xy‘,20,‘12345‘,1,‘1990-02-28‘);
insert into studentinfo(stuName,stuAge,stuTel,fclassid,stuBirth)
values(‘tom‘,22,‘12345‘,2,‘1990-02-28‘);
--更新
update studentinfo set stuBirth = ‘1990-02-21‘ where
stuName=‘xy‘;
update studentinfo set stuBirth = ‘1990-02-21‘,stuAge = 21 where
stuName=‘xy‘;
--刪除
deleted from studentinfo where stuName=‘xy‘;
--查詢
select * from studentinfo where stuName=‘xy‘;
select stuName,stuAge from studentinfo;
--別名查詢
select stuName as 姓名,stuAge as 年齡 from studentinfo;
select s.stuName as 姓名,s.stuAge as 年齡 from studentinfo s;
--運算查詢
select s.stuName as 姓名,s.stuAge+5 as 年齡 from studentinfo s;
--串聯運算查詢
select stuName||stuAge from studentinfo;
--and 和 or
select s.stuName as 姓名,s.stuAge+5 as 年齡 from studentinfo s where
s.stuName=‘xy‘ and s.stuAge=20;
select s.stuName as 姓名,s.stuAge+5 as 年齡 from studentinfo s where
s.stuName=‘xy‘ or s.stuAge=20;
--null
select * from studentinfo where stuAge is
null;
select * from studentinfo where stuAge is not
null;
--between and 包括邊界 相當於>=和<=s
select s.stuName as 姓名,s.stuAge+5 as 年齡 from studentinfo s where
s.stuAge between 10 and 20
--in
select * from studentinfo where stuName in
(‘xy‘,‘wj‘);
select * from studentinfo where stuName not in
(‘xy‘,‘wj‘);
--模糊查詢 like%,%表示多個字符
select * from studentinfo where stuName like ‘x%‘
--模糊查詢 like_ , _表示單個字段
select * from studentinfo where stuName like ‘x_‘;
--排序 order by
select * from studnetinfo order by fclassid desc;
select * from studnetinfo order by fclassid asc;
--distinct去掉重復
select distinct stuAge as 年齡 from studentinfo;
--group by,使用的時候,select 後面只能加2種字段: 1.group by 後面出現的,2.聚合函數
select fclassId as 班級號,count(stuName) as 學生個數 from studentinfo group by fclassid;
-having 在分組的基礎上過濾,出現順序where-group by-having
select fclassId as 班級號,count(stuName) as 學生個數 from studentinfo group by fclassid having count(StuName)>=2
DB2中SQL基本語句的操作