Oracle 10g SQL
Oracle 10g SQL
Ans) SQL> 2 3 4 create table try as select * from employees where 1=0;
Table created. SQL> desc try; Name ----------------------------------------EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID SQL> select * from try; no rows selected Q2)Difference between USER_CONSTRAINTS and DBA_CONSTRAINTS Ans)DBA_CONSTRAINTS describes all constraint definitions in the database. user can desc this constraint if it is connected as sysdba; USER_CONSTRAINTS describes constraint definitions on tables in the current user' s schema. user can describe this constraint without connecting as sysdba. Q3)What are different types of constraits. Explain with example. Ans)SQL PRIMARY KEY Constraint --------------------------The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain unique values. A primary key column cannot contain NULL values. Each table should have a primary key, and each table can have only ONE primary k ey. e.g CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Null? Type -------- ---------------------------NUMBER(6) VARCHAR2(20) NOT NULL VARCHAR2(25) NOT NULL VARCHAR2(25) VARCHAR2(20) NOT NULL DATE NOT NULL VARCHAR2(10) NUMBER(8,2) NUMBER(2,2) NUMBER(6) NUMBER(4)
Address varchar(255), City varchar(255), PRIMARY KEY (P_Id) ) SQL UNIQUE Constraint ------------------------The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness f or a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it. Note that you can have many UNIQUE constraints per table, but only one PRIMARY K EY constraint per table. e.g CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), UNIQUE (P_Id) ) SQL NOT NULL Constraint ----------------------------The NOT NULL constraint enforces a column to NOT accept NULL values. The NOT NULL constraint enforces a field to always contain a value. This means t hat you cannot insert a new record, or update a record without adding a value to this field. The following SQL enforces the "P_Id" column and the "LastName" column to not ac cept NULL values: e.g CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) SQL CHECK Constraint ---------------------------The CHECK constraint is used to limit the value range that can be placed in a co lumn. If you define a CHECK constraint on a single column it allows only certain value s for this column. If you define a CHECK constraint on a table it can limit the values in certain c olumns based on values in other columns in the row. e.g CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255),
Address varchar(255), City varchar(255), CHECK (P_Id>0) ) SQL FOREIGN KEY Constraint -----------------------------A FOREIGN KEY in one table points to a PRIMARY KEY in another table. e.g CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) ) Q4) Define Index with example. Ans) Indexes -----------An index can be created in a table to find data more quickly and efficiently. The users cannot see the indexes, they are just used to speed up searches/querie s. Note: Updating a table with indexes takes more time than updating a table withou t (because the indexes also need an update). So you should only create indexes o n columns (and tables) that will be frequently searched against. e.g CREATE INDEX PIndex ON Persons (LastName) Q5)Different data types in SQL Ans) Q6) How many unique key constraint can be used with primary key. Ans)Note that you can have many UNIQUE constraints per table, but only one PRIMA RY KEY constraint per table.