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

DCL, DML, DQL commands

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

DCL, DML, DQL commands

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Date: 30-12-2024 Page No:

Week-1

(DDL, DML & TCL)


Types in MySQL:
1. Numeric Types:
 INT (Integer): Used for storing whole numbers.
 DECIMAL or NUMERIC: Used for storing fixed-point numbers.
 FLOAT and DOUBLE: Used for storing floating-point numbers.
2. String Types:
 CHAR and VARCHAR: Used for storing character strings.
CHAR has a fixed length, while VARCHAR has a variable
length.
 TEXT: Used for large text data.
3. Date and Time Types:
 DATE: Used for storing dates in the format 'YYYY-MM-DD'.
 TIME: Used for storing times in the format 'HH:MM:SS'.
 DATETIME: Used for storing both date and time in the format
'YYYY-MM-DD HH:MM:SS'.
 TIMESTAMP: Similar to DATETIME, but often used to store
the current timestamp.
SQL Commands:
o SQL commands are instructions. It is used to communicate with the
database. It is also used to perform specific tasks, functions, and queries
of data.
o SQL can perform various tasks like create a table, add data to tables, drop
the table, modify the table, set permission for users.

Types of SQL Commands


There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
Page No:

1. Data Definition Language (DDL)


o DDL changes the structure of the table like creating a table, deleting a
table, altering a table, etc.
o All the command of DDL are auto-committed that means it
permanently save all the changes in the database.
Here are some commands that come under DDL:
o CREATE
o ALTER
o DROP
o TRUNCATE

a. CREATE It is used to create a new table in the database.


Syntax:

CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,.]);


Example:
CREATE TABLE EMPLOYEE(Name VARCHAR(20), Email VARCHAR
(100), DOB DATE);
Output:

b. DROP: It is used to delete both the structure and record stored in the
table.Syntax DROP TABLE table_name;
Example
DROP TABLE EMPLOYEE;
Page No:

Output:

b. ALTER: It is used to alter the structure of the database. This change could be
either to modify the characteristics of an existing attribute or probably to add a
new attribute.
Syntax:
To add a new column in the table
ALTER TABLE table_name ADD column_name COLUMN-definition; To modify
existing column in the table:
ALTER TABLE table_name RENAME (column_definitions....);
To rename existing column name in the table:
ALTER TABLE table_name RENAME old_column_name to new-
column_name;
EXAMPLE
20ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR(20));
Output:

c. TRUNCATE: It is used to delete all the rows from the table and free the
space containing the table.
Syntax:
TRUNCATE TABLE table_name;

Example:
TRUNCATE TABLE EMPLOYEE;
Output:

Data Manipulation Language


o DML commands are used to modify the database. It is responsible for
all form of changes in the database.
o 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.
Here are some commands that come under DML:
o INSERT
o UPDATE
Page No:

o DELETE

a. INSERT: The INSERT statement is a SQL query. It is used to insert data


into the row of a table.
Syntax:
TABLE_NAME (col1, col2, col3,. col N)
VALUES (value1, value2, value3,...........valueN);
Or
TABLE_NAMEVALUES (value1, value2, value3, valueN);

For example:
INSERT INTO students (Name, Subject) VALUES ("Sonoo", "DBMS");
Output:

b. UPDATE: This command is used to update or modify the value of a


column in the table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_nameN = value N]
[WHERE CONDITION]

For example:
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
Output:

c. DELETE: It is used to remove one or more rows from a table.


Syntax:
DELETE FROM table_name [WHERE condition];
For example: DELETE FROM EMPLOYEE WHERE Name="Sonoo";
Page No:

Output:

2. Transaction Control Language:


TCL, or Transaction Control Language, is a subset of SQL used to manage
database transactions. TCL commands ensure data integrity by allowing you to
control when changes to the database are saved permanently or rolled back.
o Commit and rollback are the transaction control commands in SQL.
o All the commands that are executed consecutively, treated as a single unit
of work and termed as a transaction.

o If you want to save all the commands which are executed in a transaction,
then just after completing the transaction, you have to execute
the commit command. This command will save all the commands which
are executed on a table. All these changes made to the table will be saved
to the disk permanently.

o Whenever the commit command is executed in SQL, all the updations


which we have carried on the table will be uploaded to the server, and
hence our work will be saved.
o The rollback command is used to get back to the previous permanent
status of the table, which is saved by the commit command.
o Suppose, we have started editing a table and later thought that the
changes that we have recently carried out on a table are not required.
Then, in that case, we can roll back our transaction, which simply means
to get back to the previous permanent status of the table, which is saved
by the commit command.

Common TCL Commands in SQL


1. COMMIT
2. ROLLBACK
3. SAVEPOINT
1. COMMIT
The COMMIT command is used to save changes made during a transaction to
the database permanently:
Page No:

START TRANSACTION;-- SQL statements COMMIT;

Example:
SQL1;SET autocommit=0;
INSERT INTO emp VALUES(12, 'rani',
'2023-05-05'); COMMIT;
Output:

2. ROLLBACK
Example:
SQL1;SET autocommit=0;
0;START TRANSACTION;INSERT INTO emp VALUES(12, 'rani', '2023-05-05');
delete from emp where name='rani';
Output:

SELECT * FROM

sql_work.emp; Rollback;

Output:
Page No:

3. SAVEPOINT:

 The SAVEPOINT command is used to set a savepoint within a


transaction. A savepoint allows you to create a point to which you can
later roll back if needed, without affecting the entire transaction.

Syntax:

SAVEPOINT savepoint_name;

After setting a savepoint, you can later use the ROLLBACK TO statement to
roll back to that specific savepoint within the transaction.

ROLLBACK TO savepoint_name;

Example:

use sql_work;

SET autocommit=0;
INSERT INTO emp VALUES (12, 'rani', '2023-05-05');
savepoint A;
COMMIT;
delete from emp where name='rani';

Rollback to A;

SELECT * FROM sql_work.emp;

Rollback;

commit;
Output:

You might also like