Structured Query Language
The intention of SQL is to store, manage and manipulate data
within a database management system.
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.
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.
commands that come under DDL:
CREATE: It is used to create a new table in the database.
Syntax: CREATE TABLE Table_NAME (COLUMN_NAME
DATATYPES ) ;
Example: CREATE TABLE EMP ( EMPNO NUMBER(20),NAME
VARCHAR(50),JOB VARCHAR(100),salary NUMBER(10,2 ) );
data types: CHAR, VARCHAR, NUMBER, DATE
CHAR(size) A FIXED length string (can contain letters,
numbers, and special characters).
The size parameter specifies the column length
in characters - can be from 0 to 255. Default is
1
VARCHAR(siz A VARIABLE length string (can contain letters,
e) numbers, and special characters).
The size parameter specifies the maximum
string length in characters - can be from 0 to
65535
NUMBER(p, s) The P (precision) parameter indicates the
maximum total number of digits
The s(scale) parameter indicates the maximum
number of digits stored to the right of the
decimal point.
DATE Default date format is DD-MM-YY
ALTER: It is used to alter the structure of the database.
Syntax: ALTER TABLE table name ADD( column name
datatype)
Example: ALTER TABLE EMP ADD(ADDRESS VARCHAR(20));
DROP: It is used to delete structure and record stored in the
table.
Syntax: DROP TABLE ;
Example: DROP TABLE EMP;
TRUNCATE: It is used to delete all the rows from the table.
Syntax: TRUNCATE TABLE table_name;
2.Data Manipulation Language (DML) – DML commands are
used to modify the database. commands that come under DML:
INSERT: It is used to insert data into the row of a table.
Syntax: INSERT INTO TABLE_NAME (col1, col2, col3, …. col N)
VALUES (value1, value2, value3, .... valueN);
OR
INSERT INTO TABLE_NAME VALUES (value1, value2,
value3, .... valueN);
Example: INSERT INTO EMP VALUES (1,'Smith', 'MANAGER',
5000);
INSERT INTO EMP VALUES (1, 'JON', 'TL', 3000);
Update: This command is used to update or modify the
value of a column in the table.
Syntax: UPDATE table_name SET column name= value
[WHERE CONDITION]:
Example:
update emp set empno=2 where name ='JON';
DELETE: it is used to delete particular row in the table.
Syntax; delete from table name where column name= value;
Example: delete from EMP where JOB= 'TL';
3. Data Control Language DCL ; DCL commands are used to
GRANT and TAKE BACK authority from any database user.
GRANT: It is used to give user access privileges to a
database.
Example: GRANT SELECT, UPDATE ON MY_TABLE TO
SOME_USER, ANOTHER_USER;
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: Commit command is used to save all the
transactions to the database.
Syntex: COMMIT;
Example: DELETE FROM EMP WHERE AGE = 25;
COMMIT;
Rollback: Rollback command is used to undo transactions
that have not already been saved to the database.
Syntex: ROLLBACK;
Example: DELETE FROM CUSTOMERS WHERE AGE = 25;
ROLLBACK;
SAVEPOINT: It is used to roll the transaction back to a
certain point without rolling back the entire transaction.
Syntex: SAVEPOINT SAVEPOINT_NAME;
5. Data Query Language: DQL is used to fetch the data from
the database.
Syntax: SELECT expressions FROM table name;
Example: SELECT name FROM emp;
SQL Operator -
1.Arithmetic Operators –
Operator Description
+ Add
Example: SELECT name, salary, salary + 1000 AS
"total_salary" FROM emp;
- Subtract
Example: SELECT name, salary, salary - 2000 AS
"basic_salary" FROM emp;
* Multiply
Example: SELECT name, salary, salary * 12AS "CTC"
FROM emp;
/ Divide
Example: SELECT name, salary, salary / 4AS
"weekly_pay" FROM emp;
2.Comparison Operators –
Operat Description
or
= Equal to
> Greater than
< Less than
>= Greater than or equal to
Operator Description
ALL <= TRUELess
if all than
of theor
subquery values meet the condition
equal to
AND <> TRUENot
if allequal
the conditions
to separated by AND is TRUE
ANY TRUE if any of the subquery values meet the condition