You are on page 1of 14

K. J.

Somaiya Polytechnic, Mumbai-77

Batch No: C2

Enrollment No.: FCOG19123

Experiment No: 02

Experiment Name: Creating and Executing DDL Commandsin SQL &


Apply various integrity constarints on table
DBMS Lab Experiment No.2

Experiment No.:02

Experiment Name: Creating and Executing DDL Commandsin SQL & Apply
various integrity constarints on table

Course Outcome:

O18RA61.5 Create and Execute SQL and NoSQL queries

Theory:

What are DDL commands

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 the database. 

List of DDL commands


CREATE

DROP

ALTER

TRUNCATE

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

Example of each with explanation of command

CREATE – is used to create the database or its objects (like table, index, function,
views, store procedure and triggers).

EXAMPLE
CREATE TABLE Students
(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),
);

DROP – is used to delete objects from the database.

EXAMPLE
DROP DATABASE student_data;

ALTER-is used to alter the structure of the database.

EXAMPLE
ALTER TABLE Student ADD (AGE number(3),COURSE varchar(40));

ALTER TABLE Student MODIFY COURSE varchar(20);

TRUNCATE–is used to remove all records from a table, including all spaces
allocated for the records are removed.

EXAMPLE
TRUNCATE TABLE Student_details;

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

Your output

CREATE COMMAND.

ALTER COMMAND.

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

DROP COMMAND.

TRUNCATE COMMAND

What are constraints


Department of Computer Engineering DBMS Sem-IV/Dec-April 2020
DBMS Lab Experiment No.2

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.

List of constraints

NOT NULL

UNIQUE

PRIMARY KEY

FOREIGN KEY

CHECK

DEFAULT

Example of each with explanation of command

NOT NULL - Ensures that a column cannot have a NULL value.

EXAMPLE

CREATE TABLE Persons

(
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255) NOT NULL,
    Age int
);

UNIQUE - Ensures that all values in a column are different

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

EXAMPLE

CREATE TABLE Persons

(
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    UNIQUE (ID)
);

PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each


row in a table

EXAMPLE

CREATE TABLE Persons

(
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    PRIMARY KEY (ID)
);

FOREIGN KEY - Prevents actions that would destroy links between tables

EXAMPLE

CREATE TABLE Orders

(
    OrderID int NOT NULL,
    OrderNumber int NOT NULL,
    PersonID int,
    PRIMARY KEY (OrderID),
    FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

CHECK - Ensures that the values in a column satisfies a specific condition

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

EXAMPLE

CREATE TABLE Persons

(
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    CHECK (Age>=18)
);

DEFAULT - Sets a default value for a column if no value is specified

EXAMPLE

CREATE TABLE Persons

(
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    City varchar(255) DEFAULT 'Sandnes'
);

YOUR OUTPUT

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

NOT NULL.

UNIQUE KEY.

PRIMARY KEY.

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

CHECK.

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

CONCLUSION : THUS WE HAVE CREATED VARIOUS DDL COMMANDS IN SQL AND


APPLIED VARIOUS INTEGRITY CONSTRAINTS ON TABLES.

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020


DBMS Lab Experiment No.2

Conclusion:

Department of Computer Engineering DBMS Sem-IV/Dec-April 2020

You might also like