Professional Documents
Culture Documents
A data definition language (DDL) is a programming language that is used to define and alter the
structure of database items. Views, schemas, tables, and indexes are examples of database objects.
In certain cases, this word is referred to as data description language since it defines the columns and
records in a database table.
DDL is now incorporated into any formal language for expressing data in the database industry. It is,
nevertheless, considered a subset of SQL (Structured Query Language)
SQL frequently combines imperative verbs with ordinary English, such as phrases, to make database
changes. As a result, DDL does not appear in a SQL database as a distinct language, but it does
describe modifications to the database schema.
It deals with descriptions of the database schema to build and alter the structure of objects in a
database. Unlike data manipulation language (DML) commands, which are used to modify data, DDL
commands are used to change the database structure, such as adding new tables or objects, as well as
all of their properties (data type, table name, etc.).
CREATE, ALTER, DROP, and TRUNCATE are the most often used DDL in SQL queries.
1. CREATE
This command creates a new table using a specific syntax. The syntax for the CREATE statement is as
follows:
For example:
CREATE TABLE Employee (Employee Id INTEGER PRIMARY KEY, First name CHAR (50) NULL,
Last name CHAR (75) NOT NULL);
Every command preceding the semicolon is processed by the statement's required semicolon. The data type
is specified in this case by the string CHAR. DATE, NUMBER, and INTEGER are examples of other data
types.
2. ALTER
Altering a database table is done with the modify command. This command may be used to add new
columns to a database table, delete existing columns, and even modify the data type of the columns
concerned.
For example:
To create a constraint and enforce a unique value, we introduced a unique primary key to the table in this
example. On the Employee table, the constraint "employee pk" is a primary key.
3. DROP
Drop command is used to remove items from a database, such as tables, indexes, and
views. Because a DROP command cannot be reversed, once an item is deleted, it
cannot be recovered.
The syntax for the Drop statement is: DROP object type object name;
For example:
The TRUNCATE command, like DROP, is used to rapidly delete all records from a table. Unlike
DROP, which totally destroys a table, TRUNCATE keeps the table's whole structure so it may be
reused afterward.
For example:
Here all the extents of the Employee table are marked for deallocation in this example, so they're
deemed vacant for reuse.
• The databases for which the user's role has the OWNERSHIP or USAGE privilege
are listed in the SHOW DATABASES output.
• The schemas for which the user's role has the OWNERSHIP or USAGE privilege are
included in the SHOW SCHEMAS output.
• The tables for which the user's role has any privileges are included in the SHOW
TABLES output. The USAGE privilege on the parent database and schema must also
be granted to the role.
The following clauses can be used to control the output of most SHOW commands:
• The optional LIKE clause can be used to filter the list of items returned by name.
• The database object types provide an optional IN clause that may be used to limit the
command's scope to a single schema or database, or the whole account.
7. DESCRIBE
DESC<object_type> <object_name>
Data Manipulation Language Commands
INSERT is another most important data manipulation command in Structured Query Language, which allows
users to insert data in database tables.
Syntax of INSERT Command
INSERT INTO TABLE_NAME ( column_Name1 , column_Name2 , column_Name3 , .... column_NameN ) V
ALUES (value_1, value_2, value_3, .... value_N ) ;
Example 1: This example describes how to insert the record in the database table.
Let's take the following student table, which consists of only 2 records of the student.
INSERT INTO Student (Stu_id, Stu_Name, Stu_Marks, Stu_Age) VALUES (104, Anmol, 89, 19);
UPDATE DML Command
UPDATE is another most important data manipulation command in Structured Query Language, which
allows users to update or modify the existing data in database tables.
Syntax of UPDATE Command
UPDATE Table_name SET [column_name1= value_1, ….., column_nameN = value_N] WHERE CO
NDITION;
Here, 'UPDATE', 'SET', and 'WHERE' are the SQL keywords, and 'Table_name' is the name of the table
whose values you want to update.
UPDATE Student SET Stu_Marks = 80, Stu_Age = 21 WHERE Stu_Id = 103 AND Stu_Id = 2
02;
DELETE DML Command
DELETE is a DML command which allows SQL users to remove single or multiple existing records from
the database tables.
This command of Data Manipulation Language does not delete the stored data permanently from the
database. We use the WHERE clause with the DELETE command to select specific rows from the table.
Syntax of DELETE Command
DELETE FROM Table_Name WHERE condition;
DELETE FROM Student WHERE Stu_Marks > 70 ;
Data Control Language Commands
Data control language (DCL) is used to access the stored data. It is mainly used for
revoke and to grant the user the required access to a database. In the database, this
language does not have the feature of rollback.
It is a part of the structured query language (SQL).
It helps in controlling access to information stored in a database. It complements the
data manipulation language (DML) and the data definition language (DDL).
It is the simplest among three commands.
It provides the administrators, to remove and set database permissions to desired users
as needed.
These commands are employed to grant, remove and deny permissions to users for
retrieving and manipulating a database.
DCL commands are as follows:
1. GRANT
2. REVOKE
It is used to grant or revoke access permissions from any database user.
1. GRANT COMMAND
GRANT command gives user's access privileges to the database.
This command allows specified users to perform specific tasks.
Syntax:
GRANT <privilege list>
ON <relation name or view name>
TO <user/role list>;
Here,
privilege names are SELECT,UPDATE,DELETE,INSERT,ALTER,ALL
Object name is table name
user is the name of the user to whom we grant privileges
Example : GRANT Command
In the above example, user 'ABC' has been given permission to view and
modify the records in the 'employee' table.
2. REVOKE COMMAND
REVOKE command is used to cancel previously granted or denied permissions.
This command withdraw access privileges given with the GRANT command.
It takes back permissions from user.
Syntax:
REVOKE <privilege list>
ON <relation name or view name>
FROM <user name>;
Here,
privilege names are SELECT,UPDATE,DELETE,INSERT,ALTER,ALL
objectname is table name
user is the name of the user whose privileges are removing
Example : REVOKE Command
REVOKE UPDATE
ON employee
FROM ABC;
Transaction Control Language Commands(TCL)
COMMIT is the SQL command that is used for storing changes performed by a
transaction. When a COMMIT command is issued it saves all the changes since
last COMMIT or ROLLBACK.
ROLLBACK is the SQL command that is used for reverting changes performed by a transaction. When a
ROLLBACK command is issued it reverts all the changes since last COMMIT or ROLLBACK.
This command is used to save the data at a particular point temporarily, so that
whenever needed can be rollback to that particular point.
Syntax:
Savepoint A;
If on the above table savepoint is performed:
UPDATE NAME
SET NAME= ‘Rossie’
WHERE marks= 70;
SAVEPOINT A;
SQL Join statement is used to combine data or rows from two or more tables
based on a common field between them. Different types of Joins are as
follows:
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
The simplest Join is INNER JOIN.
A. INNER JOIN
The INNER JOIN keyword selects all rows from both the tables as long as the condition is satisfied.
This keyword will create the result-set by combining all rows from both the tables where the
condition satisfies i.e value of the common field will be the same.
Syntax:
SELECT table1.column1,table1.column2,table2.column1,.... FROM table1 INNER
JOIN table2 ON table1.matching_column = table2.matching_column;
Example Queries(INNER JOIN)
This query will show the names and age of students enrolled in different courses.
Teacher table
Class table
SELECT * FROM CUSTOMERS WHERE ID IN (SELECT ID FROM CUSTOMERS WHERE SALARY
4500);
Subqueries with the UPDATE Statement
1. Example
We have the CUSTOMERS_BKP table available which is backup of CUSTOMERS
table. The following example updates SALARY by 0.25 times in the CUSTOMERS
table for all the customers whose AGE is greater than or equal to 27.
2. TRIGGER_NAME: It creates or replaces an existing trigger with the Trigger_name. The trigger
name should be unique.
BEFORE | AFTER: It specifies when the trigger will be initiated i.e. before the ongoing event
or after the ongoing event.
INSERT | UPDATE | DELETE: These are the DML operations and we can use either of them
in a given trigger.
ON[TABLE_NAME]: It specifies the name of the table on which the trigger is going to be
applied.
FOR EACH ROW: Row-level trigger gets executed when any row value of any column
changes.
TRIGGER BODY: It consists of queries that need to be executed when the trigger is called
/* Create a few records in this table */
INSERT INTO student(id, first_name, last_name) VALUES(1,'Alvaro', 'Morte’);
INSERT INTO student(id, first_name, last_name) VALUES(2,'Ursula', 'Corbero');
INSERT INTO student(id, first_name, last_name) VALUES(3,'Itziar', 'Ituno’);
INSERT INTO student(id, first_name, last_name) VALUES(4,'Pedro', 'Alonso’);
INSERT INTO student(id, first_name, last_name) VALUES(5,'Alba', 'Flores');