You are on page 1of 19

SANTHIGIRI COLLEGE OF COMPUTER SCIENCES

(AFFILIATED TO MG UNIVERSITY AND APPROVED BY AICTE)

VAZHITHALA P.O. THODUPUZHA

DEPARTMENT OF COMPUTER SCIENCES

SEMINAR REPORT ON PRIMARY KEY CONSTRIANT ENABLING AND DISABLING


Submitted to:
Mr.Gibin George Dept. of Computer Science

Submitted by:
Akhil C R No: 2 S3 MCA

PRIMARY KEY CONSTRIANT ENABLING AND DISABLING

INTRODUCTION

A database is a collection of related data items, typically describing the activities of one or more related organizations. A database might contain information about the entities in it as well as the relationships amongst them. For example, in a university database, The entities can be students, faculty, classes etc. Relationships can be activities such as enrollment of students in various courses, faculty teaching courses and so on. A Database Management System or DBMS, is a software designed to assist in maintaining and utilizing large collections of data. The need for such systems, as well as their use is growing rapidly.

QUERIES

The ease with which the data can be obtained from the database often determines its value to a user. In contrast to the older database systems, relational database systems allows a rich class of questions to be posed easily, this feature has contributed much to its popularity.

Such questions involving the data stored in a database are called are queries. A database provides a specialized query language, in which queries are posed. Relational data model is one that stores data in the form of tables. A very attractive feature of the relational model is that it supports powerful query languages. Relational calculus is a formal query language based on mathematical logic, and queries in this language have an intuitive, precise meaning. Relational Algebra is another formal query language, based on a collection of operators for manipulating relations, which is equivalent in power to the calculus.

PRIMARY KEY CONSTRAINT


Primary Key

A primary key is a single field or combination of fields that uniquely defines a record. None of the fields that are part of the primary key can contain a null value. A table can have only one primary key. CREATE TABLE table name (column1 data type null/not null, column2 data type null/not null, ... CONSTRAINT constraint_name PRIMARY KEY (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) );

EXAMPLES
The execution of the queries with primary key in the tables can be understood from the following examples. We have to create the following 3 tables : TASKS 1. 2. 3. 4. 5. 6. Declaring primary key immediate after the variable. Declaring primary key at the end of the create statement. Assign name to the primary key. Disabling the primary key. Enabling the primary key. Combined primary key. Student Employee Book Bookissue

1. DECLARING THE PRIMARY KEY IMMEDIATE AFTER THE VARIABLE.

Query create table student1(sid integer primary key,sname varchar(10)); Output

Here the NOT NULL indicates primary key.

Query

select * from student1;

Output

2. DECLARING PRIMARY KEY AT THE END OF CREATE STATEMENT.

Query

create table empl(eid integer,ename varchar(10) primary key(eid));

Output

3. GIVING NAME TO THE PRIMARY KEY.

Query create table book(bid integer,bname varchar(10),constraint pk51 primary key(bid));

10

4. DISABLING THE PRIMARY KEY Query

alter table book disable constraint pk51;

Output

11

Entering data with same primary key.

Even we have multiple data with same primary key, no error has been occurred because we have already disabled the primary key.

12

5. ENABLING THE PRIMARY KEY

We cannot enable the primary key because we have many rows with same primary key. So it shows an error of primary key violation. So we have to delete the repeated data by delete command.

13

Deleting the repeated data

14

Enable the primary key after deleting data with same primary key. Query alter table book enable constraint pk51;

Now the table is successfully altered because no repeated data is available on the table book;

15

Entering the repeated data after enabling primary key

Now it shows error because primary key cannot be repeated in a table.

16

6. COMBINED PRIMARY KEY Query create table bookissue(bid integer,sid integer,bname varchar(10),primary key(bid,sid)); Output

17

CONCLUSION

In relational database design, a unique key can uniquely identify each row in a table, and is closely related to the Superkey concept. A unique key comprises a single column or a set of columns. No two distinct rows in a table can have the same value (or combination of values) in those columns if NULL values are not used. Depending on its design, a table may have arbitrarily many unique keys but at most one primary key. A unique key constraint does not imply the NOT NULL constraint in practice. Because NULL is not an actual value (it represents the lack of a value), when two rows are compared, and both rows have NULL in a column, the column values are not considered to be equal. Thus, in order for a unique key to uniquely identify each row in a table, NULL values must not be used. According to the SQL standard and Relational Model theory, a unique key (unique constraint) should accept NULL in several rows/tuples however not all RDBMS implement this feature correctly

18

REFERENCE

DataBase Management Systems - Raghu Ramakrishnan - Johannes Gehrke

19

You might also like