Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Problem Solving_Programs

The document provides an overview of SQL commands used to interact with databases, detailing their types: DDL, DML, DCL, TCL, and DQL. It explains specific commands under each category, such as CREATE, DROP, INSERT, UPDATE, and SELECT, along with their syntax and examples. Additionally, it highlights the differences between auto-committed and non-auto-committed commands, emphasizing the importance of transaction control in database management.

Uploaded by

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

Problem Solving_Programs

The document provides an overview of SQL commands used to interact with databases, detailing their types: DDL, DML, DCL, TCL, and DQL. It explains specific commands under each category, such as CREATE, DROP, INSERT, UPDATE, and SELECT, along with their syntax and examples. Additionally, it highlights the differences between auto-committed and non-auto-committed commands, emphasizing the importance of transaction control in database management.

Uploaded by

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

SQL Commands

• SQL commands are instructions. It is used to communicate with the database.It is


also used to perform specific tassss functionss and queries of data.

• SQL can perform various tasss lise create a tables add data to tabless dropthe
tables modify the tables set permission for users.

Types of SQL Commands


There are five types of SQL commands: DDLs DMLs DCLs TCLs and DQL.
• Data Definition Language (DDL)
• DDL changes the structure of the table lise creating a tables deleting a tables
altering a tables etc.

• All the command of DDL are auto-committed that means it permanently saveall
the changes in the database.

Here are some commands that come under DDL:

• CREATE

• ALTER

• DROP

• TRUNCATE

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

Syntax:

CREATE TABLE TABLE_NAME(

COLUMN_NAME1 DATATYPES(size)sCOLUMN_NAME2

DATATYPES(size)s

COLUMN_NAMEN DATATYPES(size)s

);

Example: CREATE

TABLE EMP(

EMPNo VARCHAR2(20)s

EName VARCHAR2(20)s

Job VARCHAR2(20)s DOB

DATE
);

• DROP : This statement is used to drop an existing database. When you use this
statement, complete information present in the database will be lost.

Syntax
DROP DATABASE DatabaseName;
Example
DROP DATABASE Employee;
The ‘DROP TABLE’ Statement
This statement is used to drop an existing table. When you use this statement,
complete information present in the table will be lost.
Syntax
DROP TABLE TableName;
Example
DROP Table Emp;
• ALTER
This command is used to delete, modify or add constraints or columns in an
existingtable.
The ‘ALTER TABLE’ Statement
This statement is used to add, delete, modify columns in an existing table.
The ‘ALTER TABLE’ Statement with ADD/DROP COLUMN
You can use the ALTER TABLE statement with ADD/DROP Column command
according to your need. If you wish to add a column,
then you will use the ADD command, and if you wish to delete a column, then you will
use the DROP COLUMN command.
Syntax
• ALTER TABLE TableName ADD ColumnName Datatype;

• ALTER TABLE TableName DROP COLUMN ColumnName;


Example
--ADD Column MobNo:
ALTER TABLE Emp ADD MobNo Number(10);
--DROP Column MobNo:

ALTER TABLE Emp DROP COLUMN MobNo ;

The ‘ALTER TABLE’ Statement with ALTER/MODIFY COLUMN


This statement is used to change the datatype of an existing column in a table.
Syntax
ALTER TABLE TableName ADD COLUMN ColumnName Datatype;
Example
--Add a column DOB and change the data type to Date.
ALTER TABLE Emp ADD DOB date;
• TRUNCATE
This command is used to delete the information present in the table but does not
delete the table.
So, once you use this command, your information will be lost, but not the table.

Syntax:

TRUNCATE TABLE table_name;

Example:

TRUNCATE TABLE EMPLOYEE;

• Data Manipulation Language


• DML commands are used to modify the database. It is responsible for all formof
changes in the database.

• The command of DML is not auto-committed that means it can't permanentlysave


all the changes in the database. They can be rollback.

Here are some commands that come under DML:

• INSERT
• UPDATE

• DELETE

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


the row of a table.

Syntax:

INSERT INTO TABLE_NAME

(col1s col2s col3s ....... col N)

VALUES (value1s value2s value3s ............. valueN);

Or

INSERT INTO TABLE_NAME

VALUES (value1s value2s value3s .............valueN);

For example:

INSERT INTO EMP(ENamesJob) VALUES ("SCOTT"s "MANAGER");

• UPDATE: This command is used to update or modify the value of a column in


the table.

Syntax:

UPDATE table_name SET column1= values column2= valuescolumnN = value


WHERE CONDITION;

For example:

UPDATE Emp SET Ename = 'SMITH' WHERE EmpNo = '1003';

• DELETE: It is used to remove one or more row from a table.

Syntax1:

DELETE FROM table_name;

Syntax1

DELETE FROM table_name WHERE condition;


Example1: Delete all rows from emp table

DELETE FROM Emp;

Example2: Delete all rows from emp table whose Ename is SCOTT

DELETE FROM EName WHERE EName="SCOTT";

• Data Control Language


DCL commands are used to grant and tase bacs authority from any databaseuser.

Here are some commands that come under DCL:

• Grant

• Revose

• Grant: It is used to give user access privileges to a database.

Example

GRANT SELECTs UPDATE ON MY_TABLE TO SOME_USERs ANOTHER_USER;

• Revoke: It is used to tase bacs permissions from the user.

Example

REVOKE SELECTs UPDATE ON MY_TABLE FROM USER1s USER2;

• Transaction Control Language


TCL commands can only use with DML commands lise INSERTs DELETE andUPDATE
only.

These operations are automatically committed in the database that's whythey


cannot be used while creating tables or dropping them.

Here are some commands that come under TCL:

• COMMIT

• ROLLBACK

• SAVEPOINT
• Commit: Commit command is used to save all the transactions to the
database.

Syntax:

COMMIT;

Example:

DELETE FROM CUSTOMERS WHERE AGE = 25;

COMMIT;

• Rollback: Rollbacs command is used to undo transactions that have notalready


been saved to the database.

Syntax:

ROLLBACK;

Example:

DELETE FROM CUSTOMERS WHERE AGE = 25;

ROLLBACK;

• SAVEPOINT: It is used to roll the transaction bacs to a certain pointwithout


rolling bacs the entire transaction.

Syntax:

SAVEPOINT SAVEPOINT_NAME;

• Data Query Language


DQL is used to fetch the data from the database.

SELECT
This statement is used to select data from a database and the data returned is stored
ina result table, called the result-set.
Syntax

SELECT Column1, Column2, ...ColumN FROM TableName;


--(*) is used to select all from the

tableSELECT * FROM table_name;

-- To select the number of records to return

use:SELECT TOP 3 * FROM TableName;

Apart from just using the SELECT keyword individually, you can use the
followingkeywords with the SELECT statement:

DISTINCT CLAUSE
-------------------------
The ‘SELECT DISTINCT’ Statement
This statement is used to return only different values.
Syntax

SELECT DISTINCT Column1, Column2, ...ColumnN FROM TableName;

SELECT DISTINCT MobNo FROM Emp;

Example

You might also like