SQL Commands:
- SQL commands are instructions. It is used to communicate with the database. It is also used to perform specific tasks, functions, and queries of data.
- SQL can perform various tasks like create a table, add data to tables, drop the table, modify the table, set permission for users.
1. Data Definition Language (DDL)
- DDL changes the structure of the table like creating a table, deleting a table, altering a table, etc.
- All the command of DDL are auto-committed that means it permanently save all the changes in the database.
Here are some commands that come under DDL:
- CREATE
- ALTER
- DROP
- TRUNCATE
column1 datatype,
column2 datatype,
column3 datatype,
....
);
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
b. 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.
1)To add a new column in the table
Syntax:
ALTER TABLE table_name ADD column_name datatype;
Example:
ALTER TABLE Customers
ADD Email varchar(255);
2)To modify existing column in the table:
Syntax:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
3)ALTER TABLE - DROP COLUMN
To delete a column in a table.
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
c. DROP: It is used to delete both the structure and record stored in the table.
Syntax:
DROP TABLE table_name;
Example:
Syntax:
TRUNCATE TABLE table_name;
2. Data Manipulation Language
- 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.
- INSERT
- UPDATE
- DELETE
a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a table.
1)Specify both the column names and the values to be inserted:
Syntax:
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:
Syntax:
INSERT INTO table_name
VALUES(value1,value2,value3, ......);
b. UPDATE: This command is used to update or modify the value of a column in the table.
Syntax:
UPDATE table_name
SET column1=value1,column2=value2, .....
WHERE condition;
Example:
UPDATE Customers
SET cname='Manik',City='Hyd'
WHERE cid=1234;
c. DELETE: It is used to remove one or more row from a table.
1)Below Syntax is used for delete particular row from the table using where condition
DELETE FROM table_name
WHERE condition;
2)Below Syntax is used for deleting all records from the table
DELETE FROM table_name;
3. Data Control Language
DCL commands are used to grant and take back authority from any database user.
Here are some commands that come under DCL:
- Grant
- Revoke
a. Grant: It is used to give user access privileges to a database.
Example:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOTHER_USER;
b. Revoke: It is used to take back permissions from the user.
Example:
REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2;
4. Transaction Control Language
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:
- COMMIT
- ROLLBACK
- SAVEPOINT
a. Commit: Commit command is used to save all the transactions to the database.
Syntax:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
COMMIT;
b.Rollback: Rollback command is used to undo transactions that have not already been saved to the database.
Syntax:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
ROLLBACK;
c. SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back the entire transaction.
Syntax:
SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language
DQL is used to fetch the data from the database.
It uses only one command:
- SELECT
a. 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 column1,column2... from table_Name where condition;
Example:
Select name,id from student
Where id=123;
Without using where condition:
Select column1, column2..... from table_Name;
Example:
Select name,id from student;