Sei sulla pagina 1di 3

Relational Model

17. What is Key for a Table (Primary key)?


Set of attributes which can identify records uniquely from a table is called Key.

Student(Rollno, Name, Age, Bname, Perc)

18. What are the various types of Keys?


Candidate Key, Super Key, Primary Key and Foreign Key

19. What is the difference among super, primary, candidate and foreign keys?
A super key of a table is a set of attributes which can determine all other attribute values in the give
table. In other words, set of attributes which can identify the records uniquely from the table.
No two rows can have the same value of super key attributes.
A Candidate key is minimal superkey. i.e. Minimal set of attributes that can determine all attribute
values in the given table.
A Primary Key is one of the candidate keys. One of the candidate keys is selected as most important and
becomes the primary key. There cannot be more that one primary key in a table. Foreign
key is a field (or collection of fields) in one table that uniquely identifies a row of another table.

20. Explain Foreign Key with example.


Foreign key is a field (or collection of fields) in one table that uniquely identifies a row of
another table.
Example:
Consider the tables:
Student(regdno, name, age, bname, perc)
Branch(bname, yoe, intake)

In the above example, Student table gives information about students. Branch table gives information
about existing branches in the college. The bname values that come in Student table should be there in
Branch table (in other words, the bname value of any record in student table identifies a row of Branch
table uniquely).

Create table Branch(bname varchar(20) primary key, yoe number(4), intake number(3));
Create table Student(regdno varchar(10) primary key, name varchar(20), age number(2),
bname varchar(20), perc number(5,2), foreign key(bname) references Branch);
Note:
Datatypes in Oracle: integer, number, char, varchar, date, blob
Difference between char() and varchar(): char() is a fixed length character string, where as varchar() is a
variable length character string.
Blob – Blob stands for Binary large object. It is used to store images, files etc.

21. Can a table have more than one primary key?


No

22. Can a table have more than one foreign key?


YES.
Example:
Sailors(sid, sname,age,rating)
Reservers(sid, bid, day)
Boats(bid, bname, color)

In the above example, the table RESERVES has 2 foreign keys. One is (sid) references Sailors table &
another foreign key is (bid) references Boats table.

23. What is the difference between primary key and unique constraints?
Primary key cannot have NULL value, the unique constraints can have NULL values.
There is only one primary key in a table, but there can be multiple unique constrains.
Primary Key = Unique + Not Null

24. What is SQL?


SQL stands for Structured Query Language. It is the default relational database query language.
We can communicate with an RDBMS using Structured Query Language (SQL).
Query Language:
Query means Request for Information. Query Language is the language used to express queries
to Database System.

25. What is DDL, DML, DCL and TCL in SQL?


DDL stands for Data Definition Language. SQL queries like CREATE, ALTER, DROP, TRUNCATE
come under this.
DML stands for Data Manipulation Language. SQL queries like INSERT, UPDATE, DELETE and
SELECT come under this.
DCL stands for Data Control Language. SQL queries like GRANT and REVOKE come under this.
TCL stands Transaction Control Language. Examples: COMMIT and ROLLBACK;

26. Explain the various ways a table can be created in Oracle.

create table <tablename>(col1 datatype, col2 datatype,...);


create table <tablename> as <select query>;
The 2nd syntax is used to create a table from an existing table i.e. it will create the table, as well as
populate the table as specified by select query.

27. Explain various forms of ALTER TABLE command.


ALTER TABLE command can be used to alter table definitions i.e. we can add new columns, we can
modify column size, we can drop existing columns, we can add & drop constraints onto the table.
ALTER TABLE <tablename> ADD <ColName> <datatype>
ALTER TABLE <tablename> MODIFY <existing colname> <datatype with newsize>
ALTER TABLE <tablename> DROP COLUMN <columnname>
ALTER TABLE <tablename> ADD CONSTRAINT <constraintname> <definition>
ALTER TABLE <tablename> DROP CONSTRAINT <constraintname>
Examples: Consider the table R(A,B,C,D). Assume suitable data types
→ Add column E:String to the above table
ALTER TABLE R ADD E VARCHAR(20);
→ Remove column C from the above Table
ALTER TABLE R DROP COLUMN C;
→ Make column A as primary key for the above table
ALTER TABLE R ADD PRIMARY KEY(A);
→ Remove Primary Key from Table R
ALTER TABLE R DROP PRIMARY KEY;
→ Enforce the condition (C>5) on the above table
ALTER TABLE R ADD CONSTRAINT chk_on_c CHECK(C>5);

28. What is the difference between TRUNCATE, DELETE & DROP commands?
Truncate command removes all the records from the given table. Delete command is used delete/remove
records that satisfy the given condition. Whereas, Drop command removes the table.

The difference between TRUNCATE and DELETE is that the action of TRUNCATE can’t be rolled back,
whereas the action of DELETE can be rolled back.

Further Explanation:
The DELETE command is used to remove rows from a table. A WHERE clause can be used
to remove only some rows. If no WHERE condition is specified, all rows will be removed. After
performing a DELETE operation you need to COMMIT or ROLLBACK the transaction to make the
change permanent or to undo it. Note that this operation will cause all DELETE triggers on the table to
fire.

Potrebbero piacerti anche