创 库,表,约束
- SQL Create DB
- create database test
- SQL Create Table
- create table 表名(字段名1 字段类型1,字段名2 字段类型2,,,);
- SQL Unique
- alter table 表名 add UNIQUE(列名,);
- SQL Primary Key
- alter table 表名 PRIMARY key(列名,);
- SQL Foreign Key
- alter table 表名 add foreign key(列名) references 主表名(主表列)
- SQL Check
- alter table 表 add check (列 比较符 列的范围)
- SQL Default
- alter table 表名 add default ‘值’ for 列名
- SQL Alter
- alter table test 需要做的事情
- SQL View
- create view 视图名字 as select 列 from 表
- SQL Nulls
- is null与not null
- SQL isnull()
- 微软的 ISNULL() 函数用于规定如何处理 NULL 值。
- SQL 数据类型
hdbejf
- SQL Create DB
1. 语法:create database test
2. 创建数据库
--案例一:
create database test1
--当然上面是创建一个最简单的数据库 还可以在后面加上 on 然后可以自定义属性与值
--案例二:
create database text2 on primary -- 默认就属于primary文件组,可省略
(
--数据文件的具体描述--
name='stuDB_data', -- 主数据文件的逻辑名称
filename='E:\DBA\stuDB_data.mdf', -- 主数据文件的物理名称
size=5mb, --主数据文件的初始大小
maxsize=100mb, -- 主数据文件增长的最大值
filegrowth=15%--主数据文件的增长率
)
log on
(
--日志文件的具体描述,各参数含义同上--
name='stuDB_log',
filename='E:\DBA\stuDB_log.ldf',
size=2mb,
filegrowth=1mb
)
- SQL Create Table
1. 语法:create table 表名(字段名1 字段类型1,字段名2 字段类型2,字段名3 字段类型3,);
2. 创建表结构
--这是创建了一个最简单的表结构
--案例:
create table test2(
id int ,
name VARCHAR(20)
)
--PS:上面创建表的字段都是默认为空的 如果不默认的话使用not null语句,如下
create table test2(
id int not null,
name VARCHAR(20) not null
)
--标识列
create table a(
aId int not null primary key identity(1,1) ,
aName nvarchar(50) not null
);
--ps:aid设置主键与标识列从1开始每次增长1
- SQL Unique唯一约束
1. 语法:alter table 表名 add UNIQUE(列名,);
2. 为列创建唯一约束
--添加一个唯一约束
alter table users add UNIQUE(absj);
--添加一个自定义名称的唯一约束
alter table users add CONSTRAINT CH_uid UNIQUE(uid)
--创建表时把约束也给加上去
create table test(
tid int not null UNIQUE,
tname int not null
)
--删除约束
alter table users drop CH_uid
- SQL Primary Key
1. 语法:alter table 表名 add UNIQUE(列名,);
2. 设置主键
--添加一个主键
alter table users add PRIMARY key(absj);
--添加一个自定义名称的主键
alter table users add CONSTRAINT users_key primary key(absj)
--创建表时直接把主键添加上去
create table test(
tid int not null,
tname int not null,
primary key(tid)
)
--删除主键
alter table users drop users_key;
- SQL Foreign Key
1. 语法:alter table 表名 add foreign key(列名) references 主表名(主表列)
2. 设置俩张表的关系(主外键)
--默认名称
alter table student add foreign key(gradeID) references grade(gradeID)
--自定义名称
alter table student add CONSTRAINT FK__student__gradeId__6754599E foreign key(gradeID) references grade(gradeID)
--建表时设置主键
create table test1(
tid int not null,
tname int not null,
foreign key(tid) references text2(t_id)
)
--删除主键
alter table student drop FK__student__gradeId__6754599E
- SQL Check
1. 语法:alter table 表 add check (列 比较符 列的范围)
2. 检查约束
--默认名称
alter table test add check (tid > 0 and tname>20)
--自定义名称
alter table test add constraint CH_test_tid check (tid>0)
--建表时添加
create table test(
tid int not null,
tname varchar not null,
check (tid >10 )
);
--删除约束
alter table test drop CK__test__tid__7E37BEF6
- SQL Default
1. 语法:alter table 表名 add default ‘值’ for 列名
2. PS:为该表的该列添加默认值
--默认名称
alter table test add default '123456' for tname
--自定义名称
alter table test add CONSTRAINT tid_default default '123456' for tid
--创建表时
create table test(
tid int not null,
tname varchar(20) not null default 'hyasd'
);
--删除
alter table test drop constraint tid_default
- SQL Drop删除
1. 可以撤销索引,表,数据库
--索引
DROP INDEX table_name.index_name
--表
DROP TABLE test
--数据库
Drop database kzdba
--SQL Alter
--PS:修改表类型,添加,删除
--添加列
alter table test add tage nvarchar not null
--删除
alter table test drop COLUMN tage
--修改类型
alter table test alter column tage int not null
- SQL View
1. 语法:create view 视图名字 as select 列 from 表
2. 为一张表的某些字段(全部字段)创建一张视图,如果要是修改了视图的列那么相对应的表的列也会更改,简单来说一章视图和一直真实的表差不多
create view k_x as select studentNo,studentName from student
select * from k_x
--删除视图
drop view k_x
- SQL Null
1. 一般用于添加,建表与where子句中
--添加时
insert into test(tid,tname) values('1',null);
--建表时
create table test11(
t_id int not null,--不允许为空
t_name nvarchar(20) null--允许为空(default)
);
--where子句中时
select * from test where tname is null --tname为null的
select * from test where tname is not null--tname不为null的
--PS:null与0不等价
- SQL isnull()
1. 微软的 ISNULL() 函数用于规定如何处理 NULL 值。
2. isnull(列名,变成的值) 如果该列下的值有为null的值的话那么isnull就返回第二个参数
select * from test where ISNULL(tage,0) = 0
- SQL 数据类型
Character 字符串:
数据类型 | 描述 | 存储 |
---|---|---|
char(n) | 固定长度的字符串。 | 最多 8,000 个字符。 |
varchar(n) | 可变长度的字符串。 | 最多 8,000 个字符。 |
text | 可变长度的字符串。 | 最多 2GB 字符数据。 |
Unicode 字符串:
数据类型 | 描述 | 存储 |
---|---|---|
nchar(n) | 固定长度的 Unicode 数据。 | 最多 4,000 个字符。 |
nvarchar(n) | 可变长度的 Unicode 数据。 | 最多 4,000 个字符。 |
ntext | 可变长度的 Unicode 数据。 | 最多 2GB 字符数据。 |
Binary 类型:
数据类型 | 描述 | 存储 |
---|---|---|
bit | 允许 0、1 或 NULL | |
binary(n) | 固定长度的二进制数据。 | 最多 8,000 字节。 |
varbinary(n) | 可变长度的二进制数据。 | 最多 8,000 字节。 |
image | 可变长度的二进制数据。 | 最多 2GB。 |
Number 类型:
数据类型 | 描述 | 存储 |
---|---|---|
tinyint | 允许从 0 到 255 的所有数字。 | 1 字节 |
smallint | 允许从 -32,768 到 32,767 的所有数字。 | 2 字节 |
int | 允许从 -2,147,483,648 到 2,147,483,647 的所有数字。 | 4 字节 |
bigint | 允许介于 -9,223,372,036,854,775,808 和 9,223,372,036,854,775,807 之间的所有数字。 | 8 字节 |
decimal(p,s) | 固定精度和比例的数字。允许从 -10^38 +1 到 10^38 -1 之间的数字。 p 参数指示可以存储的最大位数(小数点左侧和右侧)。p 必须是 1 到 38 之间的值。默认是 18。s 参数指示小数点右侧存储的最大位数。s 必须是 0 到 p 之间的值。默认是 0。 | 5-17 字节 |
numeric(p,s) | 固定精度和比例的数字。允许从 -10^38 +1 到 10^38 -1 之间的数字。p 参数指示可以存储的最大位数(小数点左侧和右侧)。p 必须是 1 到 38 之间的值。默认是 18。s 参数指示小数点右侧存储的最大位数。s 必须是 0 到 p 之间的值。默认是 0。 | 5-17 字节 |
smallmoney | 介于 -214,748.3648 和 214,748.3647 之间的货币数据。 | 4 字节 |
money | 介于 -922,337,203,685,477.5808 和 922,337,203,685,477.5807 之间的货币数据。 | 8 字节 |
float(n) | 从 -1.79E + 308 到 1.79E + 308 的浮动精度数字数据。 参数 n 指示该字段保存 4 字节还是 8 字节。float(24) 保存 4 字节,而 float(53) 保存 8 字节。n 的默认值是 53。 | 4 或 8 字节 |
real | 从 -3.40E + 38 到 3.40E + 38 的浮动精度数字数据。 | 4 字节 |
Date 类型:
数据类型 | 描述 | 存储 |
---|---|---|
datetime | 从 1753 年 1 月 1 日 到 9999 年 12 月 31 日,精度为 3.33 毫秒。 | 8 bytes |
datetime2 | 从 1753 年 1 月 1 日 到 9999 年 12 月 31 日,精度为 100 纳秒。 | 6-8 bytes |
smalldatetime | 从 1900 年 1 月 1 日 到 2079 年 6 月 6 日,精度为 1 分钟。 | 4 bytes |
date | 仅存储日期。从 0001 年 1 月 1 日 到 9999 年 12 月 31 日。 | 3 bytes |
time | 仅存储时间。精度为 100 纳秒。 | 3-5 bytes |
datetimeoffset | 与 datetime2 相同,外加时区偏移。 | 8-10 bytes |
timestamp | 存储唯一的数字,每当创建或修改某行时,该数字会更新。timestamp 基于内部时钟,不对应真实时间。每个表只能有一个 timestamp 变量。 |
其他数据类型:
数据类型 | 描述 |
---|---|
sql_variant | 存储最多 8,000 字节不同数据类型的数据,除了 text、ntext 以及 timestamp。 |
uniqueidentifier | 存储全局标识符 (GUID)。 |
xml | 存储 XML 格式化数据。最多 2GB。 |
cursor | 存储对用于数据库操作的指针的引用。 |
table | 存储结果集,供稍后处理。 |