0% found this document useful (0 votes)
80 views

SQL - Structured Query Language

SQL is a standard language for accessing and manipulating databases. It allows users to define, manipulate, and control access to data in a database. The main components of SQL are: DDL (Data Definition Language) to define database objects; DML (Data Manipulation Language) to work with data; and DCL (Data Control Language) to control access. Common SQL statements include SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, and DROP.

Uploaded by

Anuraag Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

SQL - Structured Query Language

SQL is a standard language for accessing and manipulating databases. It allows users to define, manipulate, and control access to data in a database. The main components of SQL are: DDL (Data Definition Language) to define database objects; DML (Data Manipulation Language) to work with data; and DCL (Data Control Language) to control access. Common SQL statements include SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER, and DROP.

Uploaded by

Anuraag Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

SQL – Structured Query Language

• SQL is an ANSI (American National Standards


Institute) standard computer language for
accessing and manipulating database systems.

• SQL statements are used to retrieve and update


data in a database.

• SQL works with database programs like MS


Access, DB2, Informix, MS SQL Server, Oracle,
Sybase, etc.
Oracle offers the following basic data types:

• char(n): Fixed-length character data (string), n characters long.


The maximum size for n is 255 bytes . Note that a string of type
char is always padded on right with blanks to full length of n. (+ can
be memory consuming).
Example: char(40)

• varchar2(n): Variable-length character string. The maximum size


for n is 2000 . Only the bytes used for a string require storage.
Example: varchar2(80)

• number(o, d): Numeric data type for integers and reals. o =


overall number of digits, d = number of digits to the right of the
decimal point.
Examples: number(8), number(5,2)
Note that, e.g., number(5,2) cannot contain anything larger than
999.99 without resulting in an error. Data types derived from
number are int[eger], dec[imal], smallint
and real.

• date: Date data type for storing date and time.


The default format for a date is: DD-MMM-YY. Examples: ’13-OCT-
94’, ’07-JAN-98’

• long: Character data up to a length of 2GB. Only one long column


is allowed per table
SQL Data Definition Language
(DDL)
The Data Definition Language (DDL) part of SQL permits
database tables to be created or deleted. We can also define
indexes (keys), specify links between tables, and impose
constraints between database tables.

The most important DDL statements in SQL are: 

•CREATE TABLE - creates a new database table


•ALTER TABLE - alters (changes) a database table
•DROP TABLE - deletes a database table
•CREATE INDEX - creates an index (search key)
•DROP INDEX - deletes an index
SQL Data Manipulation Language
(DML)
SQL (Structured Query Language) is a syntax for executing
queries. But the SQL language also includes a syntax to
update, insert, and delete records.
These query and update commands together form the Data
Manipulation Language (DML) part of SQL:

•SELECT - extracts data from a database table


•UPDATE - updates data in a database table
•DELETE - deletes data from a database table
•INSERT INTO - inserts new data into a database table
SQL Statements

SELECT Data retrieval

INSERT
UPDATE
DELETE
MERGE Data Manipulation language

CREATE
ALTER
DROP
RENAME
TRUNCATE Data Definition language (DDL)
COMMIT
ROLLBACK
SAVEPOINT Transaction control

GRANT
REVOKE Data Control language (DCL)
SQL The SELECT Statement
The SELECT statement is used to select data from a table. The
tabular result is stored in a result table (called the result-set).

Syntax

SELECT column_name(s)
FROM table_name
To select the columns named "LastName" and "FirstName", use a
SELECT statement like this:
SELECT LastName, FirstName FROM Persons

Persons

LastName FirstName Address City


Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger

LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
Select All Columns

To select all columns from the "Persons" table, use a * symbol


instead of column names, like this:
SELECT * FROM Persons

LastName FirstName Address City

Hansen Ola Timoteivn 10 Sandnes

Svendson Tove Borgvn 23 Sandnes

Pettersen Kari Storgt 20 Stavanger


Create tables

To create a table:-

Create table <tablename> ( columname datatype,


columname datatype,….) ;

Eg.

Create table student ( sname varchar2(20) , rollno


number(10), subject varchar2(20) , dob date ) ;
To insert values

Insert into tablename values ( val1, val2, val3,…..);

Eg.

Insert into student values (‘sunita’, 56, ‘english’, ‘11-


05-09’);

To insert multiple rows

Insert into tablename values ( &val1, &val2, &val3,


…..);

Type / at the prompt to enter more records.


Insert Data in Specified Columns

Insert into tablename ( columname, columname)


values (val1, val2);

Eg.

Insert into student (sname ,roll ) values (‘mohan’ ,


78);
Where clause
When a where clause is added to the SQL sentence,
Only those records are displayed that satisfy the
specified condition.

Select * from tablename where condition ;

Eg.
Select * from student where marks>50 ;

Select sname, rollno from student where subject = “


english” ;
Inserting Rows with Null Values

• Implicit method: Omit the column from the


column list.

INSERT INTO students (rollno, sname) VALUES (30, ’dfg‘ );

1 row created.

• Explicit method: Specify the NULL keyword in the VALUES


clause.

INSERT INTO students VALUES (30, ‘dfg', NULL, NULL);

1 row created.
The UPDATE Statement

• Modify existing rows with the UPDATE statement.

UPDATE tablename SET column = value [, column =


value, ...] [WHERE condition];

• UPDATE student SET rollno = 70 WHERE sname = ‘dfg’ ;

1 row updated.
• UPDATE student SET rollno = 70 WHERE rollno = 10 ;

All rows in the table are modified if you omit the WHERE
clause.

• UPDATE student SET rollno = 110 ;

22 rows updated.

• UPDATE student SET marks = marks + 50 ;

22 rows updated.
The DELETE Statement

You can remove existing rows from a table by using


the DELETE statement.

 DELETE tablename ;

• DELETE student;

22 rows deleted
To delete only selected rows from the table:-

 DELETE tablename where condition ;

• DELETE student where sname = ‘sdfs’;

1 row deleted.

• DELETE student where city = ‘ bangalore’ ;

3 rows deleted.
• DELETE students WHERE city IN (‘bangalore’, ‘delhi’ );

4 rows deleted.

Dropping a Table
All data and structure in the table is deleted.

 DROP TABLE tablename;

• DROP TABLE student;

Table dropped.
Rename the table

 RENAME tablename TO newtablename;

• rename student to std;

table renamed.
The ALTER TABLE Statement

Use the ALTER TABLE statement to add, modify, or drop


columns of the table.

To add a column :-

 ALTER TABLE tablename ADD ( col1 datatype ) ;

• ALTER TABLE student ADD ( grade varchar2(3));

table altered.
To modify the column: - ( data type / size / default value)

 ALTER TABLE tablename MODIFY (column datatype ) ;

• ALTER TABLE student MODIFY ( sname


VARCHAR2(30));

Table altered.
To drop the column :-

 ALTER TABLE tablename DROP COLUMN (column);

• ALTER TABLE student DROP COLUMN grade ;

Table altered.

You might also like