当前位置:网站首页>SQL Server 2016 learning record - Data Definition
SQL Server 2016 learning record - Data Definition
2022-07-28 10:24:00 【Hard work Zhang Zhang】
Learning goals :
1、 Definition and deletion of patterns
2、 The definition of the basic table 、 Delete and modify
3、 data type
4、 Index creation and deletion
5、 The data dictionary
Learning content :
1、 Definition and deletion of patterns
Defining patterns
--[ example 3.1] Define a student - Course model S-T
create schema "S-T" authorization test;
/* For the user test Defines a pattern S-T*/
-- If not specified < Schema name >, that < Schema name > Implied as < user name >
The definition schema actually defines a namespace .
In this space, you can define the database objects that the schema contains , For example, the basic table 、 View 、 Index, etc. .
stay CREATE SCHEMA It is acceptable that CREATE TABLE,CREATE VIEW and GRANT Clause , Therefore, you can create tables while creating patterns 、 View etc. .
--[ example 3.3] For the user ZHANG A pattern is created TEST, And define a table in it TAB1
CREATE SCHEMA AAA AUTHORIZATION test
CREATE TABLE TAB1 ( COL1 SMALLINT,
COL2 INT,
COL3 CHAR(20),
COL4 NUMERIC(10,3),
COL5 DECIMAL(5,2)
);
Delete the pattern
DROP SCHEMA < Schema name > <CASCADE|RESTRICT>
CASCADE( cascade ): Delete the schema and delete all database objects in the schema
RESTRICT( Limit ): If a subordinate database object is defined in the schema ( As shown in the table 、 View etc. ), The delete statement is rejected . Only when there are no subordinate objects in this mode can
--[ example 3.4]
DROP SCHEMA s1 CASCADE;
/* Delete the pattern ZHANG, At the same time, the tables defined in the schema TAB1 Also deleted */
2、 The definition of the basic table 、 Delete and modify
Define the base table
--[ example 3.5] establish “ Student ” surface Student. Student number is the main code , The name value is unique
CREATE TABLE Student
(Sno CHAR(9) PRIMARY KEY, /* Column level integrity constraints ,Sno It's the main code */
Sname CHAR(20) UNIQUE, /* Sname Take unique value */
Ssex CHAR(2),
Sage SMALLINT,
Sdept CHAR(20)
);
-- [ example 3.6 ] Build a “ Course ” surface Course
CREATE TABLE Course
(Cno CHAR(4) PRIMARY KEY,
Cname CHAR(40),
Cpno CHAR(4),
Ccredit SMALLINT,
FOREIGN KEY (Cpno) REFERENCES Course(Cno)
);
--[ example 3.7] Build a “ Students' course selection ” surface SC
CREATE TABLE SC
(Sno CHAR(9),
Cno CHAR(4),
Grade SMALLINT,
PRIMARY KEY (Sno,Cno),
/* The main code consists of two properties , Must be defined as table level integrity */
FOREIGN KEY (Sno) REFERENCES Student(Sno),
/* Table level integrity constraints ,Sno It's the outside code , The reference table is Student */
FOREIGN KEY (Cno) REFERENCES Course(Cno)
/* Table level integrity constraints , Cno It's the outside code , The reference table is Course*/
);
Modify the basic table
ALTER TABLE < Table name >
[ ADD[COLUMN] < New column names > < data type > [ Integrity constraints ] ]
[ ADD < Table level integrity constraints >]
[ DROP [ COLUMN ] < Name > [CASCADE| RESTRICT] ]
[ DROP CONSTRAINT< Integrity constraint name >[ RESTRICT | CASCADE ] ]
[ALTER COLUMN < Name >< data type > ] ;
--[ example 3.8] towards Student Table increase “ Admission time ” Column , Its data type is date type
ALTER TABLE Student ADD S_entrance DATE;
/* Whether or not there is data in the basic table , All newly added columns are null */
--[ example 3.9] The data type of age is changed from character type to character type ( Suppose the original data type is character type ) Change it to an integer .
ALTER TABLE Student ALTER COLUMN Sage INT;
--[ example 3.10] Add the constraint that the course name must be unique .
ALTER TABLE Course ADD UNIQUE(Cname);
Delete base table
DROP TABLE < Table name >[RESTRICT| CASCADE];
RESTRICT: There are restrictions on deleting tables .
The base table to be deleted cannot be referenced by constraints of other tables
If there are objects that depend on the table , The table cannot be deleted
CASCADE: There are no restrictions on deleting this table .
While deleting the base table , Related dependent objects are deleted together
--[ example 3.11] Delete Student surface
DROP TABLE Student CASCADE ;
/* The basic table definition is deleted , Data is deleted Index built on table 、 View 、 Triggers, etc. will generally be deleted */
3、 data type
Character data type

