Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Day-3 (DBMS)

Date:
Add column
Syntax:
alter table <table_name>
add(column name type(size), column name type(size),…) ;

Code:
alter table order_detail
add(order_amount number(6));

Change of the column Name at the view level:


Syntax
Select <column_name> <alias name>, <column_name> <alias name>, … from
<table_name>;
Example:
select unit_price u_p from order_detail;

Modify a Table:
alter table <table_name>
modify(column_name newtype(new size));

Example:
alter table client_master
modify (phone NUMBER(10));

To show the list of Tables of a schema:

SQL> select * from tab;

To show the fields of a table:


SQL> desc <table name>;
SQL> desc order_detail;

To show all the columns at the same line

SQL> SET LIN9000;

To save recent modification

SQL> COMMIT;

Delete a column from the table


SQL> ALTER TABLE <table name> DROP(<column name>);
SQL> ALTER TABLE order_detail DROP(unit_price);
Rename of a table
SQL> RENAME <old name> to <new name>;
SQL> RENAME order_detail TO detail_order;

Constraint
Primary key:
create table <table name>
( <column name> <type>(<size>) PRIMARY KEY,
<column name> <type>(<size>) NOT NULL,
<column name> <type>(<size>));

Example:
SQL> create table client_master
2 (id varchar2(6) primary key,
3 name varchar2(30) ,
4 address varchar2(50),
5 phone varchar2(13));

OR
SQL> create table client_master
2 (id varchar2(6),
3 name varchar2(30),
4 address varchar2(50),
5 phone varchar2(13),
6 --
7 constraint pk_id_tbl_client_master primary key(id));
[Here pk_id_tbl_client_master is the name of ID column]

Not Null constraint:


This is alternative way to make not null
SQL> alter table client_master modify(name constraint nn_name not null);

Foreign Key:
Foreign key(<column name>) references <table name>(<column name>)
Example-1
SQL> create table client_order
2 (client_id varchar2(4),
3 order_id varchar2(6),
4 --
5 constraint fk_client_id_tbl_client_id foreign key(client_id) references
client_master(ID) on delete cascade);

Example-2:
create table order_detail
(order_id varchar2(6) primary key,
item_code varchar2(7),
unit_price number(8,2),
--
foreign key(order_id) references client_order(order_id));

Client_master
id name address phone
C_011 xx 17/G, FR 01817511852
C_012 yy 18/G, IR 089866565

Client_order
Client_id Order_id
C_011 Or_22
C_012 Or_23

Order_detail
Order_id Item_code item_name Unit_price
Or_22 Item_087 Marker 35.00
Or_23 Item_088 Pen 10.00

You might also like