当前位置:网站首页>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 .
边栏推荐
- Context values traps and how to avoid or mitigate these traps in go
- LIBCMTD.lib
- C语言 二级指针详解及示例代码
- a different object with the same identifier value was already associated with the session
- Deadlock algorithm: banker algorithm and security algorithm
- Qt | 信号和槽的一些总结
- Go 内存模型 (2014年5月31日版本)
- 【栈的应用】--- 中缀表达式转后缀表达式
- 10、链表中倒数第k个节点
- Etcd (highly available kV database)
猜你喜欢

PHP generates QR code (learning)

14. Double pointer - the container that holds the most water

uni-app项目目录、文件作用介绍 及 开发规范

14、双指针——盛最多水的容器

【微信小程序】项目实战—抽签应用

gcc: error trying to exec 'as': execvp: No such file or directory

SuperMap iserver publishing management and calling map services

C language secondary pointer explanation and example code

7、二分法——寻找一组重复或者有序但是旋转的数组

11、链表反转
随机推荐
Get to know SuperMap idesktop for the first time
a different object with the same identifier value was already associated with the session
Guangzhou metro line 14 xinshixu station is under construction, and residents in Baiyun District are about to start a double line transfer mode!
centos7下安装mysql,网上文章都不太准
It's settled! On July 30!
ogg参数filter的使用问题【急】
14. Double pointer - the container that holds the most water
语音聊天app——如何规范开发流程?
初识SuperMap iDesktop
问题总结档案
记录一次idea中的父子项目修改project与module名称,亲测!
SQL Server 2016 学习记录 --- 嵌套查询
8. Numbers that appear more than half of the time in the array
MySQL架构原理
Netease written test No. 2 -- typical application of European distance
Differences among pipes, pipe passes and pipe States
【栈的应用】--- 中缀表达式转后缀表达式
What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?
7. Dichotomy -- find a set of repeated or ordered but rotating arrays
中芯国际科创板IPO顺利过会,市值有望突破2000亿!