use 选择现有数据库。(master为系统默认数据库)
use master
go
if exists(select * from sys.databases where name = 'Student')
drop database Student
go
create database Student
go
use Student
go
create table StuInfo
(
StuId int primary key identity(1,1),
StuName varchar(20) not null,
Age int not null,
Sex char(2) not null,
CellPhone char(11) not null,
[Address] varchar(50) not null
)
1、GO是批处理的标志,GO语句把程序分成一个个代码块,即使一个代码块执行错误,它后面的代码块任然会执行。
2、if exists 判断数据库中是否存在某元素
3、drop database Student: 删除Student数据库
4、create datebase Student :创建Student数据库
5、create table StuInfo :创建数据表StuInfo
6、primary key :StuInfo表的主键
7、identity : 主键自增
8、 char存储定长数据很方便,CHAR字段上的索引效率极高,比如定义手机号char(11),那么不论你存储的数据是否达到了11个字节,都要占去11个字节的空间。
9、varchar存储变长数据,但存储效率没有CHAR高。如果一个字段可能的值是不固定长度的,我们只知道它不可能超过10个字符,把它定义为 VARCHAR(10)是最合算的
10、not null :非空约束 ,默认情况下列是可以接受 null 值,not null约束列不能接受null值
11、[Address] :Address是系统默认的字段 , 如果需要用系统默认字段就要加上 [ ] 把该字段括起来