Você está na página 1de 16

SQL

(Structured Query Language)


Presented by: Manohar Prasad Specialization: IB+IT Roll No.- 1999/01030003

Structured Query Language (SQL)


SQL allows user to access data in relational database management systems, such as Oracle, Access, SQL Server, FoxPro, Informix and others, by allowing users to describe the data the user wishes to see.

5/4/2012

www.manoharprasad.wordpress.com

SQL statements are issued for the purpose of:

Data definition - Defining tables and structures


in the database .

Data manipulation - Inserting new data,

Updating existing data, Deleting existing data, and Querying the Database ( Retrieving existing data from the database).

5/4/2012

www.manoharprasad.wordpress.com

DDL Statements
CREATE - Create new database objects such as tables or
views

DROP ALTER

- Drop a database object such as a table, view or index - Change an existing table, view or index definition such as tables or views (For Data Control ) tables and views (For Data Control )
www.manoharprasad.wordpress.com

GRANT - Allow another user to access database objects

REVOKE - Disallow a user access to database objects such as

5/4/2012

DML Statements
SELECT
INSERT UPDATE DELETE
- Retrieve data from a database table
- Insert new data into a database table - Change the values of some data items in a database table - Delete rows from a database table

TRUNCATE - Delete all rows from a database table (can not be rolled back) COMMIT
5/4/2012

- Make all recent changes permanent (DML transactional)


www.manoharprasad.wordpress.com 5

Creating Tables
Tables are defined with the CREATE TABLE command. This command creates an empty table, a table with no rows.
Basically defines a table name as describing a set of named columns. Defines the data types and sizes of the

columns.
Each table must have at least one column.
5/4/2012 www.manoharprasad.wordpress.com 6

The main SQL data types


Type description Oracle SQL

variable-length char. string fixed-length char. string number date

VARCHAR2(l) CHAR(l) NUMBER DATE

5/4/2012

www.manoharprasad.wordpress.com

Creating a table
E.g.:- 1) CREATE TABLE Employee (
Emp_No Name Address DOB Salary Char(4), Varchar2(15), Varchar2(15), Date, Number(9,2) );

5/4/2012

www.manoharprasad.wordpress.com

Creating table with constrains


e.g.:- 2) CREATE TABLE Employee (
Emp_No Name Address DOB Salary Char(4) Varchar2(15) Varchar2(15), Date, Number(9,2) Check (Salary > 3500) ); Primary Key, Not Null,

Primary Key
5/4/2012

Name field Cannot be null


www.manoharprasad.wordpress.com

Always Salary must be > 3500


9

Altering a Table
Once created, tables can be altered to accommodate changing needs.

5/4/2012

www.manoharprasad.wordpress.com

10

Add new column to the table

Eg:- 1) ALTER TABLE Employee ADD Sex char(1);

5/4/2012

www.manoharprasad.wordpress.com

11

Delete a existing column from the table


2)

ALTER TABLE Employee DROP COLUMN Sex ;

5/4/2012

www.manoharprasad.wordpress.com

12

Displaying Table Structure


E.g.:Name --------------EMP_NO NAME ADDRESS DOB SALARY DESCRIBE Employee; Null? -------------NOT NULL Type ------------CHAR(4) VARCHAR2(15) VARCHAR2(15) DATE NUMBER(9,2)

SEX

CHAR(1)

5/4/2012

www.manoharprasad.wordpress.com

13

Entering Values to a table


SQL provides the key word INSERT to add data to a table. E.g.:- 1) INSERT INTO Employee VALUES (E001,Ranjith,Matara,12-Feb-76,7400);

5/4/2012

www.manoharprasad.wordpress.com

14

Entering Values to a table


Naming columns for insert
2) INSERT INTO Employee (Emp_No, Name, Salary) VALUES (E001,Ranjith,7400.00);

5/4/2012

www.manoharprasad.wordpress.com

15

5/4/2012

www.manoharprasad.wordpress.com

16