SQL Basics
SQL Basics
Syntax:
REATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Example:
CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHA
R2(100), DOB DATE);
Data Definition Language (DDL)- Drop
Drop: It is used to delete both the structure and record
stored in the table.
Syntax:
DROP TABLE ;
Example:
DROP TABLE EMPLOYEE;
Data Definition Language (DDL)- ALTER
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:
ALTER TABLE table_name ADD column_name COLUMN-definition;
ALTER TABLE MODIFY(COLUMN DEFINITION....);
Example:
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
Data Definition Language (DDL)- TRUNCATE
TRUNCATE: 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;
Data Manipulation Language
• DML commands are used to modify the database. It is
responsible for all form of CHANGES in the database.
OR
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
Example:
INSERT INTO XYZ (Author, Subject) VALUES ("Sonoo", "DBMS");
Data Manipulation Language - UPDATE
Update: This command is used to update or modify the value of a
column in the table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_n
ameN = valueN] [WHERE CONDITION]
Example:
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
Data Control Language
DCL commands are used to GRANT and TAKE BACK
authority from any database user.
Revoke
Data Control Language - Grant
GRANT: It is used to give user access privileges to a database.
Example:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOT
HER_USER;
Example:
Syntax:
SELECT expressions FROM TABLES WHERE conditions;
Example:
SELECT emp_name FROM employee WHERE age > 20;
SQL Operator
SQL Comparison Operators:
Operator Description
+
It adds the value of both operands.
-
It is used to subtract the right-hand operand from the left-hand
operand.
*
It is used to multiply the value of both operands.
/
It is used to divide the left-hand operand by the right-hand
operand.
%
It is used to divide the left-hand operand by the right-hand
operand and returns reminder.
SQL Arithmetic Operators
Operator Description
= It checks if two operands values are equal or not, if the values are
queal then condition becomes true.
!= It checks if two operands values are equal or not, if values are not
equal, then condition becomes true.
<> It checks if two operands values are equal or not, if values are not
equal then condition becomes true.
> It checks if the left operand value is greater than right operand value,
if yes then condition becomes true.
< It checks if the left operand value is less than right operand value, if
yes then condition becomes true.
>= It checks if the left operand value is greater than or equal to the right
operand value, if yes then condition becomes true.
SQL Arithmetic Operators
Operator Description
<= It checks if the left operand value is less than or equal to the right
operand value, if yes then condition becomes true.
!< It checks if the left operand value is not less than the right operand
value, if yes then condition becomes true.
!> It checks if the left operand value is not greater than the right operand
value, if yes then condition becomes true.
SQL Logical Operators
Operator Description
Between It is used to search for values that are within a set of values.