Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

SQL Commant A1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

SQL Commands

Introduction

SQL stands for Structured Query Language. SQL commands are the
instructions used to communicate with a database to perform tasks, functions,
and queries with data.

SQL commands can be used to search the database and to do other functions
like creating tables, adding data to tables, modifying data, and dropping tables.

Here is a list of basic SQL commands (sometimes called clauses) you should
know if you are going to work with SQL
❖ DDL (Data Definition Language)
The DDL Commands in Structured Query Language are used to create and
modify the schema of the database and its objects. The syntax of DDL
commands is predefined for describing the data. The commands of Data
Definition Language deal with how the data should exist in the database.

Here are some commands that come under DDL:

o CREATE
o ALTER
o DROP
o TRUNCATE

• CREATE Command

CREATE is a DDL command used to create databases, tables, triggers and


other database object

A database table is used to store records (data). To create a database table,


we use the SQL CREATE TABLE statement.

Syntax :
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]
);

Example:

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


AR2(100), DOB DATE);
• DROP Command

It is used to delete both the structure and record stored in the table.

The DROP COLUMN command is used to delete a column in an existing


table. SQL commands can be widely divided into DDL (Data Definition
Language), DCL (Data Control Language), DML (Data Manipulation
Language), DQL (Data Query Language), and TCL (Transition Control
Language) commands. In this article, we will look at the DROP command
in SQL, which is a DDL command.

Since the DROP command is a Data Definition Language Command, It


involves a permanent change in the table's structure, which means the
command cannot be rolled back to retrieve the original data. DROP
command is used to drop or delete entities in the database. These entities
could be tables, columns, or even entire databases themselves.

Syntax :

DROP TABLE table_name;

Example :

DROP TABLE EMPLOYEE;


• ALTER Command

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.

The ALTER TABLE statement in SQL is used to add, remove, or


modify columns in an existing table. The ALTER TABLE statement is
also used to add and remove various constraints on existing tables.

Syntax:

ALTER TABLE table_name ADD column_name COLUMN-


definition;

Example :

ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));

• TRUNCATE Command

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

The TRUNCATE TABLE command deletes the data inside a table, but not
the table itself.

Syntax:

TRUNCATE TABLE table_name;

Example:

TRUNCATE TABLE EMPLOYEE;


❖ Data Manipulation Language (DML)

DML commands are used to modify the database. It is responsible for all form
of changes in the database.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.

Here are some commands that come under DML:

o INSERT
o UPDATE
o DELETE

• INSERT Command

The INSERT statement is a SQL query. It is used to insert data into the
row of a table.
1. Insert a single row into a table
2. Insert multiple rows into a table
3. Copy rows from a table to another table.

Syntax :

INSERT INTO TABLE_NAME


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

Example :

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


")
• UPDATE Command

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


table.

The power of the UPDATE command lies in its versatility. Whether you’re
dealing with small changes or massive data modifications, this function can
handle it all without breaking a sweat. But don’t let its power intimidate you;
once you understand how SQL UPDATE works and follow some simple
syntax rules, I believe you’ll find it surprisingly easy to use.

• Use the UPDATE command for the target table

• Select the column (or columns) we want to update with new values via the
SET clause

• Optionally we can use a WHERE clause to limit the scope of the records
updated

Syntax:

UPDATE table_name SET [column_name1= value1,...column_nameN = val


ueN] [WHERE CONDITION]

Example :

UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
• DELETE Command

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


SQL DELETE is a basic SQL operation used to delete data in a
database. SQL DELETE is an important part of database management
DELETE can be used to selectively remove records from a database
table based on certain conditions. This SQL DELETE operation is
important for database size management, data accuracy, and integrity.

Syntax:

DELETE FROM table_name [WHERE condition];

Example :

DELETE FROM javatpoint


WHERE Author="Sonoo";
❖ Data Control Language (DCL)

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

Here are some commands that come under DCL:

Data Control Language (DCL) forms an essential part of Structured


Query Language. They help manage access and permissions to a
particular database. A database administrator is a person who looks into
the design and maintenance of an organization's database system. He
ensures the security of the database.

• DCL allows the administrator to control access to the data in the database.

• DCL commands permit to set the roles and permissions for different users.

• DCL commands can restrict or deny access to certain parts of the database.

• DCL ensures authorized access to sensitive data and maintains data security
& integrity.

o Grant
o Revoke

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


SQL GRANT Command SQL GRANT is a command used to provide
access or privileges on the database objects to the users. The Syntax for
the GRANT command is: GRANT privilege_name ON object_name TO
{user_name|PUBLI,|role_name}[WITHGRANTOPTION]; privilege_na
me is the access right or privilege granted to the user.
Syntax:

Grant select ,insert ,update ,delete on table name to username;

Example :

Grant select ,insert , update , delete , on student to Raj;

• Revoke: It is used to take back permissions from the user.

The “REVOKE” command in SQL is used to revoke or remove


previously granted privileges or permissions from users or roles. It allows
the database administrator to control access to database objects by
revoking privileges that were previously granted using the “GRANT”
command.

Syntax:

Revoke select ,insert ,update ,delete on tablename to suername;

Example :

Revoke select insert ,update ,delete on student to Raj;


❖ Transaction Control Language (TCL)
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.

Here are some commands that come under TCL:

o COMMIT
o ROLLBACK
o SAVEPOINT
o

• Commit: Commit command is used to save all the transactions to the


database.

Syntax:

COMMIT;
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
COMMIT;

• Rollback: Rollback command is used to undo transactions that have not


already been saved to the database.

Syntax:

ROLLBACK;

Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
ROLLBACK;

❖ Data Query Language (DQL)

DQL is used to fetch the data from the database.

It uses only one command

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. We can define DQL as follows it is a
component of SQL statement that allows getting data from the database and
imposing order upon it. It includes the SELECT statement.

SELECT

• 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:

SELECT expressions
FROM TABLES
WHERE conditions;

Example:
SELECT emp_name
FROM employee
WHERE age > 20;
❖ Conclusion
The breadth and scope of the SQL commands provide the capability to create
and manipulate a wide variety of database objects using the
various CREATE, ALTER, and DROP commands. Those database objects then
can be loaded with data using commands such as INSERT. The data can be
manipulated using a wide variety of commands, such as SELECT, DELETE,
and TRUNCATE, as well as the cursor commands,
DECLARE, OPEN, FETCH, and CLOSE. Transactions to manipulate the data
are controlled through the SET command, plus
the COMMIT and ROLLBACK commands. And finally, other commands
covered in this chapter include those that control a user’s access to database
resources through commands such as GRANT and REVOKE.
❖ Reference

https://levelup.gitconnected.com/

https://learn.microsoft.com

https://www.bing.com/

You might also like