0% found this document useful (0 votes)
9 views12 pages

Types of SQL Commands

Very Good

Uploaded by

Tanveer Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
9 views12 pages

Types of SQL Commands

Very Good

Uploaded by

Tanveer Abbas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

Types of SQL Commands

1. Data Definition Language (DDL)


o All the command of DDL are auto-committed that means it
permanently save all the changes in the database.

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

Syntax:

<
1. CREATE TABLE TABLE_NAME (COLUMN_NAMES DATATYPES [ ...]);
In above statement, TABLE_NAME is the name of the table,
COLUMN_NAMES is the name of the columns and DATATYPES is used to
define the type of data.

Example:

1. CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(


100), DOB DATE);
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax: To DROP a table permanently from memory

1. DROP TABLE table_name [cascade constraint];


The cascade constraint is an optional parameter which is used for tables
which have foreign keys that reference the table being dropped. If cascade
constraint is not specified and used attempt to drop a table that has records in
a child table, then an error will occur. So by using cascade constraints, all
child table foreign keys are dropped.

Example

1. DROP TABLE EMPLOYEE;


c. 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.

Following are the list of modifications that can be done using ALTER
command.

o With the use of ALTER commands we can add or drop one or more
columns form existing tables.
o Increase or decrease the existing column width by changing the data
type
o Make an existing mandatory column to optional.
o Enable or disable the integrity constraints in a table. We can also add,
modify or delete the integrity constraints from a table.
o We can also specify a default value for existing column in a table.
Adding new columns in Table:

With the use of ALTER table command we can add new columns existing
table.

Syntax: To add a new column in the table

1. ALTER TABLE table_name ADD column_name column-definition;


In the above syntax, where table_name corresponds to the name of the table,
column-definition corresponds to the valid specifications for a column name
and data type.
EXAMPLE:

1. ALTER TABLE STU_DETAILS ADD (ADHAR_NUM VARCHAR2 (15));


Syntax: To ADD a multiple column from a table.

ALTER TABLE table_name ADD column_name1, column_name2;

Example:

1. ALTER TABLE STU_DETAILS ADD ADHAR_NUM, NAME;


Adding constraints in a Table:

You can also add constraints to an existing table. For example: If you forget to
add a primary key constraint to a table during creation, you can add it using
the ALTER TABLE statement.

Syntax: To ADD a constraint from a table.

1. ALTER TABLE table_name ADD (column_name column-definition CONST


RAINT constraint_name);
Example:

