Showing posts with label oracle ddl commands. Show all posts
Showing posts with label oracle ddl commands. Show all posts

Saturday, March 12, 2011

Foreign Key


What is a foreign key?
A foreign key means that values in one table must also appear in another table.
The referenced table is called the parent table while the table with the foreign key is called the child table. The foreign key in the child table will generally reference a primary key in the parent table.
A foreign key can be defined in either a CREATE TABLE statement or an ALTER TABLE statement.
Using a CREATE TABLE statement
The syntax for creating a foreign key using a CREATE TABLE statement is:
CREATE TABLE table_name
(column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT fk_column
  FOREIGN KEY (column1, column2, ... column_n)
  REFERENCES parent_table (column1, column2, ... column_n)
);

For example:
CREATE TABLE supplier
(
supplier_id
numeric(10)
not null,
supplier_name
varchar2(50)
not null,
contact_name
varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);

CREATE TABLE products
(
product_id
numeric(10)
not null,
supplier_id
numeric(10)
not null,
CONSTRAINT fk_supplier
  FOREIGN KEY (supplier_id)
  REFERENCES supplier(supplier_id)
);
In this example, we've created a primary key on the supplier table called supplier_pk. It consists of only one field - the supplier_id field. Then we've created a foreign key called fk_supplier on the products table that references the supplier table based on the supplier_id field.
We could also create a foreign key with more than one field as in the example below:
CREATE TABLE supplier
(
supplier_id
numeric(10)
not null,
supplier_name
varchar2(50)
not null,
contact_name
varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id, supplier_name)
);

CREATE TABLE products
(
product_id
numeric(10)
not null,
supplier_id
numeric(10)
not null,
supplier_name
varchar2(50)
not null,
CONSTRAINT fk_supplier_comp
  FOREIGN KEY (supplier_id, supplier_name)
  REFERENCES supplier(supplier_id, supplier_name)
);
In this example, our foreign key called fk_foreign_comp references the supplier table based on two fields - the supplier_id and supplier_name fields.

Using an ALTER TABLE statement
The syntax for creating a foreign key in an ALTER TABLE statement is:
ALTER TABLE table_name
add CONSTRAINT constraint_name
  FOREIGN KEY (column1, column2, ... column_n)
  REFERENCES parent_table (column1, column2, ... column_n);
For example:
ALTER TABLE products
add CONSTRAINT fk_supplier
  FOREIGN KEY (supplier_id)
  REFERENCES supplier(supplier_id);
In this example, we've created a foreign key called fk_supplier that references the supplier table based on the supplier_id field.
We could also create a foreign key with more than one field as in the example below:
ALTER TABLE products
add CONSTRAINT fk_supplier
  FOREIGN KEY (supplier_id, supplier_name)
  REFERENCES supplier(supplier_id, supplier_name);

Wednesday, March 9, 2011

ORACLE DML DDL DCL TCL


ORACLE DML Statement (Data manipulation language)
1.       Delete
·         Delete all records from a table , the space for the record remain.
2.       Insert
·         Insert data in a table.
3.       Marge
·         Upsets operation (insert & update).
4.       Select
·         Retrieve data from a table
5.       Update
·         Update existing data within a table.
6.       Call
·         Call a PL/SQL or Java program.
7.       Lock table  
·         Control concurrency.


DCL (Data Control Language)

1.       Grant
·         Gives user access privileges to database.
2.       Revoke
·         Withdraw access privileges given with the grant command.


TCL(Transaction Control)

1.       Commit
·         Save work done.
2.       Save point
·         Indentify a point in a transition to which you can latter rollback.
3.       Rollback
·         Restore database to original since the last commit.
4.       Set transition
·         Change transition option like  isolation level and what rollback segment to use.


DDL(Data Definition language)

1.       Create
·         To create object in the database.
2.       Alter
·         Alter the structure of the database.
3.       Drop
·         Delete object of the database.
4.       Truncate
·         Remove the record from a table, including all spaces allocated for the record are removed.
5.       Commit
·         Add commends to the data dictionary.
6.       Rename
·         Rename an object.

Welcome your suggestion