Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS Exp 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Experiment No-3

Aim: Create and populate database using Data Definition Language (DDL) and DML
Commands for your specified System.

Theory: DDL- Data Definition Language (DDL) statements are used to define the database
structure or schema. Data Definition Language understanding with database schemas and
describes how the data should consist in the database, therefore language statements like
CREATE TABLE or ALTER TABLE belongs to the DDL. DDL is about "metadata".

DDL includes commands such as CREATE, ALTER and DROP statements. DDL is used to
CREATE, ALTER OR DROP the database objects (Table, Views, Users).

Data Definition Language (DDL) are used different statements :

1. CREATE - to create objects in the database


2. ALTER - alters the structure of the database
3. DROP - delete objects from the database
4. TRUNCATE - remove all records from a table, including all spaces allocated for the
records are removed
5. RENAME - rename an object

Use Following schema to perform the experiment.

Student (stuId, lastName, firstName, major, credits)

Faculty (facId, name, department, rank)

Class (classNumber, facId, schedule, room)

Enroll (classNumber, stuId, grade)

NOTE: Underlined Text: Primary Key, Italic Text: Forign Key

● DDL

1) CREATE
a) To create a database

CREATE DATABASE dbname;


Eg-
CREATE DATABASE UniversityData;
b) To select existing database
Use dbname;
Eg- USE UniversityData;

1
c) To create a Table
The create table command defines each column of the table uniquely. Each column
has minimum of three attributes.

● Name
● Data type
● Size (column width).

Each table column definition is a single clause in the create table syntax. Each table
column definition is separated from the other by a comma. Finally, the SQL
statement is terminated with a semicolon.
Syntax: Create table table name( fieldname1 datatype(),fieldname2 datatype()...);

The Structure of Create Table Command

2
Note: Create Tables for an extra entities and relationships

2) ALTER
By The use of ALTER TABLE Command we can modify our exiting table.

Adding New Columns

Syntax:
ALTER TABLE <table_name>
ADD (<NewColumnName> <Data_Type>(<size>),......n)

Example: Add a new column,cTitle, to our Classtable


ALTER TABLE Class ADD cTitle CHAR(30);

The schema of the Class table would then be:


Class(classNumber,facId,schedule,room,cTitle)

Dropping a Column from the Table

Syntax:

ALTER TABLE <table_name> DROP COLUMN <column_name>

Example:

Example: Drop the cTitle column from the Class table


ALTER TABLE Class DROP COLUMN cTitle;

This command will drop particular column

If we want to add, drop, or change a constraint, we can use the same ALTER TABLE command. For example,
if we created the Class table and neglected to make facIda foreign key in Class, we could add the constraint at any
time by writing:
ALTER TABLE Class ADD CONSTRAINT Class_facId_fk FOREIGN KEY (facId)REFERENCES Faculty
(facId)ON DELETE NO ACTION);
We could drop an existing named constraint using the ALTER TABLE command.
For example, to drop the check condition on the creditsattribute of Student that we created earlier, we could write:
ALTER TABLE Student DROP CONSTRAINT Student_credits_cc;

Modifying Existing Column

Syntax:
ALTER TABLE <table_name> MODIFY <column_name> NewDataType>(<NewSize>)

Example:

ALTER TABLE Student MODIFY stuld Varchar(20);

3
Renaming Existing Table

Syntax:

ALTER TABLE <table_name> RENAME <new_table_name>

Example:

ALTER TABLE student RENAME new_student;

ALTER TABLE new_student RENAME student; (To revert the change)

3) RENAME
Syntax:
RENAME TABLE <OldTableName> TO <NewTableName>

Example:
RENAME table visiting TO visiting_staff;

4) DROP
Syntax:
DROP TABLE <table_name>

Example:
DROP TABLE visiting_staff;

5) TRUNCATE
Syntax:
TRUNCATE TABLE <Table_name>

Example:
TRUNCATE TABLE visiting_staff;

6- SHOW
To check available databases and tables

Syntax

SHOW DATABASES;
SHOW TABLES;

7- DESCRIBE

