You are on page 1of 36

CS2072 Database Engineering Laboratory

&
CS2082 Database Management Systems
Laboratory
(LAB3)
Prof. Sambit Bakshi
Computer Science and Engineering,
National Institute of Technology, Rourkela.
bakshisambit@nitrkl.ac.in
DATA DEFINATION LANGUAGE
• DATA DEFINATION LANGUAGE (DDL) changes the structure of the table
like creating a table, deleting a table, altering a table, etc.

• All the command of DDL are auto-committed that means it permanently save all
the changes in the database.

• Some commands that come under DDL: CREATE, ALTER, DROP,


RENAME, COMMENT and TRUNCATE.
ALTER
• ALTER TABLE is used to add, delete/drop or modify columns in the existing table.

1. ALTER TABLE – ADD

2. ALTER TABLE – DROP

3. ALTER TABLE – MODIFY


ALTER TABLE – ADD

Syntax :
Example:

ALTER TABLE table_name ALTER TABLE Student ADD


ADD Columnname_1 datatype, AGE INT,
COURSE varchar(40);
Columnname_2 datatype,

Columnname_n datatype;
ALTER TABLE – DROP

Example:
Syntax :
ALTER TABLE table_name ALTER TABLE Student
DROP COLUMN column_name; DROP COLUMN COURSE;
ALTER TABLE – MODIFY

Syntax : Example:

ALTER TABLE table_name ALTER TABLE Student


ALTER COLUMN column_name datatype; ALTER COLUMN COURSE varchar(20);
DROP

• DROP is used to delete a whole database or just a table.

Syntax: Example:
DROP object object_name
DROP TABLE STUDENT;
DROP TABLE table_name;
DROP DATABASE database_name;
TRUNCATE

• TRUNCATE statement is used to mark the extents of a table for deallocation (empty
for reuse).

Example:
Syntax:
TRUNCATE TABLE table_name; TRUNCATE TABLE Student_details;
DROP TRUNCATE DELETE

• DROP is a DDL command • TRUNCATE is a DDL • DELETE is DML


that destroys the table command that helps to command used to delete
structure and the data remove the records of a one or more tuples of a
stored in it. table. table.
• DROP helps to remove • TRUNCATE helps to • DELETE command can
the records of the table, remove all the records either delete all the rows
table structure and to from the table. in one go or can delete
remove the database rows one by one using
from the system. Where Clause.
RENAME TABLE
• Rename table name, column name of an existing table, index name
can done by using the system stored procedure sp_rename.

Syntax:
EXEC sp_rename ‘OldTableName', ‘NewTableName';
RENAME COLUMN

Syntax:
EXEC sp_rename ‘TableName.OldColumnName', ‘NewColumnName', 'COLUMN';

Example:

EXEC sp_rename 'STUDENT.Name', 'S_name', 'COLUMN';


COMMENT

• Comments can be written in the following three formats:

1. Single-line comments
2. Multi-line comments
3. Inline comments
Single line comments

Syntax: Example:
-- single line comment SELECT * FROM STUDENTS --WHERE Name = 'Raj';
Multi-line comments

Example:
Syntax:
/* multi-line comment*/ /* SELECT * FROM Students;
SELECT * FROM STUDENT_DETAILS;
SELECT * FROM Orders; */
SELECT * FROM Articles;
Inline comments

Example:
Syntax:
SELECT * FROM /* Customers; */ SELECT * FROM Students;
SELECT * FROM /*
STUDENT_DETAILS;
SELECT * FROM Orders;
SELECT * FROM */ Articles;
DATA MANIPULATION LANGUAGE
• DATA MANIPULATION LANGUAGE (DML) commands are used to modify
the database. It is responsible for all form of changes in the database.
• The command of DML is not auto-committed that means it can't
permanently save all the changes in the database. They can be rollback.
• Some commands that come under DML: INSERT, UPDATE, DELETE, MERGE,
CALL , EXPLAIN PLAN and LOCK TABLE.
INSERT

• The INSERT INTO statement of SQL is used to insert a new row in a table.

Syntax: Example:

INSERT INTO Student


INSERT INTO table_name (ROLL_NO, NAME, Age)
(column1, column2, column3,..) VALUES (5,’PRATIK’,19);
VALUES ( value1, value2, value3,..);
INSERT INTO Student
VALUES (5,’HARSH’,’WESTBENGAL’,’XYZ’,19);
INSERT INTO table_name
VALUES (value1, value2, value3,…);
UPDATE

Syntax: Example:

UPDATE student SET subject = 'a', name = 'b’


UPDATE table_name WHERE roll_no = 5;
SET column1 = value1, column2 = value2,...
WHERE condition;
DELETE

• DELETE Statement in SQL is used to delete existing records from a table.

Syntax: Example:

DELETE from student WHERE name = 'b';


DELETE FROM table_name
WHERE some_condition;
DATA CONTROL LANGUAGE
• DATA CONTROL LANGUAGE (DCL) commands are used to grant and take
back authority from any database user.

• DCL includes commands such as GRANT and REVOKE which mainly deal with
the rights, permissions, and other controls of the database system.

• Some commands that come under DCL: GRANT and REVOKE .


GRANT

• GRANT statement is used to grant privileges to a user account.

Syntax: Example:

GRANT SELECT ON student TO test_user;


GRANT privileges_names ON table_name TO user;
REVOKE

• REVOKE statement is used to revoke some or all of the privileges which have been
granted to a user in the past.

Syntax: Example:

