Transact-SQL
Transact-SQL(又稱 T-SQL),是在 Microsoft SQL Server 和 Sybase SQL Server 上的 ANSI SQL 實(shí)現(xiàn),與 Oracle 的 PL/SQL 性質(zhì)相近(不只是實(shí)現(xiàn) ANSI SQL,也為自身數(shù)據(jù)庫系統(tǒng)的特性提供實(shí)現(xiàn)支持),在 Microsoft SQL Server 和 Sybase Adaptive Server 中仍然被使用為核心的查詢語言。
數(shù)據(jù)庫
1、創(chuàng)建數(shù)據(jù)庫
USE master ;
GO
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\saledat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = Sales_log,
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\salelog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB ) ;
GO
2、查看數(shù)據(jù)庫
SELECT name, database_id, create_date
FROM sys.databases ;
3、刪除數(shù)據(jù)庫
表
1、創(chuàng)建表
CREATE TABLE PurchaseOrderDetail
(
ID uniqueidentifier NOT NULL
,LineNumber smallint NOT NULL
,ProductID int NULL
,UnitPrice money NULL
,OrderQty smallint NULL
,ReceivedQty float NULL
,RejectedQty float NULL
,DueDate datetime NULL
);
2、刪除表
DROP TABLE dbo.PurchaseOrderDetail;
3、重命名表
EXEC sp_rename 'Sales.SalesTerritory', 'SalesTerr';
列
1、添加列
ALTER TABLE dbo.doc_exa ADD column_b VARCHAR(20) NULL, column_c INT NULL ;
2、刪除列
ALTER TABLE dbo.doc_exb DROP COLUMN column_b;
3、重命名列
EXEC sp_rename 'Sales.SalesTerritory.TerritoryID', 'TerrID', 'COLUMN';
約束
1、主鍵
--在現(xiàn)有表中創(chuàng)建主鍵
ALTER TABLE Production.TransactionHistoryArchive
ADD CONSTRAINT PK_TransactionHistoryArchive_TransactionID PRIMARY KEY CLUSTERED (TransactionID);
--在新表中創(chuàng)建主鍵
CREATE TABLE Production.TransactionHistoryArchive1
(
TransactionID int IDENTITY (1,1) NOT NULL
, CONSTRAINT PK_TransactionHistoryArchive_TransactionID PRIMARY KEY CLUSTERED (TransactionID)
)
;
--查看主鍵
SELECT name
FROM sys.key_constraints
WHERE type = 'PK' AND OBJECT_NAME(parent_object_id) = N'TransactionHistoryArchive';
GO
--刪除主鍵
ALTER TABLE Production.TransactionHistoryArchive
DROP CONSTRAINT PK_TransactionHistoryArchive_TransactionID;
GO
視圖
1、創(chuàng)建視圖
CREATE VIEW V_EmployeeHireDate
AS
SELECT p.FirstName, p.LastName, e.HireDate
FROM HumanResources.Employee AS e JOIN Person.Person AS p
ON e.BusinessEntityID = p.BusinessEntityID ;
GO
2、刪除視圖
DROP VIEW V_EmployeeHireDate;
存儲(chǔ)過程
1、創(chuàng)建存儲(chǔ)過程
CREATE PROCEDURE P_UspGetEmployeesTest
@LastName nvarchar(50),
@FirstName nvarchar(50)
AS
SELECT FirstName, LastName, Department
FROM HumanResources.vEmployeeDepartmentHistory
WHERE FirstName = @FirstName AND LastName = @LastName
AND EndDate IS NULL;
GO
2、刪除存儲(chǔ)過程
DROP PROCEDURE P_UspGetEmployeesTest;
3、執(zhí)行存儲(chǔ)過程
EXEC P_UspGetEmployeesTest N'Ackerman', N'Pilar';
-- Or
EXEC P_UspGetEmployeesTest @LastName = N'Ackerman', @FirstName = N'Pilar';
GO
-- Or
EXECUTE P_UspGetEmployeesTest @FirstName = N'Pilar', @LastName = N'Ackerman';
GO
4、重命名存儲(chǔ)過程
EXEC sp_rename 'P_UspGetAllEmployeesTest', 'P_UspEveryEmployeeTest2';
5、帶有輸出參數(shù)的存儲(chǔ)過程
CREATE PROCEDURE P_UspGetEmployeeSalesYTD
@SalesPerson nvarchar(50),
@SalesYTD money OUTPUT
AS
SELECT @SalesYTD = SalesYTD
FROM SalesPerson AS sp
JOIN vEmployee AS e ON e.BusinessEntityID = sp.BusinessEntityID
WHERE LastName = @SalesPerson;
RETURN
GO
--調(diào)用
DECLARE @SalesYTDBySalesPerson money;
EXECUTE P_UspGetEmployeeSalesYTD
N'Blythe',
@SalesYTD = @SalesYTDBySalesPerson OUTPUT;
GO
數(shù)據(jù)類型
總結(jié)
以上所述是小編給大家介紹的Sql Server數(shù)據(jù)庫常用Transact-SQL腳本,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
您可能感興趣的文章:- mysql報(bào)錯(cuò):Deadlock found when trying to get lock; try restarting transaction的解決方法
- c#中SqlTransaction——事務(wù)詳解
- mysql Non-Transactional Database Only(只支持MyISAM)
- 淺談Transact-SQL
- MySQL數(shù)據(jù)庫事務(wù)隔離級(jí)別介紹(Transaction Isolation Level)
- Transact_SQL 小手冊(cè)