温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-20 09:48:08 来源:网络 阅读:292 作者:codejson 栏目:数据库


--创建表

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),

     Name VarChar(10) NOT NULL,

     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、<、>、>=、<=、!=(或<>)等


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI