国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【SQL Server】數(shù)據(jù)庫開發(fā)指南(二)MSSQL數(shù)據(jù)庫開發(fā)對于庫、表、數(shù)據(jù)類型、約束等相關(guān)操作

這篇具有很好參考價值的文章主要介紹了【SQL Server】數(shù)據(jù)庫開發(fā)指南(二)MSSQL數(shù)據(jù)庫開發(fā)對于庫、表、數(shù)據(jù)類型、約束等相關(guān)操作。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

本系列博文還在更新中,收錄在專欄:#MS-SQL Server 專欄中。

本系列文章列表如下:

【SQL Server】 Linux 運維下對 SQL Server 進行安裝、升級、回滾、卸載操作
【SQL Server】數(shù)據(jù)庫開發(fā)指南(一)數(shù)據(jù)庫設(shè)計的核心概念和基本步驟
【SQL Server】數(shù)據(jù)庫開發(fā)指南(二)MSSQL數(shù)據(jù)庫開發(fā)對于庫、表、數(shù)據(jù)類型、約束等相關(guān)操作
【SQL Server】數(shù)據(jù)庫開發(fā)指南(三)面向數(shù)據(jù)分析的 T-SQL 編程技巧與實踐
[ 云原生 | Docker ] 構(gòu)建高可用性的 SQL Server:Docker 容器下的主從同步實現(xiàn)指南
【SQL Server】數(shù)據(jù)庫開發(fā)指南(五)T-SQL 高級查詢綜合應(yīng)用與實戰(zhàn)
【SQL Server】數(shù)據(jù)庫開發(fā)指南(六)索引和視圖的使用技巧、方法與綜合應(yīng)用
【SQL Server】數(shù)據(jù)庫開發(fā)指南(七)MS-SQL存儲過程全面解析:種類、優(yōu)點和創(chuàng)建方法詳解
【SQL Server】數(shù)據(jù)庫開發(fā)指南(八)高級數(shù)據(jù)處理技術(shù) MS-SQL 事務(wù)、異常和游標的深入研究
【SQL Server】數(shù)據(jù)庫開發(fā)指南(九)詳細講解 MS-SQL 觸發(fā)器的基礎(chǔ)概念與應(yīng)用場景

一、SQL Server 中的 GO 關(guān)鍵字

GO 是 T-SQL 中的一種分隔符,用于將批處理中的 T-SQL 語句分成多個批次。每個批次在 GO 后面執(zhí)行。GO 通常被用于以下情況:

  • 在一個腳本中,用于分隔多個 SQL 語句,因為 SQL Server 需要知道每個語句的結(jié)束位置。
  • 在一個事務(wù)中,用于提交或回滾一個批處理的事務(wù)。
    GO 并不是 T-SQL 的語句或命令,而是由 SQL Server 客戶端應(yīng)用程序識別的一個指令。

以下是一個使用 GO 分隔批處理的示例:

-- 創(chuàng)建新的表
create table Employee (
   ID INT PRIMARY KEY,
   FirstName VARCHAR(50),
   LastName VARCHAR(50),
   Age INT
)
GO

-- 向表中插入數(shù)據(jù)
insert into Employee values (1, 'John', 'Doe', 25)
insert into Employee values (2, 'Jane', 'Doe', 30)
insert into Employee values (3, 'Bob', 'Smith', 40)
GO

-- 查詢表中數(shù)據(jù)
select * from Employee

注意:GO 必須單獨一行,不能和其他語句或命令在同一行。另外,GO 后面可以指定一個可選的參數(shù),表示要執(zhí)行多少次批處理。例如,GO 5 表示要執(zhí)行當前批處理 5 次。但是這個功能只能在某些客戶端應(yīng)用程序中使用,而在 SQL Server Management Studio 中不支持。

GO 是用于將 T-SQL 批處理分成多個批次的分隔符,通常在一個腳本中或一個事務(wù)中使用。

二、切換不同數(shù)據(jù)庫

SQL Server 和其他數(shù)據(jù)庫一樣,都是使用 use 關(guān)鍵字,即可切換成所需要使用的數(shù)據(jù)庫。

use master
go

三、創(chuàng)建、刪除數(shù)據(jù)庫

3.1 創(chuàng)建方式1:基本創(chuàng)建(適合演示和學(xué)習(xí))

--判斷是否存在該數(shù)據(jù)庫,存在就刪除
if (exists (select * from sys.databases where name = 'demoTest'))
    drop database demoTest
go
--創(chuàng)建數(shù)據(jù)庫,設(shè)置數(shù)據(jù)庫文件、日志文件保存目錄
create database demoTest
on(
    name = 'demoTest',
    filename = 'c:\data\demoTest.mdf'    
)
log on(
    name = 'demoTest_log',
    filename = 'c:\data\demoTest_log.ldf'
)
go

3.2 創(chuàng)建方式2:設(shè)置存儲位置以及大小等

if (exists (select * from sys.databases where name = 'demoTest'))
    drop database demoTest
go
create database demoTest
--默認就屬于primary主文件組,可省略
on primary (    
    --數(shù)據(jù)文件的具體描述
    name = 'demoTest_data',                --主數(shù)據(jù)文件的邏輯名
    fileName = 'c:\demoTest_data.mdf',     --主數(shù)據(jù)文件的物理名
    size = 5MB,                            --主數(shù)據(jù)文件的初始大小
    maxSize = 50MB,                        --主數(shù)據(jù)文件增長的最大值
    fileGrowth = 15%                       --主數(shù)據(jù)文件的增長率
)
--日志文件的具體描述,各參數(shù)含義同上
log on (
    name = 'demoTest_log',
    fileName = 'c:\demoTest_log.ldf',
    size = 1MB,
    fileGrowth = 1MB
)
go

注意 上述 T-SQL 中, size 與 maxSize 都是以 MB 為單位,fileGrowth 可以以 MB 為單位,也可以以百分比為單位。

3.2 創(chuàng)建方式3:同時設(shè)置主與次數(shù)據(jù)文件信息

在 SQL Server 中,可以在創(chuàng)建數(shù)據(jù)庫時同時設(shè)置數(shù)據(jù)文件和數(shù)據(jù)文件。主數(shù)據(jù)文件是數(shù)據(jù)庫的主要數(shù)據(jù)文件,包含數(shù)據(jù)庫的系統(tǒng)表和用戶數(shù)據(jù)。次數(shù)據(jù)文件是包含用戶數(shù)據(jù)的文件,可以幫助擴展數(shù)據(jù)庫的存儲容量并提高性能。

if (exists (select * from sys.databases where name = 'demoTest'))
    drop database demoTest
go
create database demoTest
--默認就屬于 primary 主文件組,可省略
on primary (    
    --數(shù)據(jù)文件的具體描述
    name = 'demoTest_data',                --主數(shù)據(jù)文件的邏輯名
    fileName = 'c:\demoTest_data.mdf',     --主數(shù)據(jù)文件的物理名
    size = 5MB,                            --主數(shù)據(jù)文件的初始大小
    maxSize = 50MB,                        --主數(shù)據(jù)文件增長的最大值
    fileGrowth = 15%                       --主數(shù)據(jù)文件的增長率
),
--次數(shù)據(jù)文件的具體描述
(    
    --數(shù)據(jù)文件的具體描述
    name = 'demoTest2_data',               --主數(shù)據(jù)文件的邏輯名
    fileName = 'c:\demoTest2_data.mdf',    --主數(shù)據(jù)文件的物理名
    size = 2MB,                            --主數(shù)據(jù)文件的初始大小
    maxSize = 50MB,                        --主數(shù)據(jù)文件增長的最大值
    fileGrowth = 10%                       --主數(shù)據(jù)文件的增長率
)
--日志文件的具體描述,各參數(shù)含義同上
log on (
    name = 'demoTest_log',
    fileName = 'c:\demoTest_log.ldf',
    size = 1MB,
    fileGrowth = 1MB
),
(
    name = 'demoTest2_log',
    fileName = 'c:\demoTest2_log.ldf',
    size = 1MB,
    fileGrowth = 1MB
)
go

五、SQL Server 的基本數(shù)據(jù)類型

5.1 精確數(shù)字類型

在 SQL Server 中,有幾種精確數(shù)字類型可用于存儲精確數(shù)字值。這些類型的特點是可以精確地存儲小數(shù)點前后的數(shù)字,并避免精度丟失問題。以下是 SQL Server 中可用的精確數(shù)字類型:

類型 描述
tinyint 用于存儲非負整數(shù)值,范圍為 0 到 255。該類型存儲為 1 字節(jié)的整數(shù)。
smallint 用于存儲較小的整數(shù)值,范圍為 -32,768 到 +32,767。該類型存儲為 2 字節(jié)的整數(shù)。
int 用于存儲整數(shù)值,范圍為 -2^31(-2,147,483,648)到 2^31-1(+2,147,483,647)。該類型存儲為 4 字節(jié)的整數(shù)。
bigint 用于存儲大整數(shù)值,范圍為 -2^63(-9,223,372,036,854,775,808)到 2^63-1(+9,223,372,036,854,775,807)。該類型存儲為 8 字節(jié)的整數(shù)。
bit 可以取值為 1、0 或 NULL 的整數(shù)數(shù)據(jù)類型,每8個bit占一個字節(jié),16bit就2個字節(jié),24bit就3個字節(jié)
decimal 用于存儲精確的定點數(shù)值,例如貨幣金額。DECIMAL/NUMERIC 數(shù)據(jù)類型包含兩個參數(shù):精度和小數(shù)位數(shù)。例如,有效值從 - 10^38 +1 到 10^38 - 1。DECIMAL(10,2) 表示具有最大總位數(shù)為 10 位,其中有 2 位小數(shù)。
numeric decimal 相同
money 用于存儲貨幣值,范圍為 -2^63(-922,337,203,685,477.5808)到 2^63-1(+922,337,203,685,477.5807)。該類型存儲為 8 字節(jié)的定點數(shù)。
smallmoney 用于存儲較小的貨幣值,范圍為 -214,748.3648 到 +214,748.3647。該類型存儲為 4 字節(jié)的定點數(shù)。

5.2 近似數(shù)字類型

除了精確數(shù)字類型之外,SQL Server 還提供了近似數(shù)字類型,可以存儲小數(shù)點后面一定位數(shù)的數(shù)字,但是不保證完全準確,因為存儲的值會被截斷或四舍五入。以下是 SQL Server 中的一些近似數(shù)字類型:

類型 描述
float 表示浮點數(shù)值數(shù)據(jù)的大致數(shù)值數(shù)據(jù)類型。浮點數(shù)據(jù)為近似值;范圍-1.79E + 308 至 -2.23E - 308、0 以及 2.23E - 308 至 1.79E + 308
real real 的 SQL-92 同義詞為 float(24),范圍在-3.40E + 38 至 -1.18E - 38、0 以及 1.18E - 38 至 3.40E + 38

5.3 日期時間類型

類型 描述
datetime 表示某天的日期和時間的數(shù)據(jù)類型,范圍在1753 年 1 月 1 日到 9999 年 12 月 31 日
smalldatetime 范圍在1900 年 1 月 1 日到 2079 年 6 月 6 日

5.5 字符串類型

類型 描述
char 固定長度或可變長度的字符數(shù)據(jù)類型,范圍在范圍為 1 至 8,000字節(jié)
text 最大長度為 2^31-1
varchar 固定長度或可變長度的字符數(shù)據(jù)類型,最大存儲大小是 2^31-1 個字節(jié)

5.6 Unicode字符串類型

類型 描述
nchar 字符數(shù)據(jù)類型,長度固定,在必須在 1 到 4,000 之間
nvarchar 可變長度 Unicode 字符數(shù)據(jù)。最大存儲大小為 2^31-1 字節(jié)
ntext 長度可變的 Unicode 數(shù)據(jù),最大長度為 2^30 - 1 (1,073,741,823) 個字符

5.7 二進制字符串類型

類型 描述
binary 長度為 n 字節(jié)的固定長度二進制數(shù)據(jù),范圍從 1 到 8,000 的值。存儲大小為 n 字節(jié)。
varbinary 可變長度二進制數(shù)據(jù)。n 可以取從 1 到 8,000 的值。最大的存儲大小為 2^31-1 字節(jié)
image 長度可變的二進制數(shù)據(jù),從 0 到 2^31-1 (2,147,483,647) 個字節(jié)

六、SQL Server 判斷表或其他對象及列是否存在

6.1 判斷某個表或?qū)ο笫欠翊嬖?/h4>
--判斷某個表或?qū)ο笫欠翊嬖?/span>
if (exists (select * from sys.objects where name = 'username'))
    print '存在';
go
if (exists (select * from sys.objects where object_id = object_id('student')))
    print '存在';
go
if (object_id('student', 'U') is not null)
    print '存在';
go

6.2 判斷該列名是否存在,如果存在就刪除

--判斷該列名是否存在,如果存在就刪除
if (exists (select * from sys.columns where object_id = object_id('student') and name = 'idCard'))
    alter table student drop column idCard
go
if (exists (select * from information_schema.columns where table_name = 'student' and column_name = 'tel'))
    alter table student drop column tel
go

七、SQL Server 中創(chuàng)建、刪除表

--判斷是否存在當前table
if (exists (select * from sys.objects where name = 'classes'))
    drop table classes
go
create table classes(
    id int primary key identity(1, 2),
    name varchar(22) not null,
    createDate datetime default getDate()
)
go
if (exists (select * from sys.objects where object_id = object_id('student')))
    drop table student
go
--創(chuàng)建table
create table student(
    id int identity(1, 1) not null,
    name varchar(20),
    age int,
    sex bit,
    cid int
)
go

八、SQL Server 中修改的表

8.1 給表添加字段、修改字段、刪除字段

--添加字段
alter table student add address varchar(50) not null;
--修改字段
alter table student alter column address varchar(20);
--刪除字段
alter table student drop column number;
 
--添加多個字段
alter table student 
add address varchar(22),
    tel varchar(11),
    idCard varchar(3);
 
--判斷該列名是否存在,如果存在就刪除
if (exists (select * from sys.columns where object_id = object_id('student') and name = 'idCard'))
    alter table student drop column idCard
go
if (exists (select * from information_schema.columns where table_name = 'student' and column_name = 'tel'))
    alter table student drop column tel
go

8.2 給表添加、刪除約束

--添加新列、約束
alter table student 
    add number varchar(20) null constraint no_uk unique;  
--增加主鍵
alter table student  
    add constraint pk_id primary key(id);  
--添加外鍵約束
alter table student
    add constraint fk_cid foreign key (cid) references classes(id)
go
--添加唯一約束
alter table student
    add constraint name_uk unique(name);
--添加check約束
alter table student with nocheck   
    add constraint check_age check (age > 1);
alter table student
    add constraint ck_age check (age >= 15 and age <= 50)
--添加默認約束
alter table student
    add constraint sex_def default 1 for sex;
--添加一個包含默認值可以為空的列
alter table student 
    add createDate smalldatetime null
    constraint createDate_def default getDate() with values;
 
----- 多個列、約束一起創(chuàng)建--------
alter table student add   
    /*添加id主鍵、自增*/  
    id int identity constraint id primary key,   
    /* 添加外鍵約束*/   
    number int null    
    constraint uNumber references classes(number),  
    /*默認約束*/  
    createDate decimal(3, 3)  
    constraint createDate default 2010-6-1  
go 
 
--刪除約束
alter table student  drop constraint no_uk;

九、SQL Server 中操作數(shù)據(jù)

9.1 插入數(shù)據(jù)

插入數(shù)據(jù)的關(guān)鍵字用insert into values關(guān)鍵字,也可以像其他數(shù)據(jù)庫一樣,使用insert into select

insert into classes(name) values('1班');
insert into classes values('2班', '2011-06-15');
insert into classes(name) values('3班');
insert into classes values('4班', default);
 
insert into student values('zhangsan', 22, 1, 1);
insert into student values('lisi', 25, 0, 1);
insert into student values('wangwu', 24, 1, 3);
insert into student values('zhaoliu', 23, 0, 3);
insert into student values('mazi', 21, 1, 5);
insert into student values('wangmazi', 28, 0, 5);
insert into student values('jason', null, 0, 5);
insert into student values(null, null, 0, 5);
 
insert into student 
select 'zhangtian' name, age, sex, cid 
from student 
where name = 'tony';
    
--多條記錄同時插入
insert into student
    select 'jack', 23, 1, 5 union
    select 'tom', 24, 0, 3 union
    select 'wendy', 25, 1, 3 union
    select 'tony', 26, 0, 5;

9.2 查詢、修改、刪除數(shù)據(jù)

--查詢數(shù)據(jù)
select * from classes;
select * from student;
select id, 'bulise' name, age, sex, cid from student 
where name = 'tony';
select *, (select max(age) from student) from student 
where name = 'tony';
 
--修改數(shù)據(jù)
update student set name = 'hoho', sex = 1 where id = 1;
 
--刪除數(shù)據(jù)(from可省略)
delete from student where id = 1;

十、備份數(shù)據(jù)、備份表

常用的備份表操作 select文章來源地址http://www.zghlxwxcb.cn/news/detail-408007.html

--備份、復(fù)制student表到stu
select * into stu from student;
select * into stu1 from (select * from stu) t;
select * into stu2 from student where 1=2;   --只備份表結(jié)構(gòu)
select * from stu;
select * from stu1;
select * from stu2;

到了這里,關(guān)于【SQL Server】數(shù)據(jù)庫開發(fā)指南(二)MSSQL數(shù)據(jù)庫開發(fā)對于庫、表、數(shù)據(jù)類型、約束等相關(guān)操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包