DDL stands for “ data definition language”. It is the subset of SQL commands used to create,...

31
DATA MANIPULATION andCONTROL

Transcript of DDL stands for “ data definition language”. It is the subset of SQL commands used to create,...

DATA MANIPULATION andCONTROL

Data definition language(DDL) DDL stands for “ data definition

language”. It is the subset of SQL commands used to

create, modify and destroy databases and database objects like –tables , views etc.

A data Definition Language has a pre-defined syntax for describing data .

For example – To built a new table using SQL syntax the CREATE command is used, followed by parameters for the table name and column definition .

The DDL can also define the name of each column and the associated data type.

Once a table is created , it can be modified using the ALTER command .

If the table is no longer needed , the DROPcommand will delete the table.

Some of the comman DDL

statements in oracle include CREATE, ALTER, DROP, RENAME, TRUNCATE etc.

CREATING TABLES

CREATING TABLES

The CREATE TABLE command is used to create tables to store data.

Tables are owned by the user who creates them. The names of tables owned by a given user must be unique . The column names in the table must be unique .

Specifying table name is compulsory while creating a table.

The syntax for the CREATE TABLE statement is :

CREATE TABLE table _name

(column _name1 data type,

Column _name2 data type,

…column _nameN data type);

EXAMPLE:

CREATE TABLE STUDENT _RECORD

( Name varchar2 (20) , Class varchar2 (10), Roll _no number(5), Section char (1), Marks number (5,2) );

Column name Type Size Description

Name Varchar2 20 Name of student

Class Varchar2 10 Class of student

Roll no Number 5 Roll number of student

Section Char 1 Section of student’s class

Marks Number 5,2 Marks obtained by student

STUDENT RECORD

CREATING A TABLE WITH DATA

FROM ANOTHER TABLE

CREATING A TABLE WITH DATA FROM ANOTHER TABLE

A table can be created by using CREATE TABLE statement with data, derived another table .

The syntax is :

CREATE TABLE NEW _TABLE_NAME

[(Column_1, column_2,…. Column_n)] ,

AS SELECT Column _1 , column_2 ,…. Column _n

From OLD_TABLE_NAME ;

TABLE STUDENT

NAME CLASS ROLL NO

AGE

A BCA 1 17

B BSC 2 18

C MSC 3 19

D MCA 4 20

FOR EXAMLE

Create a table STUDENT1 from student table having student name and Rollno .

CREATE TABLE STUDENT1 [( NAME , ROLLNO)]AS SELECT NAME , CLASS,

ROLLNO, AGEFROM STUDENT

TABLE STUDENT 1NAME ROLLNO

A 1

B 2

C 3

D 4

INSERTING VALUES INTO

TABLE

INSERT STATMENT

It is used to insert new rows/records in a table.

Values can be inserted for all the columns or for selected columns of the table.

We can insert data values into a table through different ways:

Inserting the data direct to the table.

Inserting the data to a table through a select statement.

The syntax for INSERT statement is:

INSERT INTO TABLE_NAME VALUES(value 1,value 2,value 3,

…….value n);

Example: class is a table name.Insert into class values(‘ram’, 20,

’bca’);

Following statements would create 4 records in CUSTOMERS table:

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );  INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );  INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );  INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 );  

ID NAME AGE ADDRESS SALARY

1 RAMESH 32 AHEMDABAD 2000.00

2 KHILAN 25 DELHI 1500.00

3 KAUSHIK 23 KOTA 2000.00

4 CHAITALI 25 MUMBAI 6500.00

UPDATING THE COLUMNS & ROWS:

UPDATE STATEMENT

To update or modify the exiting rows in a table.

It modifies specific rows if the WHERE clause is specified.

We can use WHERE clause with UPDATE query to update selected rows otherwise all the rows would be affected.

The basic syntax of UPDATE query with WHERE clause is as follows:

UPDATE table_nameSET column1 = value1, column2 =

value2...., column N = value NWHERE [condition];

Example: UPDATE EMP SET COMM=500;EMP is the table name.

Consider the CUSTOMERS table having the following records:

ID NAME AGE ADDRESS SALARY

1 RAMESH 32 AHEMDABAD 2000.00

2 KHILAN 25 DELHI 1500.00

3 KAUSHIK 23 KOTA 2000.00

4 CHAITALI 25 MUMBAI 6500.00

Following is an example, which would update ADDRESS for a customer whose ID is 6:

SQL> UPDATE CUSTOMERSSET ADDRESS = 'Pune'WHERE ID = 4;

Now, CUSTOMERS table would have the following records:

ID NAME AGE ADDRESS SALARY

1 RAMESH 32 AHEMDABAD 2000.00

2 KHILAN 25 DELHI 1500.00

3 KAUSHIK 23 KOTA 2000.00

4 CHAITALI 25 PUNE 6500.00

DELETING ROW FROM TABLE

DELETE STATEMENT

It is used to delete rows from a table.

To delete rows from a table, table must be in your schema.

We can use WHERE clause with DELETE query to delete selected rows, otherwise all the records would be deleted.

The basic syntax of DELETE query with WHERE clause is as follows:

DELETE FROM table_nameWHERE [condition];Example:DELETE FROM EMPWHERE JOB=‘ CLERK’;EMP is the table name.

Consider the CUSTOMERS table having the following records:

ID NAME AGE ADDRESS SALARY

1 RAMESH 32 AHEMDABAD 2000.00

2 KAUSHIK 25 DELHI 1500.00

3 KHILAN 23 KOTA 2000.00

4 CHAITALI 25 MUMBAI 6500.00

Following is an example, which would DELETE a customer, whose ID is 6:

SQL> DELETE FROM CUSTOMERSWHERE ID = 4;

Now, CUSTOMERS table would have the following records:

ID NAME AGE ADDRESS SALARY

1 RAMESH 32 AHEMDABAD 2000.00

2 KAUSHIK 25 DELHI 1500.00

3 KHILAN 23 KOTA 2000.00