Lab DDL
Lab DDL
The data type of a column defines what value the column can hold: integer, character, money,
date and time, binary, and so on. Each column in a database table is required to have a name and
1
STRACTURED QUERY LANGUAGE DDL CHAPTER 7
a data type. An SQL developer must decide what type of data that will be stored inside each
column when creating a table. The data type is a guideline for SQL to understand what type of
data is expected inside of each column, and it also identifies how SQL will interact with the
stored data. The following tables describes data types which are used in the sql server.
2
STRACTURED QUERY LANGUAGE DDL CHAPTER 7
3
STRACTURED QUERY LANGUAGE DDL CHAPTER 7
Lab Experment 1
DDL(Data Definition Language) : DDL or Data Definition Language actually consists of the
SQL commands that can be used to define the database schema. It simply deals with descriptions
of the database schema and is used to create and modify the structure of database objects in
database. DDL is used to create and destroy databases and database objects. These commands
will primarily be used by database administrators during the setup and removal phases of a
database project.
Examples of DDL commands:
CREATE – is used to create the database or its objects (like table, index, function, views,
store procedure and triggers).
DROP – is used to delete objects from the database.
ALTER-is used to alter the structure of the database.
COMMENT –is used to add comments to the data dictionary.
RENAME –is used to rename an object existing in the database
The Data Definition Language (DDL) is used to create and destroy databases and database
objects. These commands will primarily be used by database administrators during the setup and
removal phases of a database project.
Let's take a look at the structure and usage of four basic DDL commands:
1. CREATE 2. ALTER 3. DROP 4. RENAME
1. CREATE:
CREATE DATABASE: A Database is defined as a structured set of data. So, in SQL the very
first step to store the data in a well-structured manner is to create a database. The CREATE
DATABASE statement is used to create a new database in SQL.
Syntax: CREATE database <databasename>
Example: CREATE DATABASE AMUDB OR CREATE DATABASE AMUDB;
If you want the name of the database to be in different words, include them in square brackets.
Here is an example:
CREATE DATABASE AMUDB OR CREATE DATABASE [AMU DB];
When you have multiple databases in your SQL Schema, then before starting your operation, you
would need to select a database where all the operations would be performed.
USE statement: is the SQL statement that used to select any existing database in SQL schema.
Syntax: USE Database_name;
Example: if you want to work with AMUDB database, then you can execute the following
SQL command and start working with AMUDB database.
USE AMUDB;
4
STRACTURED QUERY LANGUAGE DDL CHAPTER 7
CREATE TABLE: We have learned above about creating databases. Now to store the data we
need a table to do that. The CREATE TABLE statement is used to create a table in SQL. We
know that a table comprises of rows and columns. So while creating tables we have to provide all
the information to SQL about the names of the columns, type of data to be stored in columns,
size of the data etc. Let us now dive into details on how to use CREATE TABLE statement to
create tables in SQL
CREATE TABLE table_name
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);
Where:-
table_name: name of the table.
Column1 name of the first column.
Data_type: Type of data we want to store in the particular column.
For example, int for integer data.
Size: Size of the data we can store in a particular column. For example if for a column we
specify the data_type as int and size as 10 then this column can store an integer number of
maximum 10 digits.
Example:
create table student (
stuId INT PRIMARY KEY, OR CONSTRAINT Student_stuId_pk PRIMARY KEY
(stuId),
lastName CHAR(20) NOT NULL,
firstName CHAR(20) NOT NULL,
sex CHAR(5),
);
Example 2.write sql query for employee relation schema
CREATE TABLE EMPLOYEE
( Fname VARCHAR(15) NOT NULL,
Minit CHAR,
Lname VARCHAR(15) NOT NULL,
Ssn CHAR(9) NOT NULL,
Bdate DATE,
Address VARCHAR(30),
Sex CHAR,
Salary DECIMAL(10,2),
Super_ssn CHAR(9),
Dno INT NOT NULL,
5
STRACTURED QUERY LANGUAGE DDL CHAPTER 7
2. ALTER
ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also
used to add and drop various constraints on the existing table and helps to change the width as
well as datatype of fields of existing relations
A. ALTER..ADD...: ADD is used to add columns into the existing table. Sometimes we
may require to add additional information, in that case we do not require to create the
whole database again, ADD comes to our rescue.
Syntax: ALTER TABLE table_name
ADD (Columnname_1 datatype (size),
Columnname_2 datatype,
…
Columnname_n datatype);
Example: alter table student ADD address varchar(23), department varchar(23);
B. ALTER..MODIFY...: This is used to change the width as well as data type of fields of
existing relations.
Syntax; ALTER TABLE relation_name MODIFY COLUMN (field_1
newdata_type(Size), field_2 newdata_type(Size),....field_newdata_type(Size));
Example; ALTER TABLE student ALTER COLUMN address char(32);
C. ALTER ..DROP...: This is used to remove any field of existing relations and table.
Syntax: ALTER TABLE relation_name
Example; ALTER TABLE student DROP column department,(from student table)
3. DROP is used to delete database and its objects. This is used to delete the structure of a
relation. It permanently deletes the records in the table
A. DROP DATABASE
DROP DATABASE <DATABASENAME>
B. DROP TABLE.
Syntax: DROP TABLE relation_name;
Example: DROP TABLE student;
4. RENAME: It is used to change database, table name and column name.
Renaming a column name
6
STRACTURED QUERY LANGUAGE DDL CHAPTER 7
Renaming a database
To change the name of a database, Transact-SQL provides sp_renamedb.
The formula used would be: EXEC sp_renamedb 'ExistingName', 'NewName';
The EXEC sp_renamedb expression is required.
The ExistingName factor is the name of the database that you want to rename.
The NewName factor is the name you want the database to have after renaming it.
Example: EXEC sp_renamedb 'existing db', 'new db';
7
STRACTURED QUERY LANGUAGE DDL CHAPTER 7