SQL server高級應(yīng)用 收藏版建站知識
導讀:1建站知識SQL server高級應(yīng)用 收藏版,使用sqlserver的朋友可以參考下網(wǎng)站seo優(yōu)化軟件網(wǎng)站建設(shè)。
一. 建庫,建表,加約束. 1.1建庫
網(wǎng)站seo優(yōu)化診斷工具復(fù)制代碼 代碼如下:
use master go if exists (select * from sysdatabases where)—判斷master數(shù)據(jù)庫sysdatagbases表中是否存在將要創(chuàng)建的數(shù)據(jù)庫名 drop database MyDatabase—如果sysdatabases表中存在該數(shù)據(jù)庫名,則將它刪除 go exec xp_cmdshell ‘md D:/MyDatabases'—利用存儲過程創(chuàng)建一個文件夾用于存儲數(shù)據(jù)物理文件(數(shù)據(jù)文件,日志文件),DOS命令(mkdir=md) go create database MyDatabase—創(chuàng)建數(shù)據(jù)庫 on ( name='MyDatabase_data',--指定邏輯文件名 filename='D:/MyDatabases/MyDatabase_data.mdf',--指定物理文件名 size=5mb,--初始大小 maxsize=50mb,--指定物理文件最大容量,可選 filegrowth=20%--增長量 ) log on ( name='MyDatabase_log',--指定邏輯日志文件名 filename='D:/MyDatabases/MyDatabase_log.ldf',--指定日志物理文件名 size=5mb,--初始大小 maxsize=50mb,--指定日志物理文件最大容量,可選 filegrowth=20%--增長量 ) go use MyDatabase go
1.2建表.復(fù)制代碼 代碼如下:
If exists (select * from sysobjects where) drop table Mytable go create table Mytable ( ID int not null identity(1,1) primary key,--標識種子1,標識增量1,設(shè)該列為主鍵 name nchar(10) not null,--不可為空 degree numeric(18,0)—身份證,numeric(18,0)代表18位數(shù)字,小數(shù)位數(shù)為0 ) Go
1.3加約束.復(fù)制代碼 代碼如下:
Alter table stuInfo add constraint PK_stuNo primary key(stuNo)—主鍵 alter table stuMarks add constraint FK_stuNo foreign key(stuNo) references stuInfo(stuNo)—外鍵 alter table stuInfo add constraint UQ_stuID unique(stuID)—唯一約束 alter table stuInfo add constraint df_stuAddress default(‘地址不詳') for stuAddres—默認約束 alter table stuMarks add constraint ck_stuAge check(stuAge between 15 and 40)—檢查約束 alter table stuMarks drop constraint ck_stuAge—刪除約束
1.4帳戶管理. 1.4.1創(chuàng)建登錄帳戶.復(fù)制代碼 代碼如下:
exec sp_grantlogin 'jbtraining/s26301' --windows用戶為jbtraining/s26301,jbtraining 表示域 exec sp_addlogin 'admin','0533131'--SQL登錄帳戶,帳戶: 'admin',密碼:0533131.
1.4.2創(chuàng)建數(shù)據(jù)庫用戶.復(fù)制代碼 代碼如下:
exec sp_grantdbaccess 'jbtraining/s26301','s26301dbuser'--s26301dbuser為數(shù)據(jù)庫用戶名 exec sp_grantdbaccess 'admin', 's2630網(wǎng)站建設(shè)公司1dbuser'--s26301dbuser為數(shù)據(jù)庫名
1.4.3向數(shù)據(jù)庫用戶授權(quán).復(fù)制代碼 代碼如下:
/*為s26301dbuser分配對表mytable的select,insert,updata,delete權(quán)限*/ grant select,insert,update,delete on mytable to s26301dbuser /*為s26301dbuser用戶分配創(chuàng)建表的權(quán)限 grant create table to s26301dbuser
二.T-SQL編程 2.1變量. 局部變量的名稱必須以標記@作為前綴: Declare @name varchar(8)--聲明變量。 Declare @name varchar(8)=value--初始值。 Set @name=value-- 賦值。 Select @name=value--賦值。 全局變量 SQL server中的所有全局變量都使用兩個@標志作為前綴:聲明: 本文由我的SEOUC技術(shù)文章主頁發(fā)布于:2023-05-23 ,文章SQL server高級應(yīng)用 收藏版建站知識主要講述高級,標簽,SQL網(wǎng)站建設(shè)源碼以及服務(wù)器配置搭建相關(guān)技術(shù)文章。轉(zhuǎn)載請保留鏈接: http://www.bifwcx.com/article/web_5491.html