Exact numeric data type

Approximate numerical data type

Binary data type

Date and time data types


4、 Index creation and deletion
The purpose of indexing : Speed up the query
Common indexes in relational database management system :
- Index on sequential file
- B+ Tree index
- hash (hash) Indexes
- Bitmap index
characteristic :
B+ Tree index has the advantage of dynamic balance
HASH Index has the characteristics of fast search speed
Index
CREATE [UNIQUE] [CLUSTERED] INDEX < Index name >
ON < Table name >(< Name >[< order >][,< Name >[< order >] ]…);
< Table name >: The name of the underlying table to be indexed
Indexes : It can be built on one or more columns of the table , The column names are separated by commas
< order >: Specifies the order of index values , Ascending :ASC, Descending :DESC. The default value :ASC
UNIQUE: Each index value of this index only corresponds to a unique data record
CLUSTERED: Indicates that the index to be built is a clustered index
--[ example 3.13] stay Student Tabular Sname( full name ) Create a clustered index on the column
CREATE CLUSTERED INDEX Stusname
ON Student(Sname);
* Cluster index is established on the most frequently queried columns to improve query efficiency
At most one clustered index can be created on a basic table
Clustered indexes should not be established for frequently updated columns *
--[ example 3.14] For students - In the course database Student,Course,SC Three tables are built Vertical index .
--Student Create a unique index in ascending order of student number ;Course The table has a unique index in ascending order of course number ;SC The table creates a unique index in ascending order of student number and descending order of course number
CREATE UNIQUE INDEX Stusno ON Student(Sno);
CREATE UNIQUE INDEX Coucno ON Course(Cno);
CREATE UNIQUE INDEX SCno ON SC(Sno ASC,Cno DESC);
Delete index
DROP INDEX < Index name >;
When deleting an index , The system will delete the relevant information from the data dictionary
Quoted description .
--[ example 3.15] Delete Student Tabular Stusname Indexes
DROP INDEX Stusname on student;
5、 The data dictionary
A data dictionary is a set of system tables in a relational database management system , It records all the definition information in the database :
- Relational schema definition
- View definition
- Index definition
- Integrity constraint definition
- The operation authority of all kinds of users to the database
- Statistics, etc
The relational database management system is executing SQL Data definition statement , In fact, it is updating the corresponding information in the data dictionary table .
边栏推荐
- Sleeping barber problem
- 记录一次idea中的父子项目修改project与module名称,亲测!
- Context values traps and how to avoid or mitigate these traps in go
- [cloud co creation] enterprise digital transformation, Huawei cloud consulting is with you
- 问题总结档案
- 数据库mysql基础
- 利用正则表达式从文件路径中匹配文件名
- 多线程与高并发(三)—— 源码解析 AQS 原理
- Aqua Data Studio 18.5.0导出insert语句
- 在mt6735中添加新的开机logo与开\关机动画
猜你喜欢

用两个栈实现一个队列【C语言】
![[wechat applet] project practice - lottery application](/img/7b/a0545f077259b3dc971dc246813177.png)
[wechat applet] project practice - lottery application

C language secondary pointer explanation and example code

巧用ngx_lua做流量分组

语音聊天app——如何规范开发流程?

利用正则表达式从文件路径中匹配文件名

Voice chat app - how to standardize the development process?

Aqua Data Studio 18.5.0导出insert语句

Get to know SuperMap idesktop for the first time

【微信小程序】项目实战—抽签应用
随机推荐
数据库安全 --- 创建登录名 用户+配置权限【笔记】
Aqua Data Studio 18.5.0导出insert语句
9. Delete nodes in the linked list
初识SuperMap iDesktop
按位与、或、异或等运算方法
Ie compatibility problem handling
IDEA创建我的第一个项目
16. String inversion
Continue to write the greatest work based on modelarts [play with Huawei cloud]
中兴通讯总裁徐子阳:5nm芯片将在2021年推出
简介
Prometheus 运维工具 Promtool (四)TSDB 功能
Performance test of API gateway APIs IX in Google cloud T2a and T2D
吴雄昂遭Arm罢免内幕:建私人投资公司,损害了股东利益?
Record a parent-child project in idea, modify the name of project and module, and test it personally!
【微信小程序】项目实战—抽签应用
Xu Ziyang, President of ZTE: 5nm chip will be launched in 2021
5. Dynamic programming -- Fibonacci series
Multithreading and high concurrency (III) -- source code analysis AQS principle
2. Output one of the repeated numbers in the array