SQL Commands PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document provides an overview of SQL commands. It explains different types of SQL commands such as DDL (Data Definition Language), DML (Data Manipulation Language), DCL (Data Control Language), TCL (Transaction Control Language), and DQL (Data Query Language). The document also includes syntax and examples for each type of command. This information is valuable for those learning about databases and SQL programming.
Full Transcript
Youtube:ComputerInfo प्रदे श,संघ लोक सेवा कक्षा:9861506269 What Query language? A query language is a specialized programming language for searching, creating and changing the contents of a database. Most query languages are textual, meaning that the queries...
Youtube:ComputerInfo प्रदे श,संघ लोक सेवा कक्षा:9861506269 What Query language? A query language is a specialized programming language for searching, creating and changing the contents of a database. Most query languages are textual, meaning that the queries are expressed as text string processed by the DBMS. SQL Commands: SQL (Structured Query Language) commands are instructions. It is used to communicate with the datatbase. It 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. Types of SQL Commands: There are five types of SQL Commands: DDL, DML, DCL, TCL, and DQL DDL= Data Definition Language DML= Data Manipulation Language DCL= Datta Control Language TCL= Transaction Control Language DQL= Data Query Language Youtube:ComputerInfo प्रदे श,संघ लोक सेवा कक्षा:9861506269 1. DDL (Data Definition Language) DDL changes the structure of the table like creating a table, deleting a table, altering a table etc. All the commands of DDL are auto-commited that means it permanently save all the changes in the database. Some DDL commands are: CREATE, ALTER, DROP, TRANCATE a) CREATE : It is used to create a new table in the database. Syntax: CREATE TABLE TABLE_NAME(COLUMN_NAME(DATATYPE[SIZE]); Example: CREATE TABLE EMPLOYEE(Name VARCHAR(20), Email VARCHAR(100), DOB DATE); b) DROP : It is used to delete both the structure and record stored in the table. Syntax: DROP TABLE table_name; example: 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. Syntax: to add a new column in the table ALTER TABLE table_name ADD column_name Column-definition; To modify existing column in the table; ALTER TABLE table_name MODIFY(column_definition…); Example: ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR(20)); ALTER TABLE STU_DETAILS MODIFY(NAME VARCHAR(20)); d) Trucate: It is used to delete all the rows from the table and free the space containing the table. Syntax: Truncate TABLE table_name; example: TRUNCATE TABLE EMPLOYEE; 2. Data Manipulation Language (DML) DML commands are used to modify the database. It is responsible for all form of channges 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. Some DML commands are: INSERT, UPDATE, DELETE etc. a) INSERT: The INSERT command is used to insert data into the row of a table. Youtube:ComputerInfo प्रदे श,संघ लोक सेवा कक्षा:9861506269 Syntax: INSERT INTO TABLE_NAME (col1, col2, col3…..colN) VALUES(value1, value2, value3,….valueN); Or INSERT INTO TABLE_NAME VALUES(value1, value2, value3,…...valueN); For example: INSERT INTO EMPLOYEE(Name, Address, Mobile) VALUES("Arun", "Gaur, Rautahat", "98646"); b) UPDATE: This command is used to update or modify the value of a column in a table. Syntax: UPDATE table_name SET(column_name1=value1, …..column_nameN=ValueN) (WHERE CONDITION); Example: UPDATE EMPLOYEE SET NAME="RAMU" WHERE ID="1"; c) DELETE: This command is used to remove one or more row from a table. Syntax: DELETE FROM table_name (where condition); Example: DELETE FROM EMPLOYEE WHERE Name="Arun" 3. Data Control Language (DCL) DCL commands are used to grant and take back authority form any database user. Some DCL commands are: GRANT, REVOKE etc. a) GRANT: It is used to give user access privileges to a database. Example: GRANT, SELECT, UPDATE ON MY_TABLE USER1, USER2; b) REVOKE: It is used to tack back permissions from the user. example: REVOKE SELECT, UPDATE ON MY_TABLE FROM USER1, USER2; Youtube:ComputerInfo प्रदे श,संघ लोक सेवा कक्षा:9861506269 3. 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. Some TCL commands are: COMMIT, ROLLBACK, SAVEPOINT a) COMMIT: Commit command is used to save all the transactions to the database. Syntax: Commit Example: DELETE FROM EMPLOYEE WHERE AGE=25; COMMIT; b) ROLLBACK: Rollback command is used to undo transactions that have not alreadybeen saved to the database. Syntax: ROLLBACK; Example: DELETE FROM CUSTOMERS WHERE AGE:25; ROLLBACK; c) Save point: 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): DDL command is used to fetch the data from the database. It uses only one command. Syntaxt: SELECT expressions FROM TABLES WHERE conditions Example: SELECT emp_name FROM employee WHERE age>20;