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

SQL Commands

The document provides an overview of SQL commands categorized into five main types: Data Definition Language (DDL), Data Query Language (DQL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL). Each category includes commands for creating, modifying, querying, and controlling access to database objects and data. Key commands such as SELECT, INSERT, UPDATE, DELETE, and others are explained with their syntax and usage.

Uploaded by

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

SQL Commands

The document provides an overview of SQL commands categorized into five main types: Data Definition Language (DDL), Data Query Language (DQL), Data Manipulation Language (DML), Data Control Language (DCL), and Transaction Control Language (TCL). Each category includes commands for creating, modifying, querying, and controlling access to database objects and data. Key commands such as SELECT, INSERT, UPDATE, DELETE, and others are explained with their syntax and usage.

Uploaded by

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

SQL Commands

1.Data Definition Languagae(DCL)


DDL or Data Definition Language actually consists of
the SQL commands that can be used to defining, altering,
and deleting database structures such as tables, indexes,
and schemas. It simply deals with descriptions of the database
schema and is used to create and modify the structure of database
objects in the database.
Command Description Syntax

Create database or its objects


CREATE TABLE table_name (column1
CREATE (table, index, function, views,
data_type, column2 data_type, ...);
store procedure, and triggers)
Command Description Syntax

Delete objects from the


DROP DROP TABLE table_name;
database

Alter the structure of the ALTER TABLE table_name ADD


ALTER
database COLUMN column_name data_type;

Remove all records from a table,


TRUNCATE including all spaces allocated for TRUNCATE TABLE table_name;
the records are removed

Add comments to the data COMMENT 'comment_text' ON TABLE


COMMENT
dictionary table_name;

Rename an object existing in the RENAME TABLE old_table_name TO


RENAME
database new_table_name;

2. Data Query Language (DQL) in SQL


DQL statements are used for performing queries on the data
within schema objects. The purpose of the DQL Command is to get
some schema relation based on the query passed to it. This
command allows getting the data out of the database to perform
operations with it. When a SELECT is fired against a table or tables
the result is compiled into a further temporary table, which is
displayed or perhaps received by the program.
Command Description Syntax

It is used to retrieve data from the SELECT column1, column2, ...FROM table_name
SELECT
database WHERE condition;
3. Data Manipulation Language (DML) in SQL
The SQL commands that deal with the manipulation of data present
in the database belong to DML or Data Manipulation Language and
this includes most of the SQL statements. It is the component of the
SQL statement that controls access to data and to the database.
Basically, DCL statements are grouped with DML statements.
Command Description Syntax

INSERT INTO table_name (column1, column2, ...) VALUES


INSERT Insert data into a table
(value1, value2, ...);

Update existing data UPDATE table_name SET column1 = value1, column2 =


UPDATE
within a table value2 WHERE condition;

Delete records from a


DELETE DELETE FROM table_name WHERE condition;
database table

LOCK Table control concurrency LOCK TABLE table_name IN lock_mode;

Call a PL/SQL or JAVA


CALL CALL procedure_name(arguments);
subprogram

EXPLAIN Describe the access path


EXPLAIN PLAN FOR SELECT * FROM table_name;
PLAN to data

4. Data Control Language (DCL) in SQL


DCL (Data Control Language) includes commands such
as GRANT and REVOKE which mainly deal with
the rights, permissions, and other controls of the database
system. These commands are used to control access to data in the
database by granting or revoking permissions.
Command Description Syntax

Assigns new privileges to a user


GRANT privilege_type [(column_list)]
account, allowing access to
GRANT ON [object_type] object_name TO user
specific database objects,
[WITH GRANT OPTION];
actions, or functions.

Removes previously granted


REVOKE [GRANT OPTION FOR]
privileges from a user account,
privilege_type [(column_list)] ON
REVOKE taking away their access to
[object_type] object_name FROM user
certain database objects or
[CASCADE];
actions.

5. Transaction Control Language (TCL) in SQL


Transactions group a set of tasks into a single execution unit. Each
transaction begins with a specific task and ends when all the tasks in
the group are successfully completed. If any of the tasks fail, the
transaction fails. Therefore, a transaction has only two
results: success or failure. We can explore more
about transactions here.
Common TCL Commands
Command Description Syntax

BEGIN BEGIN TRANSACTION


Starts a new transaction
TRANSACTION [transaction_name];

Saves all changes made during the


COMMIT COMMIT;
transaction

Undoes all changes made during the


ROLLBACK ROLLBACK;
transaction
Command Description Syntax

Creates a savepoint within the current


SAVEPOINT SAVEPOINT savepoint_name;
transaction

Some of The Most Important SQL Commands


• SELECT - extracts data from a database
To Return data from the Customers table:

SELECT column1, column2, ...


FROM table_name;

To Return all the columns from the Customers table:

SELECT * FROM Customers;

• UPDATE - updates data in a database


UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

DELETE - deletes data from a database


DELETE FROM table_name WHERE condition;

Note: Be careful when deleting records in a table! Notice the WHERE clause in
the DELETE statement. The WHERE clause specifies which record(s) should be deleted. If you
omit the WHERE clause, all records in the table will be deleted!

• INSERT INTO - inserts new data into a database


It is possible to write the INSERT INTO statement in two ways:

1.Specify both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

2. If you are adding values for all the columns of the table, you do not need to specify the column
names in the SQL query. However, make sure the order of the values is in the same order as the
columns in the table. Here, the INSERT INTO syntax would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);
• CREATE DATABASE - creates a new database
CREATE DATABASE databasename;

SHOW DATABASES;

• ALTER DATABASE - modifies a database


• DROP DATABASE: drop an existing database
DROP DATABASE databasename;

• CREATE TABLE - creates a new table


cREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);

The column parameters specify the names of the columns of the table.

The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer,
date, etc.).

• ALTER TABLE - modifies a table


The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.

To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype;

To delete a column in a table

ALTER TABLE table_name


DROP COLUMN column_name;

To rename a column in a table, use the following syntax:

ALTER TABLE table_name


RENAME COLUMN old_name to new_name;

• TRUNCATE TABLE- delete the data inside the table


TRUNCATE TABLE table_name;

• DROP TABLE - deletes a table


DROP TABLE table_name;
• CREATE INDEX - creates an index (search key)
Creates an index on a table. Duplicate values are allowed:

CREATE INDEX index_name


ON table_name (column1, column2, ...);

• DROP INDEX - deletes an index


ALTER TABLE table_name
DROP INDEX index_name;

You might also like