DDL Commands
Môn học: Hệ quản trị cơ sơ dữ liệu [Buổi 2]
GV: Nguyễn Mai Huy
DDL
Data Definition Language
Faculty of Information Technology
Data Definition Language
Các lệnh DDL trong SQL được sử dụng để tạo lược đồ cơ sở dữ liệu
nhằm xác định loại và cấu trúc của dữ liệu sẽ được lưu trữ trong CSDL.
Các lệnh DDL trong T-SQL được chia thành 4 loại chính như sau:
CREATE
ALTER
DROP
TRUNCATE
Nguyễn Mai Huy – Master of Information technology
DDL & Database object
Create Database <Database_Name>;
Alter Database <Db_OldName>
Modify name = <Db_NewName>;
Drop Database <DbName>;
Create Database OnlineTrading;
Nguyễn Mai Huy – Master of Information technology
DDL & Table
Create Table <Table_Name>
Alter Table <Table_Name>
Drop Table <Table_Name>
Truncate Table <Table_Name>
Nguyễn Mai Huy – Master of Information technology
DDL & View
Create View <View_Name>
Alter View <View_OldName>
Drop View <ViewName>
Nguyễn Mai Huy – Master of Information technology
DDL & Trigger
Create Trigger <Trigger_Name>
Alter Trigger <Trigger_Name>
Drop Trigger <TriggerName>
Nguyễn Mai Huy – Master of Information technology
DDL & Function
Create Function < Function_Name>
Alter Function <Function_Name>
Drop Function <FunctionName>
Nguyễn Mai Huy – Master of Information technology
DDL & Stored Procedure
Create Procedure <Procedure_Name>
Alter Procedure <Procedure_Name>
Drop Procedure <ProcedureName>
Nguyễn Mai Huy – Master of Information technology
Data types T-SQL
Faculty of Information Technology
Data types :: T-SQL
The Transact SQL language allow you to use various data types like
Binary Strings: Binary,Varbinary
Character Strings: Char,Varchar, Text
Date and Time: Date, Datetime, Datetime2, Datetimeoffset, Smalldatetime, Time
Numerics: Bigint, Int, Smallint, Tinyint, Decimal, Numeric, Bit, Float, Real, …
Unicode Character Strings: Nchar, Nvarchar, NText
Other Data Types: Uniqueidentifier, Table, Cursor, SQL_VARIANT, XML, …
Nguyễn Mai Huy – Master of Information technology
String data types
Nguyễn Mai Huy – Master of Information technology
Numeric data types
Nguyễn Mai Huy – Master of Information technology
Numeric data types
Nguyễn Mai Huy – Master of Information technology
Date and Time data types
Nguyễn Mai Huy – Master of Information technology
Other data types
Nguyễn Mai Huy – Master of Information technology
T-SQL Operators
Arithmetic Operators
(add, subtract, multiply, divide, Modulo: +, -, *, /, %)
Bitwise Operators
(and, or, exclusive or: &, |, ^)
Comparison Operators
(equal, greater than, less than, not equal: =, >, <, >=, <=, <>)
Compound Operators
(add equals, subtract equals, multiply equals: +=, -=, *=, /=, %=, &=, |=, |*=)
Logical Operators
(all, any, some, in, between, and, or, not, like, exists)
Nguyễn Mai Huy – Master of Information technology
Wildcard Characters in T-SQL
Nguyễn Mai Huy – Master of Information technology
Wildcard Characters :: Example
Nguyễn Mai Huy – Master of Information technology
DDL & Table
Faculty of Information Technology
DLL & Table
Create Table <Table_Name>;
Alter Table
Alter Table <Table_Name> Add column <col_name> <data_type>;
Alter Table <Table_Name> Drop column <col_name>;
Alter Table <Table_Name> Alter column <col_name> <data_type>;
Drop Table <TbName>;
Truncate Table <TbName>;
Nguyễn Mai Huy – Master of Information technology
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a
table. This ensures the accuracy and reliability of the data in the
table. If there is any violation between the constraint and the data
action, the action is aborted.
Constraints can be column level or table level. Column level
constraints apply to a column, and table level constraints apply to
the whole table.
Nguyễn Mai Huy – Master of Information technology
SQL Constraints
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
FOREIGN KEY - Uniquely identifies a row/record in another table
CHECK - Ensures that all values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column when no value is specified
INDEX - Used to create and retrieve data from the database very quickly
Nguyễn Mai Huy – Master of Information technology
DDL & Constraint
Add constraint
Alter Table <table_name> Add constraint <constraint_name>
Drop constraint
Alter Table <table_name> Drop constraint <constraint_name>
Nguyễn Mai Huy – Master of Information technology
Index
Faculty of Information Technology
What is index ?
Index là một cấu trúc liên kết giữa việc lưu trữ
trên đĩa với dữ liệu của một Table hay View
nhằm tăng tốc độ khi truy xuất thông tin trên
các đối tượng dạng này
Mặc nhiên, Index được tạo ra cho Table/View
dựa vào Primary key. Những Key này được
lưu trữ trong một cấu trúc B-Tree để cho phép
SQL Server tìm Row hay liên kết các Rows với
key tương ứng một cách nhanh nhất
Nguyễn Mai Huy – Master of Information technology
Pages – index nodes
Nguyễn Mai Huy – Master of Information technology
Types of indexes
Clustered indexes: dữ liệu sẽ được sắp sếp trên
bảng theo giá trị của field tương ứng. Một bảng chỉ có thể có 1
field được index theo dạng này. Mặc nhiên trong một table,
primary key field sẽ được index theo dạng này
Nonclustered Indexes: Một bảng tham chiếu được
tạo ra tương ứng với các Index được tạo ở dạng này, bao gồm
2 cột. Cột thứ nhất chứa các giá trị của field được index và cột
này được sắp sếp theo trật tự quy ước (do người lập trình thiết
lập khi tạo Noncluster index); cột còn lại sẽ chứa địa chỉ, trỏ
tới dòng có chứa giá trị tương ứng ở trên bảng.
Nguyễn Mai Huy – Master of Information technology
Clustered indexes
Clustered index sắp xếp và lưu các dòng dữ
liệu ở trong bảng dựa trên khóa. Bao gồm
những cột được định nghĩa trong Index.
Mỗi bảng chỉ có thể có 1 Clustered Index,
bởi dữ liệu của bảng chỉ có thể sắp xếp lưu
trữ theo một cách duy nhất.
Các dòng dữ liệu của một bảng được lưu
theo trật tự sắp xếp khi bảng đó được tạo
Clustered index
Một bảng chứa Clustered index thì được
gọi là Clustered table
Nếu một Table không chứa Clustered index
thì các dòng dữ liệu trong nó được lưu trữ
theo cấu trúc không sắp xếp gọi là Heap.
Nguyễn Mai Huy – Master of Information technology
Nonclustered indexes
Nonclustered index
là một cấu trúc
không đồng nhất dựa
trên các dòng dữ
liệu.
Một Nonclustered
index chứa các giá
trị khóa rời rạc, và
các giá trị khóa này
là một con trỏ, trỏ
tới dòng chứa giá trị
tương ứng
Nguyễn Mai Huy – Master of Information technology
Create | Drop Index
Create [ Clustered | Nonclustered ] Index <index_name> ON
<object> ( column1 [Asc|Desc] , column2 [Asc|Desc], … )
Drop Index <table_name.index_name>
Nguyễn Mai Huy – Master of Information technology
Syntax for
Create – Alter table
Faculty of Information Technology
Create table
Nguyễn Mai Huy – Master of Information technology
Example : Create table
Nguyễn Mai Huy – Master of Information technology
Example : Create table
Nguyễn Mai Huy – Master of Information technology
Example : Create table
Nguyễn Mai Huy – Master of Information technology
Alter table – Add Column
Nguyễn Mai Huy – Master of Information technology
Alter table – Alter Column
Nguyễn Mai Huy – Master of Information technology
Alter table – Drop Column
Nguyễn Mai Huy – Master of Information technology
Alter table – Add | Drop constraint
Nguyễn Mai Huy – Master of Information technology
Nhớ gì ?!!!
DDL: Create – Alter – Drop <Db_Object>
Data types & Operator
DDL & Table
Create table & Constraint
Alter table :: Add, Drop : Column, Constraint
Clustered Index & Nonclustered Index
Nguyễn Mai Huy – Master of Information technology
Tài liệu tham khảo
Itzik Ben-Gan, “Microsoft® SQL Server ® 2012 T-SQL Fundamentals”,
O’Reilly Media Inc, 2012
Itzik Ben-Gan, Dejan Sarka, Ed Katibah, Greg Low, Roger Wolter, and Isaac
Kunen, “Inside Microsoft SQL Server 2008: T-SQL Programming”,
Microsoft Press, 2010
w3schools, “Introduction to SQL”,
https://www.w3schools.com/sql/sql_intro.asp, 10:54PM, 18/06/2020
Microsoft SQL Server Tutorial, “Tutorials for SQL Server”,
https://docs.microsoft.com/en-us/sql/sql-server/tutorials-for-sql-server-2016,
10:54 PM, 18/06/2020
Nguyễn Mai Huy – Master of Information technology