35
DDL (Data Definition Language) Data Definition Language is used 1. to Create an object 2. to alter the structure of an object 3. to drop an object created [email protected]

DDL (Data Definition Language)

Embed Size (px)

DESCRIPTION

DDL (Data Definition Language). Data Definition Language is used 1. to Create an object 2. to alter the structure of an object 3. to drop an object created. [email protected]. How To Create Table?. Syntax : create table tablename (columnname data type(size), - PowerPoint PPT Presentation

Citation preview

Page 1: DDL (Data Definition Language)

DDL (Data Definition Language)

Data Definition Language is used

1. to Create an object2. to alter the structure of an object3. to drop an object created

[email protected]

Page 2: DDL (Data Definition Language)

How To Create Table?

• Syntax : create table tablename

(columnname data type(size), columnname data type(size), …);

We should specify unique column name We should specify proper data type along with its width. If the above command is executed successfully the message

“table created” is displayed.

[email protected]

Page 3: DDL (Data Definition Language)

Naming Conventions For Table Name

The table name should adhere strictly to the following norms

First letter should be alphabet Reserve word cannot be used Maximum length for table name is 30 characters Two different tables cannot have same name Underscore, numerals & letters are allowed but not blank space and

Single quotes.

Note:If the user uses double quotes then upper & lower case are not

equivalent.e.g. “info”, “INFO”, “Info” are not same.

[email protected]

Page 4: DDL (Data Definition Language)

Example To Create Table

• Create table client_master

(client_no varchar2(6), name varchar2(15), addr varchar2(25), bal_due number(10,2));

[email protected]

Page 5: DDL (Data Definition Language)

How to view table structure?

Command: descIf user wants to view the table structure above command will achieve the same.

Syntax:Desc (tablename);

Example: desc client_master;

This will display structure of table client_master.

[email protected]

Page 6: DDL (Data Definition Language)

How to Insert Data Into Table?

• Insert Command:Once table is created it remains

skeletal structure unless it is populated with data. The insert command is used to add one or more records to a table.

• Syntax:insert into tablename values(expression,expression,…);

ORinsert into tablename

(columnname, coumnname,…) values(expression,expression,…);

[email protected]

Page 7: DDL (Data Definition Language)

Insert Statement :When inserting a single row of data into a table, the insert

operation, creates a new row in the database table

loads that row with the values passed into all the columns specified

In Insert statement columns & values have one to one relationship. (first value into first column)

If there are exactly same no. of values as per no. of columns & values are in accordance with the way the column were created; then no need to specify column name

If there are less values than the columns in the table then it is mandatory to indicate both the column name & its corresponding value in the insert statement.

[email protected]

Page 8: DDL (Data Definition Language)

(….continued)

While using ‘insert’ values are separated by commas and values having data types char, varchar/varchar2, raw, long, date should be enclosed in the single quotes.

Values must be entered in the same order as they are defined in the table.

[email protected]

Page 9: DDL (Data Definition Language)

• Use Of ‘&’ In Insert Statement

Syntax:Insert into tablename values(‘&expr1’, ‘&expr2’,…..);

Oracle will prompt the user to enter values for specified columns.

• Discussion of example. [email protected]

Page 10: DDL (Data Definition Language)

Viewing Data In The Table

Select Command:

Request for information stored in a table can be done through the select command.(This is generally referred as ‘querying’ the table.)

We can use ‘Select’ to view1. All rows and all columns2. Filtered data

[email protected]

Page 11: DDL (Data Definition Language)

• All Rows And All Columns:Syntax

In order to view global table data the syntax is,select columnname1, columnname2, ……..n from tablename;e.g.select cl_no, name, addr, bal_due from client_master;

OR select * from tablename;

e.g.select * from client_master;

Note: Oracle allows the user to use the meta character asterisk(*), which is expanded by the oracle to mean all columns.

[email protected]

Page 12: DDL (Data Definition Language)

• Filtering Table Data: SQL gives us filtering out data that is not

required. The ways of filtering table data are,

1. selected columns and all rows

2. selected rows and all columns

3. selected columns and selected rows

Note: Oracle engine compiles the sentence, executes it, and retrieves data for all columns/rows from the table.

[email protected]

Page 13: DDL (Data Definition Language)

• selected columns and all rows:It is retrieval of specific columns of a table.

Syntax:select columnname ,columnname

from tablename;Example:

select client_no, namefrom client_master;

[email protected]

Page 14: DDL (Data Definition Language)

• selected rows and all columns

• Oracle provides the option of using where clause in combination with select statement to apply filter on rows.

• Syntax

Select * from tablenamewhere search condition;

ExampleSelect *

from client_master where bal_due>0;

[email protected]

Page 15: DDL (Data Definition Language)

• Where Clause

When where clause added to SQL sentence, the oracle server compares each record from the table with the condition specified in where clause.

Oracle displays only those records that satisfy the specified condition.

Where clause can appear only after from clause.

In search condition all standard operators can be used. (logical, arithmetic, comparison)

[email protected]

Page 16: DDL (Data Definition Language)

• Selected Rows And Selected Columns

It is used to view specific data set from a table .

Syntax: select columnname, columnname

from tablenamewhere search condition;

Example:Select client_no, client_name

from client_master where bal_due>0;

[email protected]

Page 17: DDL (Data Definition Language)

• Elimination Of Duplicates:

To prevent selection of duplicate rows, we include distinct clause in the select statement.

Syntax:select distinct columnname, columnname

from tablename;

The above syntax scans through the values of columns specified and displays unique values from amongst them.

Example:select distinct job from employee;

[email protected]

Page 18: DDL (Data Definition Language)

(….continued)

Syntax:select distinct *

from tablename; The above syntax scans through entire rows, and eliminates rows that

have exactly the same contents in each column.

Example:select distinct * from client_master;This will select only unique row from client_master,

[email protected]

Page 19: DDL (Data Definition Language)

• Sorting Data In a Table

Oracle allows data from a table to be viewed in a sorted order.

The rows retrieved from the table will be sorted in eitherascending or descending order

Ascending or descending order of the data is depends on condition specified in the select clause.

Syntaxselect * from tablenameorder by columnname, columname [sort order];

[email protected]

Page 20: DDL (Data Definition Language)

• Ascending Order:

select * from client_masterorder by client_no;The above query will display all records from client_master

sorted in ascending order as per column client_no.

• Descending Order:For viewing the data in descending order the word ‘desc’ must

mentioned after the column name and before the semi colon in order by clause.

In case there is no mention of sort order in order by clause Oracle engine sorts in ascending order by default.

select client_no, name, addr1, cityfrom client_masterorder by client_no desc;

[email protected]

Page 21: DDL (Data Definition Language)

• Creating a Table From a Table:syntax:

create table tablename[(columnname, columnname)]

as select columnname, columnname from tablename;

Source Table: Source table is that identified in the select clause.Target Table: The target table is one specified In the create statement.

The above SQL statement will populate target table with the data from source table.

[email protected]

Page 22: DDL (Data Definition Language)

• (….continued)

• Example:

create table course(Coursename, Fees, Stud_admit)as select Coursename, Fees, Stud_admit

from college_info;

If the source table college_info was populated with records these will be uploaded in course table.

[email protected]

Page 23: DDL (Data Definition Language)

• How to create table structure from Source Table?

• To create a target table without records from source table (only structure creation) , the select statement should contain a ‘where clause’.

• The condition specified in where clause must not be satisfied.

[email protected]

Page 24: DDL (Data Definition Language)

• Inserting Data Into a Table From Another Table:

Oracle allows to populate a table with data that already exist in another table. (one row at a time into a table).

Insertion of records from one table to another:Syntax:

insert into tablenameselect columnname, columnname

from tablename;

Example:insert into Client_master

select client_name, addrfrom client_info;

[email protected]

Page 25: DDL (Data Definition Language)

Insertion of data set from one table to another:Syntax:

insert into tablenameselect columnname, columnname

from tablename where column=expr;

Example:insert into Client_master

select client_name, addrfrom client_info where client_name=‘Akash’;

[email protected]

Page 26: DDL (Data Definition Language)

• Delete Operations:

The Delete command is used to remove rows from table. We can use delete command for two operations.

1. To delete all the rows from table2. To delete a set of rows.

• Removal of All Rows:Syntax:delete from tablename;Example:delete from client_master;

[email protected]

Page 27: DDL (Data Definition Language)

• (…continued)Removal Of Specified Rows:

Syntax:delete from tablename

where search condition;

Example:delete from client_master

where bal_due<1000;

[email protected]

Page 28: DDL (Data Definition Language)

• Updating Contents Of A Table:

Update command is used to change data values in a table.

We can use update commandTo update all rows from a tableTo update data set of rows from a table

Updating All Rows:

Syntax:update tablename

set columnname=‘expr’;Example:

update client_masterset bal_due=5000; [email protected]

Page 29: DDL (Data Definition Language)

• Updating Records Conditionally: Syntax:

update tablenameset columnname=‘expr’where search condition;

Example:update client_master

set bal_due=5000where client_name=‘Thomus’;[email protected]

Page 30: DDL (Data Definition Language)

• How to Modify Structure Of Table?

We can modify structure of table by,1. Adding new column2. Modifying existing column

To Add New Columns:Syntax:

alter table tablenameadd(newcolumnname datatype(size),…..n);

alter table client_masteradd(client_tele numer(8));

[email protected]

Page 31: DDL (Data Definition Language)

• Modifying Existing Column:

Syntax:

alter table tablename

modify(columnname newdatatype(newsize),…..n);Example:

alter table client_masteradd(client_tele numer(10));

[email protected]

Page 32: DDL (Data Definition Language)

• Restrictions on the Alter Table:

Alter table cannot be performedto change name of the tableto change name of the columnto drop a columnDecrease size of a column if table data exists

[email protected]

Page 33: DDL (Data Definition Language)

• To Rename Tables:

Syntax:

rename oldname to newname;

Example:rename client_master to client_info;

[email protected]

Page 34: DDL (Data Definition Language)

• Destroying Table:

Syntax:drop table tablename;

Example:drop table client_master;

[email protected]

Page 35: DDL (Data Definition Language)

• Examining Objects Created By User:

select * from tab;

This command will display object name and type of object.

[email protected]