表定义常用案例

create database D20140415

USE D20140415

/*

一般的定义方式,很不专业

*/
create table T_Persion
(
FName VARCHAR(20),
FAge INT
)

INSERT INTO T_Persion (FName)  VALUES ('HELLO');
INSERT INTO T_Persion (FAge)  VALUES (11);
INSERT INTO T_Persion (FName,FAge)  VALUES ('HELLO',10);

SELECT * FROM T_Persion;

truncate table T_Persion;

/*
定义非空约束

NOT NULL
*/

CREATE TABLE T_Student(
FNumber VARCHAR(20) NOT NULL ,
FName VARCHAR(20) NOT NULL,
FAge INT NOT NULL,
FFavorite VARCHAR(20),
FPhoneNumber VARCHAR(20)
)

INSERT INTO T_Student (FNumber,FName,FAge) VALUES ('10001','JACK',11);
INSERT INTO T_Student (FNumber,FName,FAge) VALUES ('10002','ANDY',12);

SELECT * FROM T_Student;


/*
定义默认值


DEFAULT
*/

CREATE TABLE T_Teacher(
FNumber VARCHAR(20),
FName VARCHAR(20),
FAge INT,
FISMaster VARCHAR(5) DEFAULT 'NO'
)

INSERT INTO T_Teacher (FNumber,FName,FAge,FISMaster) VALUES ('10001','JACK',12,'YES');
INSERT INTO T_Teacher (FNumber,FName,FAge) VALUES ('10001','JACK',12);

SELECT * FROM T_Teacher;


/*
定义主键


PRIMARY KEY()
*/

CREATE TABLE T_Bus(
FNumber VARCHAR(20),
FDriverName VARCHAR(20),
FUsedYears INT,
PRIMARY KEY(FNumber)
)


/*
定义外键

FOREIGN KEY  外键字段名称  REFERENCES  目标表名(被关联的字段名称)
*/

CREATE TABLE T_Department ( /*部门信息表*/
FId VARCHAR(20),
FName VARCHAR(20),
FLevel INT ,
PRIMARY KEY (FId)
)

TRUNCATE TABLE T_Department;

INSERT INTO T_Department (FId,FName) VALUES ('001','财务部');
INSERT INTO T_Department (FId,FName) VALUES ('002','销售部');
INSERT INTO T_Department (FId,FName) VALUES ('003','工程部');
INSERT INTO T_Department (FId,FName) VALUES ('004','后勤部');

DELETE FROM T_Department;

SELECT * FROM T_Department;

drop table T_Department;

CREATE TABLE T_Employee ( /*员工信息表*/
FNumber VARCHAR(20),
FName VARCHAR(20),
FDepartmentId VARCHAR(20),
FOREIGN KEY (FDepartmentId) REFERENCES T_Department(FId)
)

INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0001','jack','001');
INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0002','lucy','001');
INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0003','jim','001');

INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0004','张总','002');
INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0005','李总','002');
INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0006','王总','002');

INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0006','王工','003');
INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0006','李工','003');
INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0006','张工','003');

INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0006','小王','004');
INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0006','小李','004');
INSERT INTO T_Employee (FNumber,FName,FDepartmentId) VALUES ('0006','小赵','004');


SELECT FNumber,T_Employee.FName,T_Department.FName FROM T_Employee INNER JOIN T_Department ON T_Employee.FDepartmentId = T_Department.FId;


/*
自增长列的定义

*/

CREATE TABLE T_test(
ID int IDENTITY(1,1),
Name varchar(50)
)

INSERT INTO T_test (Name) VALUES ('jack');

select * from T_test;










你可能感兴趣的:(表定义常用案例)