1. ALTER TABLE STU_DETAILS ADD (CONSTRAINT PK_STU_DETAILS


PRIMARY KEY (STU_ID);
Following points should be kept in mind while adding new
columns/relationships to existing tables.

o No need for parentheses if you add only one column or constraints.


o You can add a column at any time if NULL is not specified. You can
add a new column with NOT NULL if the table is empty.
Modifying Column using ALTER:

With the use of ALTER table we can modify column and constraint in the
existing table. These statements can increase or decrease the column widths
and changing a column from mandatory to optional.

Syntax:

1. ALTER TABLE table_name MODIFY (column definitions....);


Example:

1. ALTER TABLE STU_DETAILS MODIFY (ADHAR_NUM VARCHAR2 (20


));
SQL does not allow column widths to be reduced even if all column values are
of valid length. So the values should be set to NULL to reduce the width of the
columns. It is also not possible to reduce the width of the ADHAR_NUM
column from 18 to 12 even if all values in the ADHAR_NUM column are less
than 12 characters, unless all al values in the name column are null. You can
modify the column form NULL to NOTNULL constraints if there is no record
in that column in the table.

Example:

1. ALTER TABLE STU_DETAILS MODIFY (ADHAR_NUM VARCHAR2 (20


) NOT NULL);
Drop column and constraints using ALTER

You cannot only modify columns but you can also drop them entirely if it is no
longer required in a table. Using drop statement in alter command we can
also remove the constraints form the table.

Syntax: To drop a column from a table.

1. ALTER TABLE table_name DROP COLUMN column_name;


Example:

1. ALTER TABLE STU_DETAILS DROP COLUMN ADHAR_NUM;


Syntax: To drop a multiple column from a table.

1. ALTER TABLE table_name DROP COLUMN column_name1, column_name


2;
Example:

1. ALTER TABLE STU_DETAILS DROP COLUMN ADHAR_NUM, NAME;


Syntax: To drop a constraint from a table.

1. ALTER TABLE table_name DROP CONSTRAINT constraint_name;


Example:

1. ALTER TABLE STU_DETAILS DROP CONSTRAINT FK_STU_DETAILS


;
Following points should be kept in mind while deleting columns/associations:

o You cannot drop columns in a table. If you want to drop a column from
a table, the deletion is permanent so you cannot undo the column if you
accidentally drop the wrong column.
o You cannot drop a column whose username is SYS.
o If you want to drop a primary key column unless you drop the foreign
keys that belong to it then use cascade keyword for this.
Example:

1. ALTER TABLE STU_DETAILS DROP PRIMARY KEY CASCADE;

o You can also enable or disable the key constraint in a table. It can be
done in various situations such as: when loading large amount of data
into table, performing batch operations, migrating the organizations
legacy data.
Example: To disable constraint

1. ALTER TABLE STU_DETAILS DISABLE CONSTRAINT FK_STU_DETA


ILS;
Example: To Enable constraint

1. ALTER TABLE STU_DETAILS ENABLE CONSTRAINT FK_STU_DETAI


LS;

o Instead of dropping a column in a table, we can also make the column


unused and drop it later on. It makes the response time faster. After a
column has been marked as unused, the column and all its contents are
no longer available and cannot be recovered in the future. The unused
columns will not be retrieved using Select statement
Example:

1. ALTER TABLE STU_DETAILS SET UNUSED COLUMN ADHAR_NUM;


RENAMING TABLE
SQL provides the facility to change the name of the table by using a ALTER
TABLE statement.

Syntax:

1. ALTER TABLE <OLD_TABLENAME> Rename to <NEW_TABLENAME>


;
Example:

1. ALTER TABLE STU_NAME Rename to STUDENT_NAME;


d. TRUNCATE: It is used to delete all the rows from the table and free the
space containing the table.

Syntax:

1. TRUNCATE TABLE table_name;


Example:

1. TRUNCATE TABLE EMPLOYEE;


e. Rename: It is used to rename the table.

Syntax:

1. Rename <OLD_TABLENAME> to <NEW_TABLENAME>;


In the above syntax, Rename is a command, <OLD_TABLENAME> is the
name of the table and <NEW_TABLENAME> is the name that you have
changed.

Example:

1. Rename STU_NAME to STUDENT_NAME;

2. 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.
Following are the some commands that come under DML:
a. INSERT: The INSERT statement is a SQL query. It is used to insert data
into the row of a table. To insert a new row into a table you must be your on
schema or INSERT privilege on the table.

Following are the list of points should be considered while inserting the data
into tables.

o SQL uses all the columns by default if you do not specify the column
name while inserting a row.
o The number of columns in the list of column name must match the
number of values that appear in parenthesis after the word "values".
o The data type for a column and its corresponding value must match.
Syntax: To add row in a table

1. INSERT INTO TABLE_NAME


2. (col1, col2, col3,.... col N)
3. VALUES (value1, value2, value3, .... valueN);
Or

1. INSERT INTO TABLE_NAME


2. VALUES (value1, value2, value3, .... valueN);
In the above syntax, TABLE_NAME is the name of the table in which the
data will be inserted. The (col1, col2, col3, col N) are optional and name of the
columns in which values will be inserted. The value1 corresponds to the value
of be inserted in col1 and similarly value2 corresponds to the value of be
inserted in col2 and so on.
For example:

1. INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS");


Syntax: To add multiple rows in a table

1. INSERT INTO TABLE_NAME


2. (col1, col2, col3,.... col N)
3. VALUES (value1, value2, value3, .... valueN), (value1, value2, value3, .... value
N);
For example:

1. INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS"), (


"Raman", "DBMS"), ("Priya", "DBMS");
b. UPDATE: This command is used to update or modify the value of a column
in the table.

Syntax: To update record in a table

