SQL Server 数据库基础编程

 

http://www.w3school.com.cn/sql/sql_syntax.asp

 

Ø Go批处理语句

用于同时执行多个语句

 

Ø 使用、切换数据库

use master
go

 

Ø 创建、删除数据库

方法1

--判断是否存在该数据库,存在就删除
if (exists (select * from sys.databases where name = 'database_name'))
drop database database_name
go
--创建数据库,设置数据库文件、日志文件保存目录
create database database_name
on(
name = 'database_name',
filename = 'c:\data\database_name.mdf'
)
log on(
name = 'database_name_log',
filename = 'c:\data\database_name_log.ldf'
)
go

 

方法2(设置文件大小)、

--判断是否存在该数据库,存在就删除
if (exists (select * from sys.databases where name = 'database_name'))
drop database database_name
go
create database database_name
--默认就属于primary主文件组,可省略
on primary (
--数据文件的具体描述
name = 'database_name_data',        --主数据文件的逻辑名
fileName = 'c:\database_name_data.mdf',     --主数据文件的物理名
size = 3MB,                  --主数据文件的初始大小
maxSize = 50MB,               --主数据文件增长的最大值
fileGrowth = 10%                --主数据文件的增长率
)
--日志文件的具体描述,各参数含义同上
log on (
name = 'database_name_log',
fileName = 'c:\database_name_log.ldf',
size = 1MB,
fileGrowth = 1MB
)
go

 

方法3(设置次数据文件)

if (exists (select * from sys.databases where name = 'database_name'))
drop database database_name
go
create database database_name
--默认就属于primary主文件组,可省略
on primary (
--数据文件的具体描述
name = 'database_name_data', --主数据文件的逻辑名
fileName = 'c:\database_name_data.mdf', --主数据文件的物理名
size = 3MB, --主数据文件的初始大小
maxSize = 50MB, --主数据文件增长的最大值
fileGrowth = 10% --主数据文件的增长率
),
--次数据文件的具体描述
(
--数据文件的具体描述
name = 'database_name2_data', --主数据文件的逻辑名
fileName = 'c:\database_name2_data.mdf', --主数据文件的物理名
size = 2MB, --主数据文件的初始大小
maxSize = 50MB, --主数据文件增长的最大值
fileGrowth = 10% --主数据文件的增长率
)
--日志文件的具体描述,各参数含义同上
log on (
name = 'database_name_log',
fileName = 'c:\database_name_log.ldf',
size = 1MB,
fileGrowth = 1MB
),
(
name = 'database_name2_log',
fileName = 'c:\database_name2_log.ldf',
size = 1MB,
fileGrowth = 1MB
)
go

 

Ø 基本数据类型

http://www.w3school.com.cn/sql/sql_datatypes.asp

 

Ø 判断表或其他对象及列是否存在

--判断某个表或对象是否存在
if (exists (select * from sys.objects where name = 'classes'))
print 'Exist';
go
if (exists (select * from sys.objects where object_id = object_id('student')))
print 'Exist';
go
if (object_id('student', 'U') is not null)
print 'Exist';
go

--判断该列名是否存在,如果存在就删除
if (exists (select * from sys.columns where object_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

 

Ø 创建、删除表

--判断是否存在当前table
if (exists (select * from sys.objects where name = 'classes'))
drop table classes
go
create table classes(
id int primary key identity(1, 2),
name varchar(22) not null,
createDate datetime default getDate()
)
go
if (exists (select * from sys.objects where object_id = object_id('student')))
drop table student
go


--创建table
create table student(
id int identity(1, 1) not null,
name varchar(20),
age int,
sex bit,
cid int
)
go

 

Ø 给表添加字段、修改字段、删除字段

--添加字段
alter table student add address varchar(50) not null;
--修改字段
alter table student alter column address varchar(20);
--删除字段
alter table student drop column number;

--添加多个字段
alter table student
add address varchar(22),
tel varchar(11),
idCard varchar(3);

--判断该列名是否存在,如果存在就删除
if (exists (select * from sys.columns where object_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 number varchar(20) null constraint no_uk unique;
--增加主键
alter table student
add constraint pk_id primary key(id);
--添加外键约束
alter table student
add constraint fk_cid foreign key (cid) references classes(id)
go
--添加唯一约束
alter table student
add constraint name_uk unique(name);
--添加check约束
alter table student with nocheck
add constraint check_age check (age > 1);
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 createDate smalldatetime null
constraint createDate_def default getDate() with values;

----- 多个列、约束一起创建--------
alter table student add
/*添加id主键、自增*/
id int identity constraint id primary key,
/* 添加外键约束*/
number int null
constraint uNumber references classes(number),
/*默认约束*/
createDate decimal(3, 3)
constraint createDate default 2010-6-1
go

--删除约束
alter table student drop constraint no_uk;

 

Ø 插入数据

insert into classes(name) values('1班');
insert into classes values('2班', '2011-06-15');

insert into student
select 'bulise' name, age, sex, cid
from student
where name = 'tony';

--多条记录同时插入
insert into student
select 'jack', 23, 1, 5 union
select 'tom', 24, 0, 3 union
select 'wendy', 25, 1, 3 union
select 'tony', 26, 0, 5;

 

Ø 查询、修改、删除数据

--查询数据
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 = 'hoho', sex = 1 where id = 1;

--删除数据(from可省略)
delete from student where id = 1;

 

Ø 备份数据、表

--备份、复制student表到stu
select * into stu from student;
select * into stu1 from (select * from stu) t;

 

Ø 利用存储过程查询表信息

--查询student相关信息
exec sp_help student;
exec sp_help classes;

 

 

 

转载自 http://hoojo.cnblogs.com/

 

posted on 2015-06-26 11:14  alex_cool  阅读(196)  评论(0编辑  收藏  举报