SqlServer knowledge

Table creation operation: CREAT TABLE table name
(
column name 1 data type column characteristics,
column name 3 data type column characteristics,
...
)
column characteristics:
including whether the column is empty (NULL), whether it is an identity column ( Automatic numbering), whether there is a default value, whether there is a default value, whether it is a primary key, etc.
use MySchool Set the current data to MySchool, so as to create a table in the MySchool database
Go
query whether the table exists operation:
IF EXISTS(SELECT*FROM sysobjects WHERE name='Student' )
Delete table operation: DROP TABLE Student
Go
CREATE TABLE Student--create student information table
(
... ...
)
Go
syntax for adding constraints:
ALTER TABLE table name
ADD CONSTRAINT constraint name constraint type specific constraint description
Primary key constraint:
ALTER TABLE Student
ADD CONSTRAIN PK_stuNo PRIMARY KEY (StudentNo)
Unique constraint:
ALTER TABLE Student
ADD CONSTRAIN UQ_stuID UNIQUE (IDENTITYCard)
Default constraint:
ALTER TABLE Student
ADD CONSTRAIN DF_stuAddress DEFAULT FOR stuAddress
check constraints:
ALTER TABLE Student
ADD CONSTRAIN CK_stuBornDate CHECK (BornDate>'1990-01-01')
foreign key constraints:
ALTER TABLE Student
FOREIGN KEY(GradeID) REFERENCES Grade(GradeID)
delete constraints Syntax:
ALTER TABLE table name
DROP CONSTRAINI constraint name
Use "  WITH NoCHECK " to resolve the conflict between the existing data in the table and the added constraint
ALTER TABLE Student (WITH NoCHECK--check constraint (the class hour must be greater than or equal to 0))
ADD CONSTRAIN CK_ClassHour CHECK ( ClassHour>=0)
Change table operations:
ALTER TABLE table name
( ALTER COLUM column name column definition, change column attributes
ADD column name data type constraints, add column
...
DROP column name, delete column
.... ..
ADD CONSTRAINT constraint name constraint, add constraint
...)
insert data operation:
INSERT [INTO] 表名[(列名)] VALUES (值列表)
插入多行数据的三种方法:
INSERT INTO <表名>(列名)
SELECT<列名>
FROM<源表名> 把多行数据插入已有的表中
SELECT<列名>
INTO<表名>
FROM<源表名> 把多行数据插入新表中
INSERT INTO <表名>(列名)
SELECT<列名>UNION
SELECT<列名>UNION
...... 合并插入多行数据
更改数据操作:
UPDATE 表名 SET 列名=更新值
[WHERE 更新条件]
删除数据操作:
DELETE[FROM] 表名 [WHERE 删除条件]
删除表中所有数据:
TRUNCATE TABLE 表名

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324622072&siteId=291194637