Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Class 4

Completed- DBMS Characteristics


• Terms and Terminologies √
• Data, Information, Database, DBMS, Database System, Table/Relation/Entity type,
Attributes/Columns/Fields, rows/tuples/instances/records, Schema, sub schema,
• Self Describing Nature of Database √
• Metadata
• Data Independence √
• Physical and Logical Data Independence, Data Abstraction
• Consistency √
• Change in any level should be reflected in all related levels

• Controlled Redundancy √
• Achieving Complex Relationship and Integrity – In Progress
Integrity Constraints
Type of Integrity Constraints
• Entity Integrity Constraint
• Referential Integrity Constraint
• Domain Integrity Constraint
• Self Referential Integrity Constraint
Keys
1. Candidate Key
2. Primary Key
3. Unique Key
4. Super Key
5. Foreign Key
6. Check constraint
7. Not Null
8. Default Constraint
9. Recrsive foreign Key
Candidate Key – Theoretical Key
Definition: An attribute or set of attributes that will retrieve unique rows
from the relation
Candidate Key # Attribute
Example: Student 1 Rollno
2 Name ,address
1. Rollno
3 Email
2. Name 4 Mobile
3. address 5 Rollno, name
.
4. Mobile
.
5. Email
Primary Key
• Uniqueness
• Not Null
• Will not change its value over lifetime
• Not an intelligent key

• Except Candidate key 1 other keys may fail in any one of the
properties. Rollno is the Primary key
Unique Key
• Unique
• But can be Null

• Example: Email, Mobile


Super key
• Bill book(date, sno , product_name, Price)
• Sno cycle
• Other attributes may not satisfy primary key properties
• Minimal Candidate key
Foreign Key
• A foreign key is a key used to link two tables together. This is
sometimes also called as a referencing key.
• A Foreign Key is a column or a combination of columns whose values
match a Primary Key in a different table.
• The relationship between 2 tables matches the Primary Key in one
of the tables with a Foreign Key in the second table.

• Master Table/Parent Table- Primary key


• Slave/Child Table – Foreign Key
CREATE TABLE CUSTOMERS CREATE TABLE ORDERS
( ID INT NOT NULL, ( ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL, DATE DATETIME,
AGE INT NOT NULL, CUSTOMER_ID INT references CUSTOMERS(ID),
ADDRESS CHAR (25) , AMOUNT double,
SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
PRIMARY KEY (ID) );

Insert into Customers Values(‘1’, ‘a’,’18’,’abc’,123456);


Insert into Orders values(‘o1’,’12-jan-21’, ‘1’, 123478);
CREATE TABLE CUSTOMERS CREATE TABLE ORDERS
( ID INT NOT NULL, ( ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL, DATE DATETIME,
AGE INT NOT NULL, CUSTOMER_ID INT references CUSTOMERS(ID),
ADDRESS CHAR (25) , AMOUNT double,
SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
PRIMARY KEY (ID) );
CREATE TABLE CUSTOMERS CREATE TABLE ORDERS
( ID INT NOT NULL, ( ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL, DATE DATETIME,
AGE INT NOT NULL, CUSTOMER_ID INT references CUSTOMERS(ID),
ADDRESS CHAR (25) , AMOUNT double,
SALARY DECIMAL (18, 2), PRIMARY KEY (ID) );
PRIMARY KEY (ID) );

Insert into Customers Values(1, ‘a’,’18’,’abc’,123456);


Insert into Orders values(‘o1’,’12-jan-21’, 1, 123478);
What will happen if
• Insert into Orders values(‘o1’,’12-jan-21’, ‘2’, 123478);
• Insert into Customers Values(‘a’,’18’,’abc’,123456);
Example 2
• Student(Rollno, Name)
• Course(Rollno, Departmet, Course code )

• Insert into Student Values(‘19Z201’, ‘A’);


• Insert into Course values(‘19Z201’, CSE,’19Z404’);
• Insert into Course values(‘19Z201’, CSE,’19Z405’);

• Rollno 19Z201 has enrolled for 2 courses


Check Constraint
• CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    CHECK (Age>=18)
);
Not Null
• CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    CHECK (Age>=18)
);
Default Constraint
• CREATE TABLE Persons (
    ID int NOT NULL,
    LastName varchar(255) NOT NULL,
    FirstName varchar(255),
    Age int,
    City varchar(255) DEFAULT ‘Coimbatore'
);
Recursive foreign Key
• foreign key is a key which is act as a primary key in another table..A
foreign key can be a column or a set of columns. The columns in the
child table often refer to the primary keycolumns in the parent table.
• recursive foreign key is a foreign key which refers back to the primary
key in the same table where child and parent table are same.
Mapping Integrity Constraints to Keys
• Entity Integrity Constraint – Primary Key, Composite Primary key,
Unique Key

• Referential Integrity Constraint – Foreign Key

• Domain Integrity Constraint – Check, Not null, default

• Self Referential Integrity Constraint – Recursive Foreign Key

You might also like