想要创建数据表,首先你要使用某个数据库
use test
创建数据表
create table 表名
(
字段名1 数据类型
字段名2 数据类型
...
)
示例1:
use test
create table Department
(
DepartmentId int primary key identity(1,1),
DepartmentName varchar(10) not null,
DepartmentRemark text
)
ps:字符串类型的一些区别
char:定长,比如char(10),无论存储数据是否真的到了10个字节,都要占用10字节 类似C语言的char[10],也就是10个字节大小的char数组
varchar:变长,比如varchar(10),是最多占10个字节,你里面放1个字节,就占1个字节
text:长文本,比如你有几千几万字,你就可以用这个
char,varchar,text前面加n(比如nvarchar):存储unicode字符,对中文友好,比如varchar(100):存储100个字母或者50个汉字, nvarchar(100):存储100个字母或100个汉字,
示例2:
create table People
(
PeopleId int primary key identity(1,1),
PeopleName nvarchar(10) not null,
PeopleSex nvarchar(1) default('男') check(PeopleSex='男' or PeopleSex='女'),
PeopleBirth datetime not null,
PeopleSalary decimal(12,2) check(PeopleSalary>=1000 and PeopleSalary<=10000),
PeoplePhone varchar(20) unique not null,
PeopleAddress varchar(50),
PeopleAddTime smalldatetime default(getdate()),
DepartmentId int references Department(DepartmentId),
)
删除数据表
if exists(select * from sys.objects where name ='Department' and type='U')
drop table Department
|