欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文教會你在sqlserver中創(chuàng)建表

 更新時間:2022年04月12日 16:09:15   作者:波哥的技術(shù)積累  
在使用SQL server新建數(shù)據(jù)庫后,接下來我們就需要新建表了,下面這篇文章主要給大家介紹了關(guān)于在sqlserver中創(chuàng)建表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下

前言

SQL Server創(chuàng)建表是最常見也是最常用的操作之一,下面就為您介紹SQL Server創(chuàng)建表的語句寫法,供您參考,希望可以讓您對SQL Server創(chuàng)建表方面有更深的認(rèn)識。

方法如下:

1:在sql語句中,臨時表有兩類,分別是局部(local)和全局(global)臨時表,局部臨時表只在其會話(事務(wù))中可見,全局臨時表可以被會話(事務(wù))中的任何程序或者模塊訪問

2:創(chuàng)建局部臨時表

use db_sqlserver
go
create table #db_local_table
(
  id  int,
  name varchar(50),
  age int,
  area int
)

創(chuàng)建的臨時表不能與其他會話共享,當(dāng)會話結(jié)束時,行和表的定義都將被刪除

3:創(chuàng)建全局臨時表

use db_sqlserver
go
create table ##db_local_table
(
  id  int,
  name varchar(50),
  age int,
  area int
)

全局臨時表對所有用戶都是可見的,在每個訪問該表的用戶都斷開服務(wù)器連接時,全局臨時表才會被刪除

4:創(chuàng)建主鍵、外鍵關(guān)聯(lián)的數(shù)據(jù)庫表

use db_sqlserver;
go
create table db_table5
(
  職工編號 int primary key,
  職工號  varchar(50) unique,
  倉庫號  varchar(50),
  工資   int
)
 
go
create table db_table6
(
  訂單編號 int primary key,
  訂單號  varchar(50) unique,
  職工號 varchar(50) references db_table5(職工號),
  訂購日期 datetime,
  銷售金額 int
)

5:創(chuàng)建具有check約束字段的數(shù)據(jù)庫表

use db_sqlserver;
go
create table db_table7
(
  倉庫編號 int primary key,
  職工號  varchar(50) unique,
  倉庫號  varchar(50),
  工資   int,
  面積  int check(面積>=600 and 面積<=1800)
)

6:創(chuàng)建含有計算字段的數(shù)據(jù)庫表

use db_sqlserver;
go
create table db_table8
(
  職工編號 int primary key,
  職工號 varchar(50) unique,
  倉庫號 varchar(50),
  基本工資 int check(基本工資>=800 and 基本工資<=2100),
  加班工資 int,
  獎金 int,
  扣率 int,
  應(yīng)發(fā)工資 as (基本工資 + 加班工資 + 獎金 - 扣率)
)

7:創(chuàng)建含有自動編號字段的數(shù)據(jù)庫表

use db_sqlserver;
go
create table db_table9
(
   倉庫編號 int identity(1,1) primary key,
   倉庫號 varchar(50) unique,
   城市 varchar(50) default('青島'),
   面積 int check(面積>=300 and 面積<=1800)
)

向表中添加記錄:

 insert into [db_sqlserver].[dbo].[db_table9](倉庫號, 面積) values('400', 1600);

倉庫編號會自動增加

8:創(chuàng)建含有排序字段的數(shù)據(jù)表

create table db_table10 
(
   倉庫編號 int identity(1, 1) primary key,
   倉庫號 varchar(50) collate french_CI_AI not null,
   城市 varchar(50) default '青島',
   面積 int check(面積>=300 and 面積<=1800)
)

倉庫號是一個排序字段,其中CI(case insensitive)表示不區(qū)分大小寫,AI(accent insensitive)表示不區(qū)分重音,即創(chuàng)建的是一個不區(qū)分大小寫

和不區(qū)分重音的排序。如果要區(qū)分大小和和區(qū)分排序,修改代碼為:French_CS_AS

9:動態(tài)判斷數(shù)據(jù)庫表是否存在

use db_sqlserver;
go
if(Exists(select * from sys.sysobjects where id=OBJECT_ID('db_table9')))
  print '數(shù)據(jù)庫表名已經(jīng)存在'
  
else 
  print '該數(shù)據(jù)庫表名不存在,可以利用該名創(chuàng)建表'

10:查看表的各種信息,可以查看指定數(shù)據(jù)庫表的屬性、表中字段屬性、各種約束等信息

use db_sqlserver;
go
execute sp_help db_table9;

11:用select語句查看數(shù)據(jù)庫表的屬性信息

use db_sqlserver;
go
select * from sysobjects where type='U'

12:重命名數(shù)據(jù)庫表

use db_sqlserver;
go
execute sp_rename "db_table9", "db_renametable"

13:增加數(shù)據(jù)庫表的新字段 

use db_sqlserver;
go
alter table db_table1 add 電子郵件 varchar(50)
alter table db_table1 add 聯(lián)系方式 varchar(50) default '0532-88886396'
 
select name 字段名, xusertype 類型編號, length 長度 from syscolumns where id = object_id('db_table1')

14:修改數(shù)據(jù)庫表的字段

use db_sqlserver;
go
alter table db_table1 alter column 電子郵件 varchar(200)
 
 
select name 字段名, xusertype 類型編號, length 長度 from syscolumns where id = object_id('db_table1')

15:刪除數(shù)據(jù)庫表字段

use db_sqlserver;
go
alter table db_table1 drop column 電子郵件 
 
 
select name 字段名, xusertype 類型編號, length 長度 from syscolumns where id = object_id('db_table1')

16:刪除數(shù)據(jù)庫表

use db_sqlserver;
go
drop table db_table1
drop table db_table1, db_table2

如果刪除有依賴關(guān)聯(lián)的數(shù)據(jù)庫表,即主鍵、外鍵關(guān)鍵表、則要刪除兩個表之間的關(guān)聯(lián)約束,然后才能刪除表。注意,也可以先刪除引用該表的數(shù)據(jù)庫表,然后
即可刪除該表,

總結(jié)

到此這篇關(guān)于在sqlserver中創(chuàng)建表的文章就介紹到這了,更多相關(guān)sqlserver創(chuàng)建表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論