SQL Server 學習筆記 | 一
阿新 • • 發佈:2018-12-17
1、Sqlserver基礎知識
1.1、建立資料庫
建立資料庫有兩種方式,手動建立和編寫sql指令碼建立,在這裡我採用指令碼的方式建立一個名稱為TSQLFundamentals2008的資料庫。
新建資料庫zhang
右鍵資料庫zhang,點選新建查詢
建立Schemas(架構)
--------------------------------------------------------------------- -- Create Schemas --------------------------------------------------------------------- CREATE SCHEMA HR AUTHORIZATION dbo; GO CREATE SCHEMA Production AUTHORIZATION dbo; GO CREATE SCHEMA Sales AUTHORIZATION dbo; GO
建立表:
--------------------------------------------------------------------- -- Create Tables --------------------------------------------------------------------- -- Create table HR.Employees. CREATE TABLE HR.Employees ( empid INT NOT NULL IDENTITY, lastname NVARCHAR(20) NOT NULL, firstname NVARCHAR(10) NOT NULL, title NVARCHAR(30) NOT NULL, titleofcourtesy NVARCHAR(25) NOT NULL, birthdate DATETIME NOT NULL, hiredate DATETIME NOT NULL, address NVARCHAR(60) NOT NULL, city NVARCHAR(15) NOT NULL, region NVARCHAR(15) NULL, postalcode NVARCHAR(10) NULL, country NVARCHAR(15) NOT NULL, phone NVARCHAR(24) NOT NULL, mgrid INT NULL, CONSTRAINT PK_Employees PRIMARY KEY(empid), CONSTRAINT FK_Employees_Employees FOREIGN KEY(mgrid) REFERENCES HR.Employees(empid), CONSTRAINT CHK_birthdate CHECK(birthdate <= CURRENT_TIMESTAMP) ); CREATE NONCLUSTERED INDEX idx_nc_lastname ON HR.Employees(lastname); CREATE NONCLUSTERED INDEX idx_nc_postalcode ON HR.Employees(postalcode);
-- Create table Production.Suppliers CREATE TABLE Production.Suppliers ( supplierid INT NOT NULL IDENTITY, companyname NVARCHAR(40) NOT NULL, contactname NVARCHAR(30) NOT NULL, contacttitle NVARCHAR(30) NOT NULL, address NVARCHAR(60) NOT NULL, city NVARCHAR(15) NOT NULL, region NVARCHAR(15) NULL, postalcode NVARCHAR(10) NULL, country NVARCHAR(15) NOT NULL, phone NVARCHAR(24) NOT NULL, fax NVARCHAR(24) NULL, CONSTRAINT PK_Suppliers PRIMARY KEY(supplierid) ); CREATE NONCLUSTERED INDEX idx_nc_companyname ON Production.Suppliers(companyname); CREATE NONCLUSTERED INDEX idx_nc_postalcode ON Production.Suppliers(postalcode);
-- Create table Production.Categories
CREATE TABLE Production.Categories
(
categoryid INT NOT NULL IDENTITY,
categoryname NVARCHAR(15) NOT NULL,
description NVARCHAR(200) NOT NULL,
CONSTRAINT PK_Categories PRIMARY KEY(categoryid)
);
CREATE INDEX categoryname ON Production.Categories(categoryname);
-- Create table Production.Products
CREATE TABLE Production.Products
(
productid INT NOT NULL IDENTITY,
productname NVARCHAR(40) NOT NULL,
supplierid INT NOT NULL,
categoryid INT NOT NULL,
unitprice MONEY NOT NULL
CONSTRAINT DFT_Products_unitprice DEFAULT(0),
discontinued BIT NOT NULL
CONSTRAINT DFT_Products_discontinued DEFAULT(0),
CONSTRAINT PK_Products PRIMARY KEY(productid),
CONSTRAINT FK_Products_Categories FOREIGN KEY(categoryid)
REFERENCES Production.Categories(categoryid),
CONSTRAINT FK_Products_Suppliers FOREIGN KEY(supplierid)
REFERENCES Production.Suppliers(supplierid),
CONSTRAINT CHK_Products_unitprice CHECK(unitprice >= 0)
);
CREATE NONCLUSTERED INDEX idx_nc_categoryid ON Production.Products(categoryid);
CREATE NONCLUSTERED INDEX idx_nc_productname ON Production.Products(productname);
CREATE NONCLUSTERED INDEX idx_nc_supplierid ON Production.Products(supplierid);
-- Create table Sales.Customers
CREATE TABLE Sales.Customers
(
custid INT NOT NULL IDENTITY,
companyname NVARCHAR(40) NOT NULL,
contactname NVARCHAR(30) NOT NULL,
contacttitle NVARCHAR(30) NOT NULL,
address NVARCHAR(60) NOT NULL,
city NVARCHAR(15) NOT NULL,
region NVARCHAR(15) NULL,
postalcode NVARCHAR(10) NULL,
country NVARCHAR(15) NOT NULL,
phone NVARCHAR(24) NOT NULL,
fax NVARCHAR(24) NULL,
CONSTRAINT PK_Customers PRIMARY KEY(custid)
);
CREATE NONCLUSTERED INDEX idx_nc_city ON Sales.Customers(city);
CREATE NONCLUSTERED INDEX idx_nc_companyname ON Sales.Customers(companyname);
CREATE NONCLUSTERED INDEX idx_nc_postalcode ON Sales.Customers(postalcode);
CREATE NONCLUSTERED INDEX idx_nc_region ON Sales.Customers(region);
-- Create table Sales.Shippers
CREATE TABLE Sales.Shippers
(
shipperid INT NOT NULL IDENTITY,
companyname NVARCHAR(40) NOT NULL,
phone NVARCHAR(24) NOT NULL,
CONSTRAINT PK_Shippers PRIMARY KEY(shipperid)
);
-- Create table Sales.Orders
CREATE TABLE Sales.Orders
(
orderid INT NOT NULL IDENTITY,
custid INT NULL,
empid INT NOT NULL,
orderdate DATETIME NOT NULL,
requireddate DATETIME NOT NULL,
shippeddate DATETIME NULL,
shipperid INT NOT NULL,
freight MONEY NOT NULL
CONSTRAINT DFT_Orders_freight DEFAULT(0),
shipname NVARCHAR(40) NOT NULL,
shipaddress NVARCHAR(60) NOT NULL,
shipcity NVARCHAR(15) NOT NULL,
shipregion NVARCHAR(15) NULL,
shippostalcode NVARCHAR(10) NULL,
shipcountry NVARCHAR(15) NOT NULL,
CONSTRAINT PK_Orders PRIMARY KEY(orderid),
CONSTRAINT FK_Orders_Customers FOREIGN KEY(custid)
REFERENCES Sales.Customers(custid),
CONSTRAINT FK_Orders_Employees FOREIGN KEY(empid)
REFERENCES HR.Employees(empid),
CONSTRAINT FK_Orders_Shippers FOREIGN KEY(shipperid)
REFERENCES Sales.Shippers(shipperid)
);
CREATE NONCLUSTERED INDEX idx_nc_custid ON Sales.Orders(custid);
CREATE NONCLUSTERED INDEX idx_nc_empid ON Sales.Orders(empid);
CREATE NONCLUSTERED INDEX idx_nc_shipperid ON Sales.Orders(shipperid);
CREATE NONCLUSTERED INDEX idx_nc_orderdate ON Sales.Orders(orderdate);
CREATE NONCLUSTERED INDEX idx_nc_shippeddate ON Sales.Orders(shippeddate);
CREATE NONCLUSTERED INDEX idx_nc_shippostalcode ON Sales.Orders(shippostalcode);
-- Create table Sales.OrderDetails
CREATE TABLE Sales.OrderDetails
(
orderid INT NOT NULL,
productid INT NOT NULL,
unitprice MONEY NOT NULL
CONSTRAINT DFT_OrderDetails_unitprice DEFAULT(0),
qty SMALLINT NOT NULL
CONSTRAINT DFT_OrderDetails_qty DEFAULT(1),
discount NUMERIC(4, 3) NOT NULL
CONSTRAINT DFT_OrderDetails_discount DEFAULT(0),
CONSTRAINT PK_OrderDetails PRIMARY KEY(orderid, productid),
CONSTRAINT FK_OrderDetails_Orders FOREIGN KEY(orderid)
REFERENCES Sales.Orders(orderid),
CONSTRAINT FK_OrderDetails_Products FOREIGN KEY(productid)
REFERENCES Production.Products(productid),
CONSTRAINT CHK_discount CHECK (discount BETWEEN 0 AND 1),
CONSTRAINT CHK_qty CHECK (qty > 0),
CONSTRAINT CHK_unitprice CHECK (unitprice >= 0)
)
CREATE NONCLUSTERED INDEX idx_nc_orderid ON Sales.OrderDetails(orderid);
CREATE NONCLUSTERED INDEX idx_nc_productid ON Sales.OrderDetails(productid);
GO
HR.Employees |
僱員表,存放員工的一些基本資訊。 |
Production.Products |
產品資訊表 |
Production.Suppliers |
供應商表 |
Production.Customers |
顧客資訊表 |
Production.Categories |
產品類別表 |
Sales.OrderDetails |
訂單詳情表 |
Sales.Orders |
訂單表 |
Sales.Shippers |
貨運公司表 |
3、Sqlserver一些基本命令:
3.1、查詢資料庫是否存在:
if DB_ID("testDB")is not null;
3.2、檢查表是否存在:
if OBJECT_ID(“textDB”,“U”) is not null ;其中U代表使用者表
3.3、建立資料庫:
create database+資料名
3.4、刪除資料庫:
drop database 資料庫名 --刪除資料庫的
drop table 表名--刪除表的
delete from 表名 where 條件 --刪除資料的
3.5、查詢語句:
use 資料庫名稱 --修改的資料庫
select*from +表名稱 --要查詢的表
select 某某,某某,某某 from 表名稱 where 條件 --帶條件查詢的資料
3.6、插入資料:
insert into 表名稱 (條件)values (相對應的值)