REVOKE SELECT ON student FROM test_user;


REVOKE privileges_names
ON table_name FROM user;
DATA QUERY LANGUAGE
• DATA QUERY LANGUAGE (DQL) statements are used for performing queries
on the data within schema objects.

• DQL includes SELECT commands.


SELECT

Syntax: Example:

SELECT column1,column2 SELECT ROLL_NO, NAME, AGE


FROM Student;
FROM table_name

SELECT * FROM table_name; SELECT * FROM Student;


DATA TYPES
• Data types are used to represent the nature of the data that can be stored in the
database table.

• Data type define :


• Value Represent : INT, CHAR, DATE, VARCHAR
• Space or Memory Occupy : Fixed and Variable.

• Data Type mainly Classified into :


1. Numeric Data Type
2. String Data Type
3. Date and Time Data Type
Syntax: Example:

CREATE TABLE TABLE_NAME CREATE TABLE STUDENT_TB


( (
ATTRIBUTE1 DATATYPE, Roll_No int primary key,
Student_Name varchar(50),
ATTRIBUTE2 DATATYPE, Subject varchar(50)
…, )
)
NUMERIC DATA TYPE
DATA TYPE DISCRIPTION STORAGE

BIT Integer that can be 0, 1, or NULL


TINYINT Range Between 0 to 255 1 Byte

SMALLINT Range Between -32,768 to 32,767 2 Byte

INT Range Between -2,147,483,648 and 2,147,483,647 4 Byte

BIGINT Range Between 8 Byte


-9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
DECIMAL(P,S) Fixed precision and scale numbers. 5-17 Byte

NUMERIC(P,S) Allows numbers from -10^38 +1 to 10^38 –1. 5-17 Byte

FLOAT(N) Floating precision number data from -1.79E + 308 to 1.79E + 308. 4 or 8 Byte
float(24) holds a 4-byte field and float(53) holds an 8-byte field. Default value
of n is 53.
REAL Floating precision number data from -3.40E + 38 to 3.40E + 38 4 Byte
STRING DATA TYPE
DATA TYPE DISCRIPTION MAX SIZE

CHAR(N) Fixed width character string 8,000 characters

VARCHAR(N) Variable width character string 8,000 characters

VARCHAR(MAX) Variable width character string 1,073,741,824 characters

TEXT Variable width character string 2GB of text data

NCHAR Fixed width Unicode string 4,000 characters

NVARCHAR Variable width Unicode string 4,000 characters

NVARCHAR(MAX) Variable width Unicode string 536,870,912 characters

NTEXT Variable width Unicode string 2GB of text data

BINARY(N) Fixed width binary string 8,000 bytes

VARBINARY Variable width binary string 8,000 bytes

VARBINARY(MAX) Variable width binary string 2GB

IMAGE Variable width binary string 2GB


DATE AND TIME DATA TYPE
DATA TYPE DISCRIPTION STORAGE

DATETIME Date: January 1, 1753, through December 31, 9999 8 Bytes


Time: 00:00:00 through 23:59:59.997
DATETIME2 Date : 0001-01-01 through 9999-12-31 6-8 Bytes
Time: 00:00:00 through 23:59:59.9999999
Default Format : YYYY-MM-DD hh:mm:ss[.nnnnnnn]
SMALLDATETIME Date : 1900-01-01 through 2079-06-06 4 Bytes
Time : 00:00:00 through 23:59:59
DATE 0001-01-01 through 9999-12-31 3 Bytes
Default Format : YYYY-MM-DD
TIME 00:00:00.0000000 through 23:59:59.9999999 3-5 Bytes
Default Fromat: hh:mm:ss[.nnnnnnn]
DATETIMEOFFSET Date : 0001-01-01 through 9999-12-31 8-10 Bytes
Time : 00:00:00 through 23:59:59.9999999
Default Format : YYYY-MM-DD hh:mm:ss[.nnnnnnn]
TIMESTAMP Stores a unique number that gets updated every time a row gets created
or modified.
Function For Date and Time Data Type
1. DATEDIFF : Return the difference between two date values

2. GETDATE : Return the current database system date and time

3. GETUTCDATE : Return the current UTC date and time

4. SYSDATETIME : Return the date and time of the SQL Server

5. DAY : Return the day of the month for a date

6. MONTH : Return the month part of a date

7. YEAR : Return the year part of a date


DATEDIFF

Syntax: Example:

SELECT
DATEDIFF(interval, date1, date2) DATEDIFF(year, '2017/08/25',
'2011/08/25')
Interval : year, month, day, quarter
GETDATE

Syntax: Example:

SELECT GETDATE();
GETDATE()

• Similar format for GETUTCDATE(), SYSDATETIME()


DAY

Syntax: Example:

DAY(date) SELECT DAY('2017/08/25') AS Day;

• Similar format for MONTH(), YEAR()


How to calculate age from date of birth

1. Get Current Date using GetDate() having format YYYY-MM-DD.

2. Get Date of Birth from table having format YYYY-MM-DD.

3. Covert Both Date in YYYYMMDD format.

4. Calculate years difference in current_date and date_of_birth.

5. Update year difference in table.


Example:

UPDATE Table_name
SET year_difference =
DATEDIFF(YEAR, date_of_birth , GETDATE())
WHERE date_of_birth IN (SELECT date_of_birth FROM Table_name)

SELECT CONVERT(varchar(10),CONVERT(DATE,dob,105),105) FROM dateofbirth


THANK YOU

You might also like