0% found this document useful (0 votes)
6 views34 pages

SQL Commands

Uploaded by

Tanish “Tam”
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
6 views34 pages

SQL Commands

Uploaded by

Tanish “Tam”
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 34

SQL

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.

1974 − Structured Query Language appeared.

1978 − IBM worked to develop Codd's ideas and released a product named
System/R.

1986 − IBM developed the first prototype of relational database and


standardized by ANSI. The first relational database was released by Relational
Software which later came to be known as Oracle.
Datatypes:
• Data types are used to represent the nature of the data that can be
stored in the database table. For example, in a particular column of a
table, if we want to store a string type of data then we will have to
declare a string data type of this column.

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),

Here 5 represents the number of digits present. If we want we an write


the above example as,

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

• 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.
• 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.
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.
• Here are some commands that come under DDL:

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:

CREATE TABLE Student


(
Roll_No varchar2(5) ,
First_Name varchar2(25) ,
Last_Name varchar2(25) ,
Age number(2) ,
Marks number(3)
);
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 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

ALTER TABLE table_name


ADD column_name COLUMN-definition;

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:

ALTER TABLE table_name MODIFY column_name column_datatype(size);


Example :

ALTER TABLE student MODIFY Last_Name varchar(25);


• Rename a Column with ALTER TABLE

ALTER TABLE table_name


RENAME COLUMN old_name TO new_name;

ALTER TABLE Employees_detail


RENAME COLUMN Emp_age to age_of_Emp;
• If you want to add multiple columns to an existing table using a single statement,
you use the following 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

TRUNCATE TABLE Table_Name;


• Example
Suppose, you want to delete the record of the Student table. To do this,
you have to write the following TRUNCATE DDL command:

TRUNCATE TABLE Student;


The above query successfully removed all the records from the student
table.
e. RENAME Command

RENAME is a DDL command which is used to change the name of the


database table.

Syntax of RENAME command:


RENAME Old_Table_Name TO New_Table_Name;

Example
RENAME Student TO Student_Details ;
2. Data Manipulation Language

• DML commands are used to modify the database. It is responsible for


all form of changes 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.
Here are some commands that come under DML:
• INSERT
• UPDATE
• DELETE
1. INSERT: The INSERT statement is a SQL query. 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 Employee(Emp_id, Emp_name) values (001, “ bhanu”);


2. UPDATE Statement
UPDATE statement is used to modify the existing records in a table.
Syntax:
UPDATE table
SET column1 = expression1,
column2 = expression2,
...
column_n = expression_n
WHERE conditions;

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;

Updating Multiple Columns using UPDATE Statement:


UPDATE Student
SET
Fathers_Name = 'Mr. Ramesh' ,
Mothers_Name = 'Ragini'
WHERE rollno = 22;

UPDATE Multiple Records :


It is the WHERE clause that determines how many records will be updated.
UPDATE Student
SET College_Name='ICCS'
Important Points About SQL UPDATE
Statement
• SQL UPDATE Statement is used to update data in an existing table in
the database.
• The UPDATE statement can update single or multiple columns using
the SET clause.
• The WHERE clause is used to specify the condition for selecting the
rows to be updated.
• Omitting the WHERE clause in an UPDATE statement will result in
updating all rows in the table.
3. DELETE Statement

In Oracle, DELETE statement is used to remove or delete a single record or


multiple records from a table.
Syntax
DELETE FROM table_name
WHERE conditions;

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;

Delete all records from table

DELETE FROM customers ;


3. Data Query Language (DQL)
DQL is a SQL statement that allows you to get and organise data from a
database. You can use the SELECT command to extract data from a
database to perform actions on it.
SELECT Statement
To retrieve data from one or more columns of a table, you use the
SELECT statement with the following :

Syntax:
SELECT column_1, column_2, ... FROM table_name;
Example:
1. SELECT name FROM employee; //Single column

2. SELECT eno, salary FROM employee //Multiple column

An asterisk(*) will produce all the columns in their proper order

3. Select * from employee // All columns/Data

4. select * from employee where salary > 50000 //Multiple Rows

You might also like