Dbms
Dbms
Key
An Attribute/group of attributes in a table that identifies a tuple uniquely is
known as a key.
Primary key
Primary key is a key in a relational database that is unique for each record. It is
a unique identifier, such as a driver license number, telephone number
(including area code), or vehicle identification number (VIN).
Candidate Keys
A table may have more than one such attribute/group of attributes that
identifies a tuple uniquely and are eligible for being a primary key, all such
attributes(s) are known as Candidate Keys.
Alternate Keys
A candidate key not selected as a primary key is called alternate or secondary
key.
Foreign Key
A Foreign Key is defined in a second table, but it refers to the primary key in
the first table. It is used to ensure referential integrity in an RDBMS.
Relational algebra
Relational algebra is a formal system for manipulating relations. Set of operations
that can be carried out on a relation:
Selection : To select a horizontal subset of a relation
Projection : To select vertical subset of a relation
Cartesian product: It operates on two relations and is denoted by X. for
example Cartesian product of two relation R1 and R2 is represented by
R=R1X R2. The degree of R is equal to sum of degrees of R1 and R2. The
cardinality of R is product of cardinality of R1 and cardinality of R2
Example Cartesian Product
The table R1
Empno Ename Dept
1 Bill A
2 Sarah C
3 John A
The table R2
Dno Dname
A Marketing
E-SCHOOL
B Sales
C Legal
R1 X R2
Empno Ename Dept Dno Dname
1 Bill A A Marketing
1 Bill A B Sales
1 Bill A C Legal
2 Sarah C A Marketing
2 Sarah C B Sales
2 Sarah C C Legal
3 John A A Marketing
3 John A B Sales
3 John A C Legal
Union
UNION is used to combine the results of two or more Select statements.
However it will eliminate duplicate rows from its result set. In case of union,
number of columns and data type must be same in both the tables.
Example of UNION
ID Name
ID Name
1 abhi 2 adam
2 adam Chester
3
ID NAME
1 Abhi
2 Adam
3 Chester
Intersect
Intersect operation is used to combine two SELECT statements, but it only
retuns the records which are common from both SELECT statements. In case
of Intersect the number of columns and datatype must be same. MySQL does
not support INTERSECT operator.
Example of Intersect
The First table,
ID NAME
1 Abhi
2 Adam
ID NAME
2 Adam
3 Chester
INTERSECT
E-SCHOOL
select * from second
ID NAME
2 Adam
Difference
Minus operation combines result of two Select statements and return only those
result which belongs to first set of result. MySQL does not support INTERSECT
operator.
Example of Minus
ID NAME
1 Abhi
2 Adam
ID NAME
2 Adam
3 Chester
MINUS
ID NAME
1 Abhi
Data Control Language (DCL) - used to control user access (e.g., GRANT,
REVOKE)
Transactional Language - used to control logical units of work (e.g., COMMIT,
ROLLBACk)
Data Types
CHAR(size) A FIXED length string (can contain letters, numbers, and special
characters). The size parameter specifies the column length in characters - can
be from 0 to 255. Default is 1.
char
VARCHAR
DDL
Example :
deptno int,
dname varchar(14),
loc varchar(13),
);
E-SCHOOL
create table emp(
empno int,
job varchar(9),
mgr int,
hiredate date,
sal int,
deptno int,
);
Examples :
Example:
Drop a column
Syntax:
Example:
Syntax:
Deleting a table
Syntax:
Example:
DML
Inserting a new row at the bottom of the table
Syntax :
E-SCHOOL
INSERT INTO table_name
VALUES (value1,value2,value3,...);
You can also specifies both the column names and the values to be inserted:
Examples
Name
Alex
Peter
Alisha
John
E-SCHOOL
Relational Operator
=, <, >, <=, >=, <>
Logical Operator
AND, OR, NOT
SELECT * FROM student WHERE fess > 7000 AND fees < 8000;
Name fees
Alisha 7800
E-SCHOOL
John 6900
Name fees
Alisha 7800
John 6900
Name
Alex
Alisha
Name
Alex
Peter
Class
K12
K11
Example
SELECT class, name, dob, fees FROM student ORDER BY class, name DESC;
select day('1999-10-11'),month('1999-10-11'),year('1999-10-11');
+-------------------+---------------------+--------------------+
| day('1999-10-11') | month('1999-10-11') | year('1999-10-11') |
+-------------------+---------------------+--------------------+
| 11 | 10 | 1999 |
E-SCHOOL
+-------------------+---------------------+--------------------+
COUNT(*)
COUNT(rno)
SUM(fees)
29300
AVG(fees)
7325.0000
MAX(fees) MIN(fees)
7900 6700
class SUM(fees)
K11 14800
K12 14500
class MAX(dob)
K11 2000-12-13
K12 1998-10-03
Note:- Above query will display the records of classes where minimum 2
students are studying. It could not be done using where clause as in test
expression using where we cannot use aggregate functions. Because where
work row wise where as having works after grouping.
Equi Join
E-SCHOOL
It is also known as an inner join. It is the most common join. It is based on
matched data as per the equality condition. The equi join uses the comparison
operator(=).
Syntax:
Table - product
Table - order_items
OR
Natural Join
Natural Join joins two tables based on same attribute name and datatypes. The
resulting table will contain all the attributes of both the table but keep only one
copy of each common column.
SELECT *
Example:- Write a query to display names of students having fees greater than
John’s fees
Solution:- Select name from student where fees>(select fees from student
where name=’John’)
name
Alex
Alisha
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
Example
WHERE rno = 12 ;
Example:
A view contains rows and columns, just like a real table. The fields in a view are
fields from one or more real tables in the database.
Example
Normalization:-
Database normalization is the process of organizing the attributes and tables of
a relational database to minimize data redundancy.
The three main types of normalization are listed below:-
Table is in 1NF
All non-key attributes of the table should be functionally dependent on
the whole key (Composite key), not on only one part of it.
Table is 2 NF
E-SCHOOL
Every non-key attribute should be fully functionally dependent on a single
key attribute
The difference is that CHAR (n) will ALWAYS be n bytes long. If the string length
is <n, it will be blank padded upon insert to ensure a length of n. A VARCHAR
(n) on the other hand will be 1 to n bytes long. A shorter string stored as
VARCHAR2 will NOT be blank padded.