SQL Commands
SQL Commands
commands
Introduction to SQL
SQL stands for Structured Query Language, which is a
standardised language for interacting with DBMS
Database Management System). SQL is used to perform
C.R.U.D (Create, Retrieve, Update & Delete) operations on databases.
SQL is a Standard
SQL is an ANSI(American National Standards Institute) standard computer language
for accessing and manipulating database system.
SQL works with database program like MS Access, DB2, Informix, MS SQL server,
Oracle, Sybase etc.
Although SQL is an ANSI/ISO standard, there are different versions of the SQL
language.
However, to be compliant with the ANSI standard, they all support at least the major
commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.
A Brief History of SQL
1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the father of relational
databases. He described a relational model for databases.
1978 − IBM worked to develop Codd's ideas and released a product named
System/R.
Data types mainly classified into three categories for every database.
1. String Data types
2. Numeric Data types
3. Date Data types
CHAR(Size)
It is used to specify a fixed length string that can contain
numbers, letters, and special characters. Its size can be 0 to
255 characters. Default is 1.
Example :
gender char(1);
VARCHAR2(Size)
It is used to specify a variable length string that can contain
numbers, letters, and special characters. Its size can be from
0 to 65535 characters.
Example :
emp_name varchar2(15);
Number :
This datatype represents any numerical data .It can store integer and
decimal number.
For e.g. salary number(5),
salary number(5,2);
Here 2 represent that there are 2 digits present after a decimal point.
Date :
• The dates and times can be represented by the data type DATE AND
TIME .
• The date is a datatype available which has components year,month and
day in the form of DD-MON-YY.
• Working with date and time can be tricky because the date formats may
vary for different reasons. For example, the United States follows the
date format of mm-dd-yyyy whereas the United Kingdom follows the
date format of dd-mm-yyyy.
For Eg
Date_Of_Birth date;
SQL Commands
1. CREATE
2. ALTER
3. DROP
4. TRUNCATE
a. CREATE It is used to create a new table in the database.
Syntax to create a new table:
CREATE TABLE table_name
(
column_Name1 data_type ( size of the column ) ,
column_Name2 data_type ( size of the column) ,
column_Name3 data_type ( size of the column) ,
...
column_NameN data_type ( size of the column )
);
CREATE TABLE EMPLOYEE
(Name VARCHAR2(20),
Email VARCHAR2(100),
DOB DATE);
• Suppose, you want to create a Student table with five columns in the
SQL database. To do this, you have to write the following DDL
command:
Syntax:
DROP TABLE table_name;
Example:
DROP TABLE EMPLOYEE;
c. ALTER: It is used to alter the structure of the table . This change could be
either to modify the characteristics of an existing column or probably to
add a new column.
Syntax:
1. To add a new column in the table
Example:
ALTER TABLE Student ADD Father's_Name Varchar(60);
2. To modify existing column in the table: Suppose, you want to change the
character size of the Last_Name column of the Student table. To do this,
you have to write the following DDL command:
Syntax:
Syntax :
ALTER TABLE table_name
ADD (column_1 column-definition,
column_2 column-definition,
...
column_n column_definition);
Example :
ALTER TABLE customers
ADD (customer_type varchar2(50),
customer_address varchar2(50));
• Similarly, we can drop a column using alter with the following
syntax:
ALTER TABLE table_name
DROP COLUMN column_name;
Example:
alter table my_table
drop column employed;
If you want to delete multiple columns to an existing table using a single statement,
you use the following
syntax:
alter table table_name drop (column_name1, column_name2);
Example:
alter table Employee233 drop(salary,EName);
d. TRUNCATE:
• TRUNCATE is another DDL command which deletes or removes all the
records from the table.
• This command also removes the space allocated for storing the table
records.
Syntax of TRUNCATE command
Example
RENAME Student TO Student_Details ;
2. Data Manipulation Language
Syntax:
INSERT INTO TABLE_NAME
(col1, col2, col3,.... col N)
VALUES (value1, value2, value3, .... valueN);
Or
Note :WHERE conditions is Optional. The conditions that must be met for the
update to execute. If no conditions are provided, then all records in the table will
be updated.
Update Single Column Using UPDATE Statement:
UPDATE Student
SET Fathers_Name = 'Mr. Ramesh'
WHERE rollno = 22;
Example
DELETE FROM Student
WHERE name = 'Sohan';
This statement will delete all records from the student table where name is
"Sohan".
Delete Example: On multiple
conditions
DELETE FROM customers
WHERE last_name = 'Maurya'
AND customer_id > 2;
Syntax:
SELECT column_1, column_2, ... FROM table_name;
Example:
1. SELECT name FROM employee; //Single column