Module 2 - Data Definition Language
01/22/2026
Tag: #ADV_DBMS
Data Definition Language (DDL)
- CREATE - Create a table
- ALTER - Modify table name , columns name
- DROP - Delete
- RENAME - Rename
- TRUNCATE - Empty rows data
- CONMMENT - SQL comment Table - Basic unit of storage, composed of rows
Naming Rules for Table and Column Names
- Must begin with a letter
- Must be 1 - 30 characters long
- Must contain only A-Z, a-z, 0-9, _, '$' and '#'
- Must not duplicate the name of another object owned by the same user
- Must not be a server - reserved word
Steps in Table Creation
- Identify data types for attributes
- Identify columns that can and cannot be null
- Identify columns that must be unique(candidate keys)
- Determine default values
- Identify constraints on columns(Domain specification)
- Create the table
Including Constraints
- Constraints enforce rules at the table level.
- Constraints prevent the deletion of a table if there are dependencies
- The following constraints are commonly used in SQL:
- 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 - Prevent actions that would destroy links between tables
- CHECK - Ensure that the values in a column satisfies a specific condition
- DEFAULT - Sets a default value for a column if no values is specified
- CREATE INDEX - Used to create and retrieve data from the database very quickly
Constraints Guidelines
- You can name a constraint, or the MS SQL Server generates a name
- Create a constraint at either of the following times:
- At the same time as the creation of the table
- After the creation of the table
- Define a constraint at the column or table level
- View a constraint in the data dictionary
DDL Statements
The following SQL statement is DDL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
SQL Constraints
MSSQL Constraints are rules applied to table columns or tables to enforce data integrity in a SQL Server database. - Accuracy - Consistency - Integrity - Rules
Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement.
- Table Level / Column Level
Table Level Constraints (資料表 層級)
- Define Primary key / Foreign Key / Unique / Check
- 通常Constraints 寫在 欄位定義的最後
- 多欄位,需要命名,方便維護與刪除 ,容易理解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Column Level Constraints (欄位 層級)
- PRIMARY KEY , UNIQUE , CHECK is identify when creating table
- The constraint name generate by MS SQL Server
1 2 3 4 5 6 | |
ALTER TABLE adding Constraints
- 資料表已經存在 Table existed
- 資料庫維護或者後期設計調整
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |