0% found this document useful (0 votes)
20 views

SQL-DDL and DML

The document discusses Structured Query Language (SQL) and its components like Data Definition Language (DDL), Data Manipulation Language (DML), Transaction Control Language (TCL), and Data Control Language (DCL). It describes SQL commands like CREATE, ALTER, DROP, SELECT, INSERT, UPDATE, DELETE, COMMIT, ROLLBACK, GRANT, REVOKE.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

SQL-DDL and DML

The document discusses Structured Query Language (SQL) and its components like Data Definition Language (DDL), Data Manipulation Language (DML), Transaction Control Language (TCL), and Data Control Language (DCL). It describes SQL commands like CREATE, ALTER, DROP, SELECT, INSERT, UPDATE, DELETE, COMMIT, ROLLBACK, GRANT, REVOKE.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

BCSE302P – Database

Management Systems
Structured Query Language
Dr. K.P. Vijayakumar
VIT Chennai
Introduction
 SQL stands for Structured Query Language.
 SEQUEL (Structured English Query
Language)
 Allows to create, access and manipulate
databases
 American National Standards Institute
(ANSI) and International Organization for
Standardization (ISO)
 SQL 86 or SQL1
 SQL 92 or SQL2
 SQL 1999 – SQL3
 SQL 2003, SQL 2006, SQL 2008, SQL 2011,
SQL 2016, SQL 2019
Schema, Catalog
 Database - Table
 Schema
 Structure of the table
 Structure of data
 Catalog
 contains a special schema
 Information of all the schemas
 Meta data, Data Dictionary
 data about data
Data Types
 Data types defines the type of value to
be stored in the table.
 SQL data types can be broadly divided
into following categories.
 Numeric data types
 Date and Time data types
 Character and String data types
 Bit string -BIT(n)
 Boolean
 Miscellaneous data types – clob, blob, xml, cursor,
table etc.
Data Types
 Numeric data types
 Number(n), Number(n,m), Decimal(n,m),
int, float
 n – no. of digits in a number. It ranges from
1 to 38
 m - no. of digits to the right of the decimal
point . It ranges from -84 to 127
 Date and Time data types
 DATE –YYYY- MM-DD,
 TO_DATE ()
Data Types
 Character and String data types
 Char, Varchar,Varchar2
 Char - 2000 bytes
 Varchar, varchar2 - 4000 bytes
 Bit string -BIT(n)
 Boolean
 Miscellaneous data types – clob, blob,
xml, cursor, table etc.
DDL, DML, TCL & DCL
 Data Definition Language
 Data Manipulation Language
 Transaction Control Language
 Data Control Language
DDL, DML, TCL & DCL
SQL Commands

DDL DML TCL DCL


CREATE SELECT COMMIT REVOKE
ALTER INSERT ROLLBACK REVOKE
DROP UPDATE SAVEPOINT

RENAME DELETE

TRUNCATE
DDL, DML, TCL & DCL
 DDL Command :
 CREATE
 Syntax
 CREATE TABLE table_name ( column1 datatype, column2 datatype, column3
datatype, .... );
 Example
 Create table Student(Name varchar2(20), Regno number,Year number);

Name Regno Year


DDL, DML, TCL & DCL
 ALTER
 To add new column
 To modify existing column
 To drop existing column
 To rename the table
 To rename the column
DDL, DML, TCL & DCL
 ALTER
 To add new column

 Syntax:
Department
 ALTER TABLE table_name ADD column_name datatype;

 Example
 Alter table Student add dept varchar2(10);
DDL, DML, TCL & DCL
 ALTER
 To modify existing column

 Syntax:
 ALTER TABLE table_name MODIFY column_name datatype;
Varchar2(10)

 Example
 Alter table Student modify regno varchar2(10);
DDL, DML, TCL & DCL
 ALTER
 To drop existing column

 Syntax:
 ALTER TABLE table_name DROP COLUMN column_name ;

 Example
 Alter table Student drop column year;
DDL, DML, TCL & DCL
Student
 ALTER
 To rename the table

 Syntax:
 ALTER TABLE table_name RENAME TO new_table_name ;

Students
 Example
 Alter table Student RENAME TO Students;
DDL, DML, TCL & DCL
 ALTER
 To rename existing column

 Syntax:
 ALTER TABLE table_name RENAME COLUMN Old_column_name To
New_column_name;

 Example
 Alter table Student RENAME COLUMN regno TO registration;
DDL, DML, TCL & DCL
 DROP – table structure will be removed
 Syntax
 DROP TABLE table_name;
 Example
 DROP TABLE Student;

 TRUNCATE – table structure will be retained, deletes only rows


 Syntax
 TRUNCATE TABLE table_name;
 Example
 Truncate TABLE Student;
DDL, DML, TCL & DCL
DML Commands:
 Insert – To insert data into the table
 Syntax:
 Insert into Table_name Values(value1, value2, …);
 Insert into Table_name (Column1, Column2, …,) Values(value1, value2);
 Insert into Table_name Values(Column1, Column2, …);
 Example:
 insert into Student values('Vijay', '22BCE1001', 2);
 insert into Student (Name, Regno) values('Kumar', '22BCE1002');
 Insert into student values('&Name','&Regno',&year);
DDL, DML, TCL & DCL
 DML Commands:
 Select – To extract data from the table
 Syntax:
 Select * from Table_name;
 Select Column1, Column2, …, from Table_name;
 Select * from Table_name where condition;
 Select Column1, Column2, …, from Table_name where condition;
 Example:
 Select * from Student;
 Select Name, Dept from Student;
 Select * from Student where name=‘Ram’;
 Select Name from Student where Regno=’21BCE1002’;
DDL, DML, TCL & DCL
 DML Commands:
 UPDATE
 Syntax:
 UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE
condition;
 Example:
 UPDATE Student SET Name=‘Ram’ WHERE Regno=‘22BCE1001’;
DDL, DML, TCL & DCL
 DML Commands:
 DELETE
 Syntax:
 DELETE FROM table_name ;
 DELETE FROM table_name WHERE condition;
 Example:
 DELETE from Student;
 DELETE from Student where Name=‘Kumar’;
DDL, DML, TCL & DCL
 TCL Commands:
 COMMIT
 commits the current transaction, making its changes permanent.
 Syntax:
 COMMIT;
 Example:
 COMMIT;
DDL, DML, TCL & DCL
 TCL Commands:
 ROLLBACK
 rolls back the current transaction, canceling its changes.
 Syntax:
 ROLLBACK;
 Example:
 ROLLBACK;
DDL, DML, TCL & DCL
Privilege
 DCL Commands: SELECT
 GRANT INSERT
UPDATE
 Syntax:
DELETE
 GRANT privilege(s) ON object TO user;
REFERENCES
 Example:
ALTER
 GRANT ALL ON Student TO Vijay;
INDEX
 GRANT SELECT, INSERT, UPDATE ON Student TO Vijay;
ALL
 GRANT SELECT ON Student TO public;
DDL, DML, TCL & DCL
 DCL Commands:
 REVOKE
 Syntax:
 REVOKE privilege(s) ON object FROM user;
 Example:
 REVOKE ALL ON Student FROM Vijay;
 REVOKE SELECT, INSERT, UPDATE ON Student FROM Vijay;
 REVOKE SELECT ON Student FROM public;

You might also like