0% found this document useful (0 votes)
16 views48 pages

1 4 SQL CONCEPT DDLDMLTCL Command

Uploaded by

belmontedenli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views48 pages

1 4 SQL CONCEPT DDLDMLTCL Command

Uploaded by

belmontedenli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

SQL CONCEPT

Introduction to SQL
Introduction to SQL
Structure Query Language(SQL) is a database query language
used for storing and managing data in Relational DBMS. SQL
was the first commercial language introduced for E.F
Codd's Relational model of database.

Today almost all RDBMS(MySql, Oracle, Infomix, Sybase, MS


Access) use SQL as the standard database query language. SQL
is used to perform all types of data operations in RDBMS.
SQL Command
SQL defines following ways to manipulate data stored in an RDBMS.

DDL: Data Definition Language


This includes changes to the structure of the table like creation of table,
altering table, deleting a table etc.

All DDL commands are auto-committed. That means it saves all the
changes permanently in the database.
SQL Command
DML: Data Manipulation Language
• DML commands are used for manipulating the data
stored in the table and not the table itself.

• DML commands are not auto-committed. It means


changes are not permanent to database, they can be
rolled back.
DML: Data Manipulation Language
TCL: Transaction Control Language
These commands are to keep a check on other
commands and their affect on the database. These
commands can annul changes made by other
commands by rolling the data back to its original state.
It can also make any temporary change permanent.
TCL: Transaction Control Language
DCL: Data Control Language
DQL: Data Query Language
DDL COMMAND
SQL: create command
create is a DDL SQL command used to create
a table or a database in relational database
management system.
Creating a Database
To create a database in RDBMS, create command is used. Following is
the syntax
Creating a Database
Example for creating Database

The above command will create a database named Test, which will be an
empty schema without any table.

To create tables in this newly created database, we can again use the create
command.
Creating a Table
create command can also be used to create tables. Now when we
create a table, we have to specify the details of the columns of the
tables too. We can specify the names and datatypes of various columns
in the create command itself.

Following is the syntax,


Creating a Table
create table command will tell the database system to create a new
table with the given table name and column information.
Creating a Table
If you are currently not logged into your database in which you want to
create the table then you can also add the database name along with
table name, using a dot operator .
Most commonly used datatypes for Table columns
DDL COMMAND
SQL: ALTER command
alter command is used for altering the table structure, such as,

• to add a column to existing table


• to rename any existing column
• to change datatype of any column or to modify its size.
• to drop a column from the table.
ALTER Command: Add a new Column
Using ALTER command we can add a column to any existing table.
Following is the syntax,
ALTER Command: Add multiple new Columns
Using ALTER command we can even add multiple new columns to any
existing table. Following is the syntax
ALTER Command: Modify an existing Column
ALTER command can also be used to modify data type of any existing
column. Following is the syntax,
ALTER Command: Rename a Column
Using ALTER command you can rename an existing column. Following is
the syntax,
ALTER Command: Drop a Column
ALTER command can also be used to drop or remove columns.
Following is the syntax,
SQL Truncate, Drop or Rename a Table
TRUNCATE command removes all the records from a table. But this
command will not destroy the table's structure. When we use
TRUNCATE command on a table its (auto-increment) primary key is also
initialized. Following is its syntax,
SQL Truncate, Drop or Rename a Table
DROP command
completely
removes a table
from the database.
This command will
also destroy the
table structure and
the data stored in
it. Following is its
syntax,
RENAME query
RENAME command is used to set a new name for any existing table.
Following is the syntax,
DML COMMAND
Using INSERT SQL command
Data Manipulation Language (DML) statements are used for
managing data in database. DML commands are not auto-
committed. It means changes made by DML command are not
permanent to database, it can be rolled back.
INSERT command
Insert command is used to insert data into a table. Following is its
general syntax,
INSERT command
example
INSERT command
Insert value into only specific columns
We can use the INSERT command to insert values for only some
specific columns of a row. We can specify the column names along with
the values to be inserted like this,
Using UPDATE SQL command
UPDATE command is used to update any record of data in a table.
Following is its general syntax,

WHEREis used to add a condition to any SQL query, we will soon


study about it in detail.
Lets take a sample table student,
Using UPDATE SQL command
Updating Multiple Columns
We can also update values of multiple columns using a
single UPDATE statement.
DELETE command
DELETE command is used to delete data from a table.
Following is its general syntax,
Delete a particular Record from a Table
In our student table if we want to delete a single record, we can use the
WHERE clause to provide a condition in our DELETE statement.
DML COMMAND

Commit, Rollback and


Savepoint SQL commands
• Transaction Control Language(TCL) commands are used to manage
transactions in the database.

• These are used to manage the changes made to the data in a table by
DML statements. It also allows statements to be grouped together
into logical transactions.
COMMIT command
COMMIT command is used to permanently save any transaction into
the database.
When we use any DML command like INSERT, UPDATE or DELETE, the
changes made by these commands are not permanent, until the
current session is closed, the changes made by these commands can be
rolled back.
Following is commit command's syntax,
ROLLBACK command
• This command restores the database to last commited state. It is also used with
SAVEPOINT command to jump to a savepoint in an ongoing transaction.
• If we have used the UPDATE command to make some changes into the database,
and realise that those changes were not required, then we can use the ROLLBACK
command to rollback those changes, if they were not commited using the
COMMIT command.
• Following is rollback command's syntax,
SAVEPOINT command
• SAVEPOINT command is used to temporarily save a transaction so
that you can rollback to that point whenever required.

• Following is savepoint command's syntax,

• In short, using this command we can name the different states of our
data in any table and then rollback to that state using the ROLLBACK
command whenever required.
Using Savepoint and Rollback
Using Savepoint and Rollback
Lets use some SQL queries on the above table and see the results.
Using Savepoint and Rollback
NOTE: SELECT statement is used to show the data stored in the table.
The resultant table will look like,
Now let's use the ROLLBACK command to roll back the
state of data to the savepoint B.
Now let's again use the ROLLBACK command to roll back the
state of data to the savepoint A

You might also like