加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SqlServer系列笔记――表的创建维护

发布时间:2020-12-12 13:23:32 所属栏目:MsSql教程 来源:网络整理
导读:--创建表 create table Employees ( ? ? ?EmployeeID Int primary key, ? ? ?Name VarChar(10) NOT NULL, ? ? ?Sex Char(2) default '男', ? ? ?Birthdate Datetime NULL, ? ? ?Address Varchar(50) NULL, ? ? ?Phone Char(13) check (phone like '000-[0_9]


--创建表

create table Employees

(

? ? ?EmployeeID Int primary key,

? ? ?Name VarChar(10) NOT NULL,

? ? ?Sex Char(2) default '男',

? ? ?Birthdate Datetime NULL,

? ? ?Address Varchar(50) NULL,

? ? ?Phone Char(13) check (phone like '000-[0_9]'),

? ? ?Remark text

)

create table wage

(

? ? ?EmployeeID Int foreign key references Employees(EmployeeID),

? ? ?Wage money NOT NULL,

? ? ?Putdate Datetime NOT NULL,

)

--添加主键约束

alter table Employees

add constraint Employees_PK ?primary key ?(EmployeeID)

--添加外键约束

alter table wage?

add constraint wage_FK foreign key (EmployeeID) references Employees(EmployeeID)

--删除约束

alter table wage

drop constraint wage_FK

--添加default约束

alter table Employees

add constraint a default ('unknown') for name,

constraint b default ('男') for sex,

?constraint ? phone_check check(phone like '(d{3})d{9}')

--删除列

alter table Employees

drop column Remark?

--添加列

alter table Employees

add Remark text,

phone varchar(10)

--删除表的全部数据,表还在

delete from table_name

DELETE FROM Person WHERE age> 20

--删除数据还原标识

truncate table table_name

--添加Insert


给可以给字段默认值,如果Guid类型主键的默认值设定为newid()就会自动生成主键:

? ? ?insert into Person3(Name,Age) values('lili',38);

??

? ?insert into Person(Id,Name,Age) values(newid(),'tom',30);

--更新Update

更新一个列:UPDATE T_Person Set Age=30


更新多个列:UPDATE T_Person Set Age=30,Name=‘tom’


更新一部分数据: UPDATE T_Person Set Age=30 where Name=‘tom’

------注意SQL中等于判断用单个=,而不是==


--Where中还可以使用复杂的逻辑判断UPDATE T_Person Set Age=30 where Name=‘tom’ or Age<25,

--or相当于C#中的||(或者)

update Person1 set NickName=N'二十岁'?

where (Age>20 and Age<30) or(Age=80)


--Where中可以使用的其他逻辑运算符:or、and、not、<、>、>=、<=、!=(或<>)等

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读