You are on page 1of 12

Data Definition Language

TCS Internal 1
Objective

Describes all of the Transact-SQL statements concerning data definition


language (DDL).

TCS Internal 2
Scope

 Creating Database Objects


 Modifying Database Object
 Removing Database Objects

TCS Internal 3
Creating Database Objects

 SQL Server database objects can be created using:


 Transact-SQL
 SQL Server Management Studio

TCS Internal 4
Creating Database (T-SQL)

 Using Transact-SQL, database can be created using the CREATE DATABASE statement.

CREATE DATABASE [db_name] [ON [ PRIMARY]


file_spect {,file_spec2} …]
[LOG ON file_spec3 {,file_spec4} ….]
[COLLATE collation_name
[FOR {ATTACH | ATTACH_REBUILD_LOG}]

Example:

CREATE DATABASE [Inventory] ON PRIMARY


( NAME = N'Inventory', FILENAME = N'D:\data\Inventory.mdf' , SIZE = 2048KB ,
MAXSIZE= UNLIMITED, FILEGROWTH = 10%
)
LOG ON
(
NAME = N'Inventory_log', FILENAME = N'E:\log\Inventory_log.ldf' , SIZE = 1024KB ,
MAXSIZE = 2048GB , FILEGROWTH = 10%
)
COLLATE SQL_Latin1_General_CP1_CI_AS

TCS Internal 5
Creating Table (T-SQL)

 Using Transact-SQL, table can be created using the CREATE TABLE statement.

CREATE TABLE [table_name]


(col_name1 type1 [NOTNULL | NULL]
[{, col_name2 type2 [NOTNULL | NULL}…])

Example:

CREATE TABLE [dbo].[EMP]


(
[EMP_NO] [int] IDENTITY(1,1) NOT NULL,
[EMP_NAME] [varchar](50) NOT NULL CONSTRAINT
)
ON [PRIMARY]

TCS Internal 6
Creating other database objects

 CREATE DEFAULT – Creates a default value


 CREATE RULE – Defined for the values of a column or for an alias data type.
 CREATE INDEX – Creates a new index on a specified table.
 CREATE PROCEDURE – Creates a new stored procedure
 CREATE TRIGGER - Creates a new trigger
 CREATE SCHEMA – Creates a new schema

TCS Internal 7
Modifying Database Objects

 SQL Server database objects can be modified using:

 Transact-SQL
 SQL Server Management Studio

 SQL Server supports changing the structure of the following database objects.

 Database
 Table
 Schema
 Stored Procedure
 View
 Trigger

TCS Internal 8
Altering a Database

 The ALTER DATABADE statement changes the physical structure of a database.

ALTER DATABASE db_name


ADD FILE file_SPEC1 [TO FILEGROUP group_name1]
| ADD LOG FILE file_spec2
| REMOVE FILE ‘file_name’
| MODIFY FILE (NAME = old_name, NEWNAME =new_name…)
| CREATE FILEGROUP group_name2 | DROP FILEGROUP filegroup_name3
| SET option_specification [WITH termination]

Example:

ALTER DATABSE projects


ADD FILE (
NAME = projects_dat1,
FILENAME = ‘C:\DATA\projects1.mdf’,
SIZE =10,
MAXSIZE = 100,
FILEGROWTH =5
)

TCS Internal 9
Altering a Table

 The ALTER TABLE statement modify the schema of a table.

ALTER TABLE table_name


ADD col_name type [NULL | IDENTITY]
DROP COLUMN col_name [{,col_name}..]
ALTER COLUMN col_name type {NULL|IDENTITY}

Example:

ALTER TABLE department


ALTER COLUMN location CHAR(25) NOT NULL

TCS Internal 10
Removing Database Objects

 All Transact –SQL statements to remove a database object have a general form:

DROP object object_name

Example : DROP DATABASE database_name


DROP TABLE table_name

 The DROP statements can be among others:

 Default
 RULE
 TYPE
 PROCEDURE
 INDEX
 VIEW
 TRIGGER
 SCHEMA
 STATISTICS

TCS Internal 11
Summary

SQL Server supports many data definition statements that create, alter, and remove
database objects. The following database objects, among others, can be created and
removed using the CREATE object and the DROP object statement, respectively:

 Database
 Table
 Schema
 View
 Trigger
 Stored Procedure
 Index
 Rule
 Default

 The Transact-SQL language provides four data manipulation statements:


 SELECT
 INSERT
 UPDATE
 DELETE

TCS Internal 12

You might also like