在一个SQL Server数据库中,可以创建多达两万亿个表
输入一条Create Table 语句 指定下列内容
1.包含表的数据库
2.表的所有者
3.表名,在同一个数据库中和同一个所有者下,改表名必须与任何其他基表或视图不同
4.指定1到1024个列
5.主键约束(可选)
6.1到250个Uniquer约束(可选)
7.1到253个外键约束(可选)
8.1个或者多个Check约束 ,限制插入表中的数据(可选)
9.存储表的文件组(可选)
--创建表*/
use databaseName
go
create Table tbName
(
tb_id int Not Null check(tb_id>0),
UserName varchar(50) NOT NULL CHECK(UserName<>'') ,
Sex int not Null Default 1 ,
price Money NOT NULL CHECK((price is NULL ) OR (price>=0)),
constraint tbPriKey Primary Key (tb_id)
) ----修改表--
--1.新增字段-
Alter Table tbName
add tbNewColumn int Null
--在为原来的表添加一条字段的时候需要注意的是 不允许指定该列为【 NOT NULL 】*/ ---2.删除字段------
Alter Table tbName drop column tbNewColumn ---3.修改字段---
Alter Table tbName Alter column tbNewColumn char(30) null ----4.新建约束-------
ALTER Table tbName ADD constraint tbNewRestrain check
yle="color:rgb(0,0,0);">(tb_id>0) -----5.删除约束---------
Alter Table tbName Drop constraint tbNewRestrain -------6.新建默认值--------
Alter Table tbName Add constraint tbNewDefault Default '10' for tb_id -------7.删除默认值----------
Alter Table tbName drop constraint tbNewDefault select * from tbName
use databaseName
go
create Table tbName
(
tb_id int Not Null check(tb_id>0),
UserName varchar(50) NOT NULL CHECK(UserName<>'') ,
Sex int not Null Default 1 ,
price Money NOT NULL CHECK((price is NULL ) OR (price>=0)),
constraint tbPriKey Primary Key (tb_id)
) ----修改表--
--1.新增字段-
Alter Table tbName
add tbNewColumn int Null
--在为原来的表添加一条字段的时候需要注意的是 不允许指定该列为【 NOT NULL 】*/ ---2.删除字段------
Alter Table tbName drop column tbNewColumn ---3.修改字段---
Alter Table tbName Alter column tbNewColumn char(30) null ----4.新建约束-------
ALTER Table tbName ADD constraint tbNewRestrain check
yle="color:rgb(0,0,0);">(tb_id>0) -----5.删除约束---------
Alter Table tbName Drop constraint tbNewRestrain -------6.新建默认值--------
Alter Table tbName Add constraint tbNewDefault Default '10' for tb_id -------7.删除默认值----------
Alter Table tbName drop constraint tbNewDefault select * from tbName
2.表约束
在我们创建表的时候,可以有选择的制定四种类型的约束:
1.主键
2.唯一性
3.外键
4.检查
create table student
(
s_id int identity(1,1) primary key,
s_name varchar(20) not null,
s_age int
) create table test
(
test_no int identity(1,1) primary key,
test_name varchar(30),
nax_marks int not null default(0),
min_marks int not null default(0)
)
create table marks
(
s_id int not null,
test_no int not null,
marks int not null default(0),
primary key(s_id,test_no),
foreign key(s_id) references student(s_id),
foreign key(test_no)
yle="color:rgb(0,0,255);">references test(test_no)
)
(
s_id int identity(1,1) primary key,
s_name varchar(20) not null,
s_age int
) create table test
(
test_no int identity(1,1) primary key,
test_name varchar(30),
nax_marks int not null default(0),
min_marks int not null default(0)
)
create table marks
(
s_id int not null,
test_no int not null,
marks int not null default(0),
primary key(s_id,test_no),
foreign key(s_id) references student(s_id),
foreign key(test_no)
yle="color:rgb(0,0,255);">references test(test_no)
)
3. 索引以及视图的创建
-----视图的建立-------
create view 视图名
(
字段1,
字段2,
..
)
as select a.字段1 ,a.字段2,
.. from tableName as a where
------索引的创建--------
create index indexName
on TableName
(字段1,字段2,字段3)
create view 视图名
(
字段1,
字段2,
..)
as select a.字段1 ,a.字段2,
.. from tableName as a where
------索引的创建--------create index indexName
on TableName
(字段1,字段2,字段3)
本文作者:Samjoe Yang
本文链接: https://need.uno/needisme-2009-12-25-t-sql-e5889be5bbbae8a1a8/
版权声明:本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论