基本数据类型按照数据的表现方式及存储方式的不同可以分为:
用户自定义类型并不是真的数据类型,知识提供一种加强数据内部元素和基本数据类型之间的一致性的机制。通过使用用户自定义是英语话剧能够简化对常用规则和默认值的管理。
在SQL Server2012中使用数据类型sp_addtype创建用户自定义数据类型。
sp_addtype[@typename=]type,
[@phystype=]system_data_type
[,[@nulltyoe=]'null_type']
[,[@owner=]'owner_name']
在SPJ数据库中,创建用来存储邮政编码的信息的postcode用户自定义数据类型。SQL语句如下:
use SPJ
exec sp_addtype
postcode, 'char(8)', not null
数据完整性:指列中每个事件都有正确的数据值。数据值的数据类型必须正确,并且数据值必须唯一正确的域中。
引用完整形:指示表之间的关系得到正确维护。一个表中的数据只应只想宁一个表中的享有行,不应指向不存在的行。
SQL Server 2012中多种强制性数据完整机制如下:
create table
[database_name . [schema_name] . |schema_name . ] table_name
(
{||}
[]
)
::=column_name [FILESTREAM ][COLLATE collation_name][NULL | NOT NULL]
使用create table语句创建数据表student,ID字段为int类型并且不允许为空;Name字段长度为50的varchar类型;Age字段为int类型。
use STC -- 打开数据库
crtea table [STC].[dbo].student -- 创建表
(
ID int not null,
Name varchar(50),
Age int
)
alter table [[database_name].[schema_name].|schema_name .] table_name
{
alter column column_name
{
[type_schema_name.] type_name [({precision [,scale]|max|xml_schema_colllection})]
[colleate collate_name][NULL | NOT NULL]
|{rowguidcol | presisted | not for replication | sparse}
}
|[with {Check | NOCheck}]
|add {|||}
|drop {[constraint] constraint_name [with ()]
|column column_name
}
}
向数据库STC中的student表中添加Sex字段。
use STC -- 使用数据库
alter table student -- 更改表
add Sex char(2) -- 添加Sex字段
删除student表中的Sex字段。
use STC
alter tbale student
drop column Sex
drop table [database_name].[schema_name].table_name[......,n]
删除STC数据库中的student表。
use STC
drop table student
insert语句可以实现向表中添加新纪录的操作。该语句可以向表中插入一条新纪录或插入一个结果集。
insert [into] table_or_view_name
V(eAxLpUreEsSsion) [,...n]
利用insert语句向数据表student添加数据记录。
use STC
insert into student(ID, Name, Age)
values(123, '李四', 15)
如果要向表中添加所有的字段,可以省略要插入的数据的列名。
use STC
insert into student
values(123, '李四', 15)
修改表中不符合要求的数据或错误的字段时,可以使用update语句进行修改。
update table_or_view_name
[from{}[,...n]]
set
{
column_name = {expression | default | BULL}
[where
将student表中所有学生的年龄加两岁。
use STC
update student
set Age = Age + 2
将student表中“李四”的性别修改为女。
use STC
update student
set Sex = '女'
where Name = '李四'
delete语句用于从表中或视图中删除行。
delete
[from ][,...n]]
[where {}]
删除student表中ID为123的学生的信息。
use STC
delete from student
where ID = 123
可以使用create table创建表时,使用NOT NULL关键字指定非空约束。
[constraint <约束名>] NOT NULL
alter table table_name
alter column column_name
column_type NUll | NOT NULL
修改student表中的非空约束。
use STC -- 使用数据库
alter table student 更改表
alterr column ID int NULL -- 更改ID字段属性
可以通过定义primary key约束来创建主键,用于强制表的实体完整性。
创建数据表student并将字段ID设置为主键约束。
use STC -- 使用数据库
create table [STC].[dbo].student -- 创建数据表
(
ID int constraint P_ID primary key, -- ID字段,设为主键约束
Name char(50),
Sex char(2),
Age int
)
use STC
alter table student
add constraint constraint_name primary key
[clustered | nonclustered]{(column[,...n])}
将student表中的ID字段指定为主键约束。
use STC
alter constraint P_ID primary key(id) -- 对ID字段添加主键约束
若要修改primary key约束,必须先删除现有的primary key约束,然后重新定义主键约束。
alter table tble_name
drop constraint constraint_name[,...n]
删除student表中的主键约束
use STC
alter tbale student
drop constraint P_ID -- 删除主键约束
唯一约束UNIQUE用于强制实施列集中中值的唯一性。
在STC数据库中创建数据表student,并将字段ID设置唯一性约束。
use STC
create table [dbo].student
(
ID int constraint UNIQ_ID unique, --设置唯一性约束
Name cher(50),
Sex char(2),
Age int
)
alter table table_name
add constraint constraint_name unique(ID)
将student表中的ID字段指定设置为唯一约束。
use STC
alter table student
add constraint UNIQ_ID unique(ID)
若要修改UNIQUE约束,必须先删除现的UNIQUE约束,然后用新定义重新创建
alter table table_name
drop constraint constraint_name[,...n]
删除student表中的唯一约束。
use STC
alter table student
drop constraint UNIQ_ID
检查约束CHECK可以强制域的完整性
创建数据表student并将字段Sex设置检查约束,在输入性别字段时,只能接受“男”或者“女”,而不能接受其他数据。
use STC
create table [STC].[dbo].student
(
ID int,
Name char(50),
Sex char(2) copnstraint CK_SEX check(sex in ('男', '女')),
Age int
)
alter table table_name
add constraint constraint_name check(logic_expression)
为student表中的Sex字段设置检查约束,在输入性别时只能接受”女“或者”男“,不能解接受其他字段。
use STC
alter table student
add constraint CK_SEX check(Sex in ('男', '女'))
修改表中某列的CKECK约束使用的表达式,必须首先删除现有的CHECK约束,然后使用新定义重新创建,才能修改CHECK约束。
alter table table_name
drop constraint constraint_name[,...n]
在创建或修改表时可通过定义默认约束DEFAULT来创建默认值。
创建数据表student并将字段Sex设置默认约束”女“。
use STC
create table [STC].[dbo].student
(
ID int,
Name char(50),
Sex char(2) constraint D_SEX default '女',
Age int
)
alter table table_name
add constraint constraint_name default constant_expression [for column_name]
为student表中的Sex字段设置默认约束为‘男’。
use STC
alter table student
add constraint D_SEX defaulr '男' for Sex
修改表中某列的DEFAULT约束使用的表达式,必须首先删除现有的DEFAULT约束,然后使用新定义重新创建,才能修改DEFAULT约束。
alter table table_name
drop constraint constraint_name[,...n]
删除student表中的默认约束
use STC
alter table student
drop constraint D_SEX
通过定义FOREIGN KEY约束来创建外键。
创建表course,并为course表创建外键约束,该约束把course中的百年好ID字段和表student中的编号ID字段关联起来,实现course中的编号ID字段的取值参照student中的编号ID字段的数据值。
use STC
create table [STC].[dbo].course
(
ID int constraint F_ID foreign key references student(ID)
Name char(50),
time datetime
)
alter table table_name
add constraint constraint_name [foreign key]{(column_name[,...n])}
references ref_table [ref_column_name[,...n]]
将student表中的ID字段设置为course表中的外键。
use STC
alter table course
add constraint F_ID foreign key(ID) references student(ID)
修改表中某列的FOREIGN KEY约束使用的表达式,必须首先删除现有的FOREIGN约束,然后使用新定义重新创建,才能修改FOREIGN KEY约束。
alter table table_name
drop constraint constraint_name[,...n]
删除course表中的外键约束
use STC
alter table course
drop constraint F_ID