您的位置:首页 > 数据库

利用T—SQL语句添加约束

2013-04-19 17:54 225 查看
-----检查数据库是否存在-------

use master

go

if exists(select*from sysdatabases where name='studentdb')

drop database studentdb

------建数据库--------

create database studentdb

on primary(

name='student',

filename='d:\data\student.mdf',

size=15,

maxsize=25,

filegrowth=10%

)

log on (

name='student_log',

filename='d:\data\student_log.ldf',

size=20,

filegrowth=1

)

------修改数据库--------

alter database studentdb

modify file(

name='student_log',

maxsize=500

)

alter database studentdb

modify file(

name='student',

filegrowth=3

)

------创建表---------

use studentdb

go

if exists(select * from sysobjects where name='t_class')

drop table t_class

create table t_class(

C_Id char(6) primary key not null,

C_Number varchar(30) unique not null,

C_Depart varchar(30) null

)

use studentdb

go

if exists(select * from sysobjects where name='t_student')

drop table t_student

create table t_student(

S_Id char(6) primary key not null,

C_Id char(6) not null,

S_Name varchar(30) null,

S_Gender char(2) check(S_Gender in('男','女')) null,

S_Birthday datetime not null,

S_Nation char(10) null,

s_idcard varchar(18) check(len(s_idcard) in(15,18)) not null,

s_email varchar(50) check(s_email like '%@%') not null

)

-------向表中添加信息---------

insert into t_class values('01','软件技术班','计算机技术系')

insert into t_class values('02','网络技术班','计算机技术系')

insert into t_class values('03','会计班','经贸系')

insert into t_student (S_Id,C_Id,S_Name,S_Gender,S_Birthday,S_Nation) values ('001','01','李林','男','1998-8-9','汉')

insert into t_student (S_Id,C_Id,S_Name,S_Gender,S_Birthday,S_Nation) values ('002','01','黄莺','女','1980-11-2','满')

insert into t_student (S_Id,C_Id,S_Name,S_Gender,S_Birthday,S_Nation) values ('003','03','张华','男','1991-3-19','汉')

------修改信息---------

update t_student set S_Name='李大林' where S_Id=001

--------显示男同学的信息------

select * from t_student where S_Gender='男'

---------显示特定信息-----

select * from t_student where year(S_Birthday)=1998

--------删除特定信息-------

delete from t_student where C_Id='01'

select * from t_student
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: