It Is Object Level Command

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

1. It is Object Level Command.

2. DDL commands are useful to create or modify or delete database


objects Like tables, views, indexes, sequences, synonyms, users,
functions, procedures, triggers, packages and so on.
1.1 Create Command
1. Create Table command is used to create a new table.
2. Create is part of Data Definition Language (DDL).
3. While creating a table we provide the basic information for each column
together with their data type and sizes.
4. Create a table ,you must have create table privilege.
Note: In oracle, in a table, Max we can maintain 1000 columns.

CREATE TABLE EMP (


EMPNO NUMBER(4, 0),
ENAME VARCHAR2(10 BYTE),
JOB VARCHAR2(9 BYTE),
MGR NUMBER(4, 0),
HIREDATE DATE,
SAL NUMBER(7, 2),
COMM NUMBER(7, 2),
DEPTNO NUMBER(2, 0)
);

• Create Table with Select Statement


We can create a table using existing table [along with data].
CREATE TABLE EMP1 AS SELECT * FROM EMP;

• Creating table with your own column names.


create table emp2 (empno,ename,job) as select * from emp;
Note:
In the above query two tables no. of columns must be same.

• Creating table with specified columns.


create table emp3 as select empno,ename from emp;

• Creating table without table data.


create table emp4 as select * From emp where 1=2;
Note:
In all databases whenever we are copying a table from another table internally
constraints (Primary key, foreign key) are never copied.
1.2 Alter Command
This command is used to change or modify the structure of a
database object i.e.
1. Increase/decrease the length of a column.
2. Change the data type of a column.
3. Change Not Null to Null or Null to Not Null.
4. Used to add a new column to an existing table.
5. Used to drop an existing column.
6. Add a new constraint.
7. Used to drop an existing constraint on a table.
8. To change the column name of a table.

• add a column to table


alter table agent1 add email char(25);

• drop a column
alter table agent1 drop(country);

• change or drop default value of a column


alter table agent1 modify commission default .05;

• drop default value of a column


alter table agent1 modify commission number;

• add individual column constraint


alter table agent1 add constraint dup_che_con unique(agent_code);

• drop individual column constraint


alter table agent1 drop constraint dup_che_con;

• change size and data type of a column


alter table agent1 modify (country varchar2(35));

• add or drop primary key of a table


alter table agent1
add constraint pk_ag_code primary key(agent_code);

• drop existing primary key of a table


alter table agent1
drop constraint pk_ag_code;

• add or drop foreign key of a table


alter table customer1

add constraint fk_ag_code foreign key (agent_code)

references agents(agent_code);

• drop existing foreign key of a table


alter table customer1
drop constraint fk_ag_code;

• add check constraint


alter table customer1
add constraint du_che_con check(grade>=1 and grade<=3);

• drop check constraint


alter table customer1 drop constraint du_che_con;

• change primary key constraint


alter table agent1
drop constraint pk_ag_code;
alter table agent1
add constraint pk_ag_code primary key(agent_code);

• change foreign key constraint


alter table customer1 drop constraint fk_ag_code;
alter table customer1 add constraint fk_ag_code foreign key (agent_code)
references agents(agent_code);
• adding multiple columns
alter table orders add (customer_name varchar2(20),payment varchar2(20));

• modify columns
alter table orders modify customer_name varchar2(15);

• modify multiple columns


alter table orders
modify (customer_name varchar2(10),payment varchar2(10));

• rename a column
alter table orders rename column payment to payment_mode;

• rename table
alter table orders rename to shoppers;

• add a not null


alter table table_name modify column_name datatype not null;

• add unique constraint


alter table table_name add constraint myuniqueconstraint unique(column1,
column2...);

1.3 Drop Table:

1. Drop command is used to remove the object from the database.

2. The DROP command not only removes the table but also

removes table-based indexes, privileges, and constraints.


3. At a later stage even if we wanted, we will not be able to get the
table or its data, as it is permanently removed.
Syntax : drop table <tablename>;
drop table emp;
1.4 Rename Table
1. Rename command is used to rename the object in database.
2. Rename command is part of DDL (Data Definition Language)
Syntax: rename <old table name> to <new table name>;
rename emp to na_emp;

1.5 Truncate Table


1. Truncate command is used to delete the data permanently from the database table.
2. Truncate command is part of DDL (Data Definition Language).
3. System does AUTO commit after the deletion and hence the data deleted cannot be
rolled back.
4. Using Truncate we cannot delete partial records from the database.
5. Once we run truncate command, system will delete all the records permanently from
the database.
syntax: truncate table <tablename>
truncate table emp;

select b.*,
sum (weight) over (order by weight
rows between 1 preceding and current row
) running_row_weight,
sum (weight) over (order by weight
range between 1 preceding and current row) running_value_weight
from bricks b
order by weight, brick_id;

BRICK_ID COLOUR SHAPE WEIGHT RUNNING_ROW_WEIGHT RUNNING_VALUE_WEIGHT


---------- ---------- ---------- ---------- ------------------ --------------------
1 blue cube 1 1 3
3 red cube 1 2 3
6 green pyramid 1 2 3
2 blue pyramid 2 3 7
4 red cube 2 4 7
5 red pyramid 3 5 7

You might also like