DBMS Exp 3
DBMS Exp 3
DBMS Exp 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).
● DDL
1) CREATE
a) To create a database
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()...);
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.
Syntax:
ALTER TABLE <table_name>
ADD (<NewColumnName> <Data_Type>(<size>),......n)
Syntax:
Example:
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;
Syntax:
ALTER TABLE <table_name> MODIFY <column_name> NewDataType>(<NewSize>)
Example:
3
Renaming Existing Table
Syntax:
Example:
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.
Example-
DESC Student;
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.
● 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.
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 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.
6
1) INSERT
Syntax
Example
2) SELECT
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;