DBMS Module 2
DBMS Module 2
An attribute that can uniquely identify a tuple in a relation is called the key
of the table.
The value of the attribute for different tuples in the relation has to be
unique.
Types of key
Candidate key
Super key
Primary key
Alternate key
Foreign key
CandidateKey
Stu_id name phone city email
100 jhj 7897 ban vj
Candidate key is a set of attributes (or attribute) which uniquely identify the tuples in a
relation or table.
Ex. Student{ID, F_name, M_name, L_name, Age}
two candidate keys ID and {First_name, Last_name, DOB}. So here, there are present more
than one candidate keys, which can uniquely identifies a tuple in a relation.
5
Stuid,phone,email
(stuid,name)(stuid phone)(stuid email)
Nc(n/2)=5c2=10
4c2
6c3
Super key
Primary Key is a set of attributes (or attribute) which uniquely identify the
tuples in a relation or table.
Primary key is a minimal super key, so there is one and only one primary key
in any relation.
Ex. Student{ID, F_name, M_name, L_name, Age} Here only ID can be primary
key because name, age and address can be same but ID can’t be same.
Alternate key
A table can have multiple candidate keys. Among these candidate keys, only
one key gets selected as primary key, the remaining keys are known as
alternative or secondary keys.{stuid,emp_number}
Foreign key
Foreign keys are the columns of a table that points to the primary key of
another table. They act as a cross-reference between tables.
Fk is used to link 2 tables together
SQL
SQL is a database computer language designed for the retrieval
and management of data in a relational database. SQL stands
for Structured Query Language.
Why to Learn SQL?
RDBMS stands
for Relational Database Management
System. RDBMS is the basis for SQL, and
for all modern database systems like MS
SQL Server, IBM DB2, Oracle, MySQL, and
Microsoft Access.
A Relational database management
system (RDBMS) is a database
management system (DBMS) that is
based on the relational model as
introduced by E. F. Codd.
What is a table?
⮚ Every table is broken up into smaller entities called fields. The fields in
the CUSTOMERS table consist of ID, NAME, AGE, ADDRESS and SALARY.
⮚ A field is a column in a table that is designed to maintain specific
information about every record in the table.
Fields and Records
Records
⮚ A record is also called as a row of data is each
individual entry that exists in a table.
⮚ For example, there are 7 records in the above
CUSTOMERS table. Following is a single row of
data or record in the CUSTOMERS table −
SQL SYNTAX
SQL is followed by a unique set of rules and guidelines called
Syntax.
All the SQL statements start with any of the keywords like SELECT,
INSERT, UPDATE, DELETE, ALTER, DROP, CREATE, USE, SHOW and
all the statements end with a semicolon (;).
EMPno JOB Hiredate Sal Deptno
Number(4) Varchar(15 Date Number(5, Number(2)
) 2)
100 Account 21-08-2000 20000.50
1.ER diagram
Entity-Emp Attributes-EMPno,JOB,Hiredate,Sal,Deptno
2.
3.Primary key
4.sales-5char account-8char
5programms
DBMS Languages
DDL
Syntax
The basic syntax of the USE statement is as shown below −
USE DatabaseName;
2) SELECT COUNT(*)
FROM PRODUCTS;
WHERE RATE>=20;
3) SELECT COUNT(DISTINCT COMPANY) FROM PRODUCTS;
4)SELECT COMPANY, COUNT(*)
FROM PRODUCTS
GROUP BY COMPANY;
5) SELECT COMPANY, COUNT(*)
FROM PRODUCTS
GROUP BY COMPANY
HAVING COUNT(*)>2;
2. SUM Function