4
To obtain information about table structure or query execution plans.

DESCRIBE < table_name>


DESC < table_name>

Example-

DESC Student;

● Apply Integrity Constraints for the specified system


MySQL CONSTRAINT is used to define rules to allow or restrict what values can be stored
in columns. The purpose of inducing constraints is to enforce the integrity of a database.
MySQL CONSTRAINTS are used to limit the type of data that can be inserted into a table.

MySQL CONSTRAINTS can be classified into two types - column level and table level.

The column level constraints can apply only to one column where as table level constraints are
applied to the entire table.

MySQL CONSTRAINT is declared at the time of creating a table.

MySQL CONSTRAINTs are :

● NOT NULL
● UNIQUE
● PRIMARY KEY
● FOREIGN KEY
● CHECK
● DEFAULT
● AUTO INCREMENT

CONSTRAINT DESCRIPTION

In MySQL NOT NULL constraint allows to specify that a column can not
contain any NULL value. MySQL NOT NULL can be used to CREATE and
NOT NULL ALTER a table.

Eg. lastName CHAR(20) NOT NULL,


firstName CHAR(20) NOT NULL

5
The UNIQUE constraint in MySQL does not allow to insert a duplicate value
in a column. The UNIQUE constraint maintains the uniqueness of a column
UNIQUE
in a table. More than one UNIQUE column can be used in a table.

Eg. CONSTRAINT Class_schedule_room_uk UNIQUE (schedule, room)


A PRIMARY KEY constraint for a table enforces the table to accept unique
data for a specific column and this constraint creates a unique index for
PRIMARY KEY
accessing the table faster.

Eg: CONSTRAINT Faculty_facId_pk PRIMARY KEY (facId));


A FOREIGN KEY in MySQL creates a link between two tables by one
specific column of both tables. The specified column in one table must be a
FOREIGN KEY PRIMARY KEY and referred by the column of another table known as
FOREIGN KEY.

Eg. CONSTRAINT Class_facId_fk FOREIGN KEY (facId) REFERENCES Faculty


(facId) ON DELETE NO ACTION);
A CHECK constraint controls the values in the associated column. The
CHECK constraint determines whether the value is valid or not from a
CHECK
logical expression.

Eg: CONSTRAINT Student_credits_cc CHECK ((credits >=0) AND (credits < 150);
In a MySQL table, each column must contain a value ( including a NULL).
While inserting data into a table, if no value is supplied to a column, then
DEFAULT
the column gets the value set as DEFAULT.

credits SMALLINT DEFAULT 0 CHECK ((credits >=0) AND (credits < 150))
Auto-increment allows a unique number to be generated automatically when
AUTO a new record is inserted into a table. Often this is the primary key field that
INCREMENT we would like to be created automatically every time a new record is
inserted.

Eg. Field name int AUTO_INCREMENT PRIMARY KEY

● DML(Data Manipulation Language)

A data manipulation language (DML) is a family of computer languages including commands


permitting users to manipulate data in a database. This manipulation involves inserting data
into database tables, retrieving existing data, deleting data from existing tables and modifying
existing data. DML is mostly incorporated in SQL databases.

Use following database to perform the experiment.


Database:

6
1) INSERT

This command adds one or more records to a database table.

Syntax

INSERT INTO "table_name" ("column1", "column2", ...)


VALUES ("value1", "value2", ...);

Example

1) insert into Student (stuld, lastname, firstname, major, credits) values(‘S1001’,'Smith',Tom',


'History', 90);

2) SELECT

The SELECT statement is used to select data from a database.

Syntax

7
SELECT * FROM table_name;

Example-
Select * from Student;

3) UPDATE
The UPDATE statement is used to update existing records in a table.

Syntax
UPDATE table_name
SET column1=value1, column2=value2,...
WHERE some_column=some_value;

Example-
1) update Student set Name='JANEE' where stuID=S1020;

4) DELETE

This command removes one or more records from a table according to specified conditions.

Syntax: DELETE
FROM table_name
WHERE some_column=some_value;

Example-1) delete from Student


where stuID=S1020;

You might also like