1. UPDATE table_name SET [column_name1= value1,...column_nameN = value


N] [WHERE CONDITION]
In the above syntax, table_name is the name of the table, the column_name is
the name of column in the table to be modified, and value1 corresponds to the
valid SQL values. The "WHERE" is a condition that restricts the rows
updated for which the specified condition is true. If condition is not specified
is not defined then SQL updates all the rows in the table. It contains
comparison and logical operators etc.

The following the list of points should be remembered while executing the
UPDATE statement.

o It references only one table.


o In the SET clause atleast one column must be assigned an expression for
the update statement,
o In the where clause you could also give multiple conditions for update
statement.
For example:

1. UPDATE students
2. SET User_Name = 'Sonoo'
3. WHERE Student_Id = '3'
c. DELETE: It is used to remove one or more row from a table. To delete
rows from the table, it must be in your schema or you must have delete
privilege.

Syntax: To Delete a record from table

1. DELETE FROM table_name [WHERE condition];


In the above syntax, condition is used in the where clause to filter the records
that are actually being removed. You can remove zero or more rows from a
table. If you do not use where condition then DELETE statement will remove
all the rows from the table. You can also use one or multiple conditions in
WHERE clause.

For example:

1. DELETE FROM javatpoint


2. WHERE Author="Sonoo";
d. SELECT: This is the same as the projection operation of relational algebra.
It is used to select the attribute based on the condition described by WHERE
clause.

Syntax: It is used for retrieve the records from table

1. SELECT expressions
2. FROM TABLES
3. WHERE conditions;
For example:

1. SELECT emp_name
2. FROM employee
3. WHERE age > 20;

3. Data Control Language


DCL commands are used to grant and take back authority from any database
user.

Following are the some commands that come under DCL:


Syntax:

1. GRANT <obj_priv> ON <obj_name> To <username>;


In the above syntax, obj_priv> is the DML statement like Insert, Delete ,
update and Select and <obj_name> is a table, view etc. and username is the
name of the authorized user.

Example

1. GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHE


R_USER;
b. Revoke: It is used to take back permissions from the user.

Syntax:

1. REVOKE <obj_priv> ON <obj_name> FROM <username>;


In the above syntax, obj_priv> is the DML statement like Insert, Delete ,
update and Select and <obj_name> is a table, view etc. and username is the
name of the user from whom the permission is revoked.

Example

1. REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;

4. Transaction Control Language


Transactions are atomic i.e. either every statement succeeds or none of
statement succeeds. There are number of Transaction Control statements
available that allow us to control this behavior. These statements ensure data
consistency. TCL commands can only use with DML commands like INSERT,
DELETE and UPDATE only.

These operations are automatically committed in the database that's why they
cannot be used while creating tables or dropping them.

Following are the some commands that come under TCL:

a. Commit: Commit command is used to save all the transactions to the


database. It makes your changes permanent and ends the transaction.

Syntax: To permanently save the changes

1. COMMIT;
Example:

1. DELETE FROM CUSTOMERS


2. WHERE AGE = 25;
3. COMMIT;
b. Rollback: Rollback command is used to undo transactions that have not
already been saved to the database. Rollback also serves to end the current
transaction and begin a new one.
Consider a Situation where you have completed a series of INSERT, UPDATE
or DELETE statements but have not yet explicitly committed them and yiu
encounter a problem such as computer failure, then SQL will automatically
rollback any uncommitted work.

Syntax: To remove the changes

1. ROLLBACK;
Example:

1. DELETE FROM CUSTOMERS


2. WHERE AGE = 25;
3. ROLLBACK;
c. SAVEPOINT: It is used to roll the transaction back to a certain point
without rolling back the entire transaction.

Syntax:

1. SAVEPOINT SAVEPOINT_NAME;
In the above syntax, SAVEPOINT_NAME is the name given to savepoint.

To selectively ROLLBACK a group of statements within a large transaction


use the following command is used.

1. Rollback TO <save_point_name>
Example:

1. DELETE FROM CUSTOMERS WHERE AGE = 15;


2. SAVEPOINT A;
3. DELETE FROM CUSTOMERS WHERE AGE = 35;
4. ROLLBCAK TO A;

You might also like