SQL Server知识点-2.相关语句总结
数据库类型:
时间类型:datetime
1.char和varchar的区别
char:定长;固定的长度 ,优化了性能,浪费了空间
varhar:变长;可变长度,优化了空间浪费了性能。
2.char和nchar的区别
char:单字节 char(2) 只能方一个中文
nchar:双字节 nchar(2) 可以放2个中文
varchar 4000个字节
nvarchar 8000个字节
text 可以写到亿级别以上
decimal 类似于float 小数类型
decimal(15,2) 长度为15,小数位占2位
bit 布尔类型 0和1 0为false 1为true
int,bigint 数字数据
truncate table 表名 删除数据行
执行效率更快,但有约束时不能用
drop 表名 删除表(整个)
数据库限制长度:check(len(列名)=11) len小写
1.建库建表语句
--使用系统数据库(有资格创建)
use master
--go 代表分批次执行
go
--判断要创建的数据库是否存在,若存在就删除,然后在重新创建
if exists(select * from sysdatabases where name='db_student')
drop database db_student
go
create database db_student
go
--使用数据库
use db_student
go
--创建表
if exists(select * from sysobjects where name='StuInfo')
drop table StuInfo
go
create table StuInfo(
stuId int primary key identity(20170901,1),--主键自增
stuName char(10) not null,--不为空
stuSex char(2) check(stuSex='男' or stuSex='女')--检查约束
)
go
if exists(select * from sysobjects where name='StuMarks')
drop table StuMarks
go
create table StuMarks(
ExamNo int