1. 程式人生 > >SQL server 2000資料庫基礎程式設計(一)

SQL server 2000資料庫基礎程式設計(一)

以下所有內容適用於SQL server 2000,其他版本或型別資料庫可能會有部分不同,語句都差不多,請隨機應變,例:
sysdatabases <–> sys.databases
sysobjects <–> sys.objects

一、使用、切換資料庫
例如跳到 master資料庫

use master
go

go批處理語句,用於同時執行多個語句。

二、建立、刪除資料庫
1、判斷該資料庫是否存在,如果存在就刪除;
2、建立資料庫,設定資料庫檔案、日誌檔案儲存路徑

if (exists (select * from sysdatabases where
name = 'studb')) drop database studb go create database studb on( name = 'studb', filename = 'c:\data\students.mdf' ) log on( name = 'studb_log', filename = 'c:\data\studb_log.ldf' ) go

如果是自己的使用者資料庫,直接建立就行了
三、建立、刪除表
1、判斷當前table是否存在,如果存在,刪除;
2、建立表

--建立class表
if(exists(select *from
sysobjects where name = 'class')) drop table class go create table class( id int primary key identity(1,1), name varchar(22) not null, createDate datetime default getdate() ) go
--建立student表
if(exists(select * from sysobjects where name = 'student'))
drop table student
go

create table
student( id int identity(1,1) not null, name varchar(22), age int, sex bit ) go

identity(a,b),標識列,a、b均為正整數,a表示開始數,b表示增幅,就像identity(1,1)意思就是該列自動增長,由1開始每次增加1,依次為12345···;identity(1,2),則表示id依次為13579···;identity(2,2),則表示id依次為2468···

三、給表新增欄位、修改欄位、刪除欄位

--新增欄位
alter table student add addr varchar(50)
--修改欄位
alter table student alter column addr varchar(20)
--刪除欄位
alter table student drop column addr
--新增多個欄位
alter table student 
add addr varchar(22),
    tel varchar(11),
    idCard varchar(3)
--判斷該列名是否存在,如果存在就刪除
if (exists (select * from syscolumns where id = object_id('student') and name = 'idCard'))
alter table student drop column idCard
go

if (exists (select * from information_schema.columns where table_name = 'student' and column_name = 'tel'))
alter table student drop column tel
go

四、新增、刪除約束

--新增主鍵
alter table student  
add constraint pk_id primary key(id)
--新增新列、約束
alter table student 
add number varchar(20) null constraint no_uk unique
--新增唯一約束
alter table student
add constraint name_uk unique(name)
--新增check約束
alter table student
add constraint ck_age check (age >= 15 and age <= 50)
--新增預設約束
alter table student
add constraint sex_def default 1 for sex
----- 多個列、約束一起建立--------
alter table student add
/*新增id主鍵、自增*/  
    id int identity constraint id primary key,   
/*新增外來鍵約束*/   
    number int null constraint uNumber references class(number),  
/*預設約束*/  
    createDate decimal(3, 3) constraint createDate default 2016-6-1  
go
--刪除約束
alter table student  drop constraint no_uk

五、插入資料

--新增班級表資訊
insert into class(name) values('1班');
insert into class values('2班', '2016-06-15');
insert into class values('3班', default);
 --新增學生表資訊
insert into student values('ada', 22, 1, 1);
insert into student values('lucy', 25, 0, 1);
insert into student values('petter', 24, 1, 3);
insert into student values('bob', 23, 0, 3);
insert into student values('lilei', 21, 1, 5);
insert into student values('tom', 28, 0, 5);
insert into student values('jason', null, 0, 5);
insert into student values(null, null, 0, 5);

insert into student select 'bulise' name, age, sex, cid from student where name = 'tony';
--多條記錄同時插入
insert into student
select 'jack', 23, 1, 5 union
select 'taomi', 24, 0, 3 union
select 'wendy', 25, 1, 3 union
select 'tony', 26, 0, 5;

六、查詢、修改、刪除、備份資料

--查詢資料
select * from class;
select * from student;
select id, 'bulise' name, age, sex, cid from student where name = 'tony';
select *, (select max(age) from student) from student where name = 'tony';
--修改資料
update student set name = 'hehe', sex = 1 where id = 1;
--刪除資料(from可省略)
delete from student where id = 1;
--備份、複製student表到stu
select * into stuBack from student;
select * into stu1 from (select * from stuBack);

select * from stuBack;
select * from stu1;