T-SQL(SQLSERVER)

使用自定义类型名

CREATE DATABASE Student
GO

USE Student
GO

Exec sp_addtype char20,'varchar(20)','null'
GO

 

在库里建立表

DECLARE @CHAR20 char20

USE Student
GO

CREATE TABLE Student(
    Sno int PRIMARY KEY,
    Ssex char(2) NOT NULL,
    Sage smallint,
    Sdept char,

    CONSTRAINT chk_Sex  CHECK (Ssex in ('','')),
    CONSTRAINT chk_Sage CHECK (Sage BETWEEN 15 AND 30)
)

CREATE TABLE Course(
    Cno int PRIMARY KEY,
    Cname char,
    Cpno int NULL,
    Credeit int,
    Teacher char20
)

CREATE TABLE SC(
    Sno int NOT NULL,
    Cno int NOT NULL,
    Grade int,
    
    CONSTRAINT PK2 PRIMARY KEY(Sno, Cno),
    CONSTRAINT FK_Sno FOREIGN KEY(Sno) REFERENCES Student(Sno),
    CONSTRAINT FK_Cno FOREIGN KEY(Cno) REFERENCES Course(Cno),
    CONSTRAINT chk_Grade CHECK (Grade BETWEEN 0 AND 100)
)

 

 

 

参考:https://docs.microsoft.com/zh-cn/sql/t-sql/statements/insert-transact-sql?view=aps-pdw-2016-au7

 

 

 计算日期

 year(getDate())-Sage

注意是year(A) - int类型,而不是year(A-int)

 

TSQL语句官方文档

https://docs.microsoft.com/zh-cn/sql/t-sql/statements/statements?view=sql-server-ver15

CREATE VIEW

 

你可能感兴趣的:(T-SQL(SQLSERVER))