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 :
ALTER TABLE table_name
ADD Columnname_1 datatype,
Columnname_2 datatype,
…
Columnname_n datatype;
Example:
ALTER TABLE Student ADD
AGE INT,
COURSE varchar(40);
ALTER TABLE – DROP
Syntax :
ALTER TABLE table_name
DROP COLUMN column_name;
Example:
ALTER TABLE Student
DROP COLUMN COURSE;
ALTER TABLE – MODIFY
Syntax :
ALTER TABLE table_name
ALTER COLUMN column_name datatype;
Example:
ALTER TABLE Student
ALTER COLUMN COURSE varchar(20);
DROP
• DROP is used to delete a whole database or just a table.
Syntax:
DROP object object_name
DROP TABLE table_name;
DROP DATABASE database_name;
Example:
DROP TABLE STUDENT;
TRUNCATE
• TRUNCATE statement is used to mark the extents of a table for deallocation (empty
for reuse).
Syntax:
TRUNCATE TABLE table_name;
Example:
TRUNCATE TABLE Student_details;
DROP TRUNCATE DELETE
• DROP is a DDL command
that destroys the table
structure and the data
stored in it.
• TRUNCATE is a DDL
command that helps to
remove the records of a
table.
• DELETE is DML
command used to delete
one or more tuples of a
table.
• DROP helps to remove
the records of the table,
table structure and to
remove the database
from the system.
• TRUNCATE helps to
remove all the records
from the table.
• DELETE command can
either delete all the rows
in one go or can delete
rows one by one using
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
Syntax:
/* multi-line comment*/
Example:
/* SELECT * FROM Students;
SELECT * FROM STUDENT_DETAILS;
SELECT * FROM Orders; */
SELECT * FROM Articles;
Inline comments
Syntax:
SELECT * FROM /* Customers; */
Example:
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.
• The INSERT INTO statement of SQL is used to insert a new row in a table.
Syntax:
INSERT INTO table_name
(column1, column2, column3,..)
VALUES ( value1, value2, value3,..);
INSERT INTO table_name
VALUES (value1, value2, value3,…);
INSERT
Example:
INSERT INTO Student
(ROLL_NO, NAME, Age)
VALUES (5,’PRATIK’,19);
INSERT INTO Student
VALUES (5,’HARSH’,’WESTBENGAL’,’XYZ’,19);
UPDATE
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2,...
WHERE condition;
Example:
UPDATE student SET subject = 'a', name = 'b’
WHERE roll_no = 5;
DELETE
• DELETE Statement in SQL is used to delete existing records from a table.
Syntax:
DELETE FROM table_name
WHERE some_condition;
Example:
DELETE from student WHERE name = 'b';
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:
GRANT privileges_names ON table_name TO user;
Example:
GRANT SELECT ON student TO test_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:
REVOKE privileges_names
ON table_name FROM user;
Example:
REVOKE SELECT ON student FROM test_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:
SELECT column1,column2
FROM table_name
SELECT * FROM table_name;
Example:
SELECT ROLL_NO, NAME, AGE
FROM Student;
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:
CREATE TABLE TABLE_NAME
(
ATTRIBUTE1 DATATYPE,
ATTRIBUTE2 DATATYPE,
…,
)
Example:
CREATE TABLE STUDENT_TB
(
Roll_No int primary key,
Student_Name varchar(50),
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
-9,223,372,036,854,775,808 and 9,223,372,036,854,775,807
8 Byte
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.
float(24) holds a 4-byte field and float(53) holds an 8-byte field. Default value
of n is 53.
4 or 8 Byte
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
Time: 00:00:00 through 23:59:59.997
8 Bytes
DATETIME2 Date : 0001-01-01 through 9999-12-31
Time: 00:00:00 through 23:59:59.9999999
Default Format : YYYY-MM-DD hh:mm:ss[.nnnnnnn]
6-8 Bytes
SMALLDATETIME Date : 1900-01-01 through 2079-06-06
Time : 00:00:00 through 23:59:59
4 Bytes
DATE 0001-01-01 through 9999-12-31
Default Format : YYYY-MM-DD
3 Bytes
TIME 00:00:00.0000000 through 23:59:59.9999999
Default Fromat: hh:mm:ss[.nnnnnnn]
3-5 Bytes
DATETIMEOFFSET Date : 0001-01-01 through 9999-12-31
Time : 00:00:00 through 23:59:59.9999999
Default Format : YYYY-MM-DD hh:mm:ss[.nnnnnnn]
8-10 Bytes
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:
DATEDIFF(interval, date1, date2)
Interval : year, month, day, quarter
Example:
SELECT
DATEDIFF(year, '2017/08/25',
'2011/08/25')
GETDATE
Syntax:
GETDATE()
• Similar format for GETUTCDATE(), SYSDATETIME()
Example:
SELECT GETDATE();
DAY
Syntax:
DAY(date)
• Similar format for MONTH(), YEAR()
Example:
SELECT DAY('2017/08/25') AS Day;
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

225523359001djcj4_DBMS_LAB_THEORY_DML.pdf

  • 1.
    CS2072 Database EngineeringLaboratory & CS2082 Database Management Systems Laboratory (LAB3) Prof. Sambit Bakshi Computer Science and Engineering, National Institute of Technology, Rourkela. bakshisambit@nitrkl.ac.in
  • 2.
    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.
  • 3.
    ALTER • ALTER TABLEis used to add, delete/drop or modify columns in the existing table. 1. ALTER TABLE – ADD 2. ALTER TABLE – DROP 3. ALTER TABLE – MODIFY
  • 4.
    ALTER TABLE –ADD Syntax : ALTER TABLE table_name ADD Columnname_1 datatype, Columnname_2 datatype, … Columnname_n datatype; Example: ALTER TABLE Student ADD AGE INT, COURSE varchar(40);
  • 5.
    ALTER TABLE –DROP Syntax : ALTER TABLE table_name DROP COLUMN column_name; Example: ALTER TABLE Student DROP COLUMN COURSE;
  • 6.
    ALTER TABLE –MODIFY Syntax : ALTER TABLE table_name ALTER COLUMN column_name datatype; Example: ALTER TABLE Student ALTER COLUMN COURSE varchar(20);
  • 7.
    DROP • DROP isused to delete a whole database or just a table. Syntax: DROP object object_name DROP TABLE table_name; DROP DATABASE database_name; Example: DROP TABLE STUDENT;
  • 8.
    TRUNCATE • TRUNCATE statementis used to mark the extents of a table for deallocation (empty for reuse). Syntax: TRUNCATE TABLE table_name; Example: TRUNCATE TABLE Student_details;
  • 9.
    DROP TRUNCATE DELETE •DROP is a DDL command that destroys the table structure and the data stored in it. • TRUNCATE is a DDL command that helps to remove the records of a table. • DELETE is DML command used to delete one or more tuples of a table. • DROP helps to remove the records of the table, table structure and to remove the database from the system. • TRUNCATE helps to remove all the records from the table. • DELETE command can either delete all the rows in one go or can delete rows one by one using Where Clause.
  • 10.
    RENAME TABLE • Renametable 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';
  • 11.
    RENAME COLUMN Syntax: EXEC sp_rename‘TableName.OldColumnName', ‘NewColumnName', 'COLUMN'; Example: EXEC sp_rename 'STUDENT.Name', 'S_name', 'COLUMN';
  • 12.
    COMMENT • Comments canbe written in the following three formats: 1. Single-line comments 2. Multi-line comments 3. Inline comments
  • 13.
    Single line comments Syntax:Example: -- single line comment SELECT * FROM STUDENTS --WHERE Name = 'Raj';
  • 14.
    Multi-line comments Syntax: /* multi-linecomment*/ Example: /* SELECT * FROM Students; SELECT * FROM STUDENT_DETAILS; SELECT * FROM Orders; */ SELECT * FROM Articles;
  • 15.
    Inline comments Syntax: SELECT *FROM /* Customers; */ Example: SELECT * FROM Students; SELECT * FROM /* STUDENT_DETAILS; SELECT * FROM Orders; SELECT * FROM Articles;*/
  • 16.
    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.
  • 17.
    • The INSERTINTO statement of SQL is used to insert a new row in a table. Syntax: INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2, value3,..); INSERT INTO table_name VALUES (value1, value2, value3,…); INSERT Example: INSERT INTO Student (ROLL_NO, NAME, Age) VALUES (5,’PRATIK’,19); INSERT INTO Student VALUES (5,’HARSH’,’WESTBENGAL’,’XYZ’,19);
  • 18.
    UPDATE Syntax: UPDATE table_name SET column1= value1, column2 = value2,... WHERE condition; Example: UPDATE student SET subject = 'a', name = 'b’ WHERE roll_no = 5;
  • 19.
    DELETE • DELETE Statementin SQL is used to delete existing records from a table. Syntax: DELETE FROM table_name WHERE some_condition; Example: DELETE from student WHERE name = 'b';
  • 20.
    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 .
  • 21.
    GRANT • GRANT statementis used to grant privileges to a user account. Syntax: GRANT privileges_names ON table_name TO user; Example: GRANT SELECT ON student TO test_user;
  • 22.
    REVOKE • REVOKE statementis used to revoke some or all of the privileges which have been granted to a user in the past. Syntax: REVOKE privileges_names ON table_name FROM user; Example: REVOKE SELECT ON student FROM test_user;
  • 23.
    DATA QUERY LANGUAGE •DATA QUERY LANGUAGE (DQL) statements are used for performing queries on the data within schema objects. • DQL includes SELECT commands.
  • 24.
    SELECT Syntax: SELECT column1,column2 FROM table_name SELECT* FROM table_name; Example: SELECT ROLL_NO, NAME, AGE FROM Student; SELECT * FROM Student;
  • 25.
    DATA TYPES • Datatypes 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
  • 26.
    Syntax: CREATE TABLE TABLE_NAME ( ATTRIBUTE1DATATYPE, ATTRIBUTE2 DATATYPE, …, ) Example: CREATE TABLE STUDENT_TB ( Roll_No int primary key, Student_Name varchar(50), Subject varchar(50) )
  • 27.
    NUMERIC DATA TYPE DATATYPE 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 -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 8 Byte 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. float(24) holds a 4-byte field and float(53) holds an 8-byte field. Default value of n is 53. 4 or 8 Byte REAL Floating precision number data from -3.40E + 38 to 3.40E + 38 4 Byte
  • 28.
    STRING DATA TYPE DATATYPE 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
  • 29.
    DATE AND TIMEDATA TYPE DATA TYPE DISCRIPTION STORAGE DATETIME Date: January 1, 1753, through December 31, 9999 Time: 00:00:00 through 23:59:59.997 8 Bytes DATETIME2 Date : 0001-01-01 through 9999-12-31 Time: 00:00:00 through 23:59:59.9999999 Default Format : YYYY-MM-DD hh:mm:ss[.nnnnnnn] 6-8 Bytes SMALLDATETIME Date : 1900-01-01 through 2079-06-06 Time : 00:00:00 through 23:59:59 4 Bytes DATE 0001-01-01 through 9999-12-31 Default Format : YYYY-MM-DD 3 Bytes TIME 00:00:00.0000000 through 23:59:59.9999999 Default Fromat: hh:mm:ss[.nnnnnnn] 3-5 Bytes DATETIMEOFFSET Date : 0001-01-01 through 9999-12-31 Time : 00:00:00 through 23:59:59.9999999 Default Format : YYYY-MM-DD hh:mm:ss[.nnnnnnn] 8-10 Bytes TIMESTAMP Stores a unique number that gets updated every time a row gets created or modified.
  • 30.
    Function For Dateand 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
  • 31.
    DATEDIFF Syntax: DATEDIFF(interval, date1, date2) Interval: year, month, day, quarter Example: SELECT DATEDIFF(year, '2017/08/25', '2011/08/25')
  • 32.
    GETDATE Syntax: GETDATE() • Similar formatfor GETUTCDATE(), SYSDATETIME() Example: SELECT GETDATE();
  • 33.
    DAY Syntax: DAY(date) • Similar formatfor MONTH(), YEAR() Example: SELECT DAY('2017/08/25') AS Day;
  • 34.
    How to calculateage 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.
  • 35.
    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
  • 36.