3. Page 3Classification: Restricted
SQL
SQL is a database computer language designed for the retrieval and management of data in relational database. SQL stands for
Structured Query Language.
DDL Command
Data Definition Language (DDL) is a standard for commands that define the different structures in a database. DDL
statements create, modify, and remove database objects such as tables, and users. Common DDL statements are CREATE,
ALTER, and DROP.
• CREATE - to create objects in the database
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
);
• ALTER - alters the structure of the database
ALTER TABLE table_name
ADD column_name datatype
• DROP - delete objects from the database
DROP INDEX index_name ON table_name
• TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
TRUNCATE TABLE table_name
• COMMENT - add comments to the data dictionary
--Select all:
SELECT * FROM Customers;
• RENAME - rename an object
Alter table Stu_Table rename to Stu_Table_10
4. Page 4Classification: Restricted
DML Command
DML is abbreviation of Data Manipulation Language. It is used to retrieve, store, modify, delete, insert
and update data in database. Examples: SELECT, UPDATE, INSERT statements.
• SELECT - retrieve data from the a database
SELECT * FROM table_name;)
• INSERT - insert data into a table
INSERT INTO table_name VALUES (value1,value2,value3,...);
• UPDATE - updates existing data within a table
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
• DELETE - deletes all records from a table, the space for the records remain
DELETE FROM table_name
WHERE some_column=some_value;
• MERGE - UPSERT operation (insert or update)
5. Page 5Classification: Restricted
DCL Command
Data Control Language (DCL) statements. Some examples:
• GRANT - gives user's access privileges to database
GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];
• REVOKE - withdraw access privileges given with the GRANT command
REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}
SQL Joins
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field
between them.
The most common type of join is: SQL INNER JOIN (simple join). An SQL INNER JOIN returns all rows
from multiple tables where the join condition is met.
(Refer: http://www.w3schools.com/sql/sql_join.asp)
SELECT Table1.Column1_name, Table2.Columns2_Name, Table1.Column2_name
FROM Table1
INNER JOIN Table2
ON Table2.Column2_name=Table1.Column_name;