溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SqlServer系列筆記——表的創(chuàng)建維護(hù)

發(fā)布時(shí)間:2020-07-20 09:48:08 來源:網(wǎng)絡(luò) 閱讀:294 作者:codejson 欄目:數(shù)據(jù)庫


--創(chuàng)建表

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)

--刪除表的全部數(shù)據(jù),表還在

delete from table_name

DELETE FROM Person WHERE age> 20

--刪除數(shù)據(jù)還原標(biāo)識

truncate table table_name

--添加Insert


給可以給字段默認(rèn)值,如果Guid類型主鍵的默認(rèn)值設(shè)定為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’


更新一部分?jǐn)?shù)據(jù): UPDATE T_Person Set Age=30 where Name=‘tom’

------注意SQL中等于判斷用單個=,而不是==


--Where中還可以使用復(fù)雜的邏輯判斷UPDATE T_Person Set Age=30 where Name=‘tom’ or Age<25,

--or相當(dāng)于C#中的||(或者)

update Person1 set NickName=N'二十歲' 

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


--Where中可以使用的其他邏輯運(yùn)算符:or、and、not、<、>、>=、<=、!=(或<>)等


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI