Ch-11 Relational Databases
Ch-11 Relational Databases
Keys in database-Keys are very important part of Relational database model. They are used to establish
and identify relationships between tables and also to uniquely identify any record or row of data inside a table.
A Key can be a single attribute or a group of attributes, where the combination may act as a key.
KEYS IN A DATABASE
Key plays an important role in relational database; it is used for identifying unique rows
from table & establishes relationship among tables on need.
Types of keys in DBMS
Unique key
•A unique key in SQL is the set of fields or columns of a table that helps us
uniquely identify records. The unique key guarantees the uniqueness of the
columns in the database. It is similar to the primary key but can accept a null
value, unlike it. A primary key constraint automatically defines a unique key
constraint. More than one unique key can exist in a table, unlike the primary key,
which can exist only once.
Primary key
•It is the first key which is used to identify one and only one instance
of an entity uniquely. An entity can contain multiple keys as we saw
in PERSON table. The key which is most suitable from those lists
become a primary key.
•In the EMPLOYEE table, ID can be primary key since it is unique for
each employee. In the EMPLOYEE table, we can even select
License_Number and Passport_Number as primary key since they are
also unique.
•For each entity, selection of the primary key is based on
requirement and developers.
2. Candidate key
•A candidate key is an attribute or set of an attribute which
can uniquely identify a tuple.
•The remaining attributes except for primary key are
considered as a candidate key. The candidate keys are as
strong as the primary key.
•For example: In the EMPLOYEE table, id is best suited for
the primary key. Rest of the attributes like SSN,
Passport_Number, and License_Number, etc. are
considered as a candidate key.
Foreign key
•Foreign keys are the column of the table which is used to
point to the primary key of another table.
•In a company, every employee works in a specific
department, and employee and department are two
different entities. So we can't store the information of the
department in the employee table. That's why we link
these two tables through the primary key of one table.
•We add the primary key of the DEPARTMENT table,
Department_Id as a new attribute in the EMPLOYEE table.
•Now in the EMPLOYEE table, Department_Id is the foreign
key, and both the tables are related.
ALTERNATE KEYS is a column or group of columns in a table that uniquely identify every row in that
table. A table can have multiple choices for a primary key but only one can be set as the primary key. All
the keys which are not primary key are called an Alternate Key Or Candidate key which is not a primary
key is known as alternate key.
Example:
Referential Integrity
It is a system of rules that a DBMS uses to ensure that relationship between records in
related s tables are valid, and that user’s don’t accidently delete or change related data.
In order to access data from the MySQL database, all program and user must
use SQL (Structured Query Language). SQL is a set of commands that are
recognized by all the RDBMSs and has become a standard language for
database handling.
There are numerous version of SQL. The original version was developed at IBM’s
San Jose Research Laboratory with a name of Sequel, as a part of System R
project in 1970s. It was standardized by ANSI in 1986 by the name of SQL.
SQL is a Standard Query language, whereas MySQL is a DBMS Software based on SQL.
Features of MySQL:
i. Speed –
ii. Accuracy-
iii. Ease of use-
iv. Cost-
v. Query Language Support-
vi. Portability-
vii.Data types-
viii.Security-
ix. Connectivity-
x. Localization-
the chief inventor of MySQL was A. K. Monty(Michael Widenius). The name given
MySQL to it on the name of his daughter My.
Why SQL is required
SQL is required:
•To create new databases, tables and views
•To insert records in a database
•To update records in a database
•To delete records from a database
•To retrieve data from a database
INSERT: The INSERT statement is a SQL query. It is used to insert data into the row of a table.
UPDATE: This command is used to update or modify the value of a column in the table.
Rollback: Rollback command is used to undo transactions that have not already been saved to
the database.
SAVEPOINT: It is used to roll the transaction back to a certain point without rolling back the
entire transaction.
Commit: Commit command is used to save all the transactions to the database.
Data Query Language
DQL is used to fetch the data from the database.
It uses only one command:
SELECT: This is the same as the projection operation of relational algebra. It is used to select the
attribute based on the condition described by WHERE clause.
Syntax:
1.SELECT expressions
2.FROM TABLES
3.WHERE conditions;
For example:
4.SELECT emp_name
5.FROM employee
6.WHERE age > 20;
SQL Data Types
Data types are used to represent the nature of the data that can be stored in the database table.
For example, in a particular column of a table, if we want to store a string type of data then we will
have to declare a string data type of this column.
Data types mainly classified into three categories for every database.
•String Data types
•Numeric Data types
•Date and time Data types
CHAR(Size) It is used to specify a fixed length string that can contain numbers, letters, and
VARCHAR(Size) It is used to specify a variable length string that can contain numbers, letters,
BINARY(Size) It is equal to CHAR() but stores binary byte strings. Its size parameter specifies
VARBINARY(Size) It is equal to VARCHAR() but stores binary byte strings. Its size parameter
TEXT(Size) It holds a string that can contain a maximum length of 255 characters.
ENUM(val1, val2, It is used when a string object having only one value, chosen from a list of
val3,...) possible values. It contains 65535 values in an ENUM list. If you insert a value
SET( val1,val2,va It is used to specify a string that can have 0 or more values, chosen from a list of
l3,....) possible values. You can list up to 64 values at one time in a SET list.
BLOB(size) It is used for BLOBs (Binary Large Objects). It can hold up to 65,535 bytes.
MySQL Numeric Data Types
BIT(Size) It is used for a bit-value type. The number of bits per value is specified in size. Its size can
be 1 to 64. The default value is 1.
INT(size) It is used for the integer value. Its signed range varies from -2147483648 to 2147483647
and unsigned range varies from 0 to 4294967295. The size parameter specifies the max
display width that is 255.
FLOAT(size, d) It is used to specify a floating point number. Its size parameter specifies the total number
of digits. The number of digits after the decimal point is specified by d parameter.
FLOAT(p) It is used to specify a floating point number. MySQL used p parameter to determine
whether to use FLOAT or DOUBLE. If p is between 0 to24, the data type becomes FLOAT ().
If p is from 25 to 53, the data type becomes DOUBLE().
DOUBLE(size, d) It is a normal size floating point number. Its size parameter specifies the total
number of digits. The number of digits after the decimal is specified by d parameter.
DECIMAL(size, d) It is used to specify a fixed point number. Its size parameter specifies the total
number of digits. The number of digits after the decimal parameter is specified
by d parameter. The maximum value for the size is 65, and the default value is 10.
The maximum value for d is 30, and the default value is 0.
BOOL It is used to specify Boolean values true and false. Zero is considered as false, and
nonzero values are considered as true.
MySQL Date and Time Data Types
DATE It is used to specify date format YYYY-MM-DD. Its supported range is from '1000-
01-01' to '9999-12-31'.
DATETIME(fsp) It is used to specify date and time combination. Its format is YYYY-MM-DD
hh:mm:ss. Its supported range is from '1000-01-01 00:00:00' to 9999-12-31
23:59:59'.
TIMESTAMP(fsp) It is used to specify the timestamp. Its value is stored as the number of seconds
since the Unix epoch('1970-01-01 00:00:00' UTC). Its format is YYYY-MM-DD
hh:mm:ss. Its supported range is from '1970-01-01 00:00:01' UTC to '2038-01-09
03:14:07' UTC.
TIME(fsp) It is used to specify the time format. Its format is hh:mm:ss. Its supported range
is from '-838:59:59' to '838:59:59'
YEAR It is used to specify a year in four-digit format. Values allowed in four digit format
from 1901 to 2155, and 0000.
SQL Operators
SQL statements generally contain some reserved words or characters that are used to perform
operations such as comparison and arithmetical operations etc. These reserved words or
characters are known as operators.
Generally there are three types of operators in SQL:
1.SQL Arithmetic Operators
2.SQL Comparison Operators
3.SQL Logical Operators
SQL Arithmetic Operators:
= Examine both operands value that are equal or not,if (a=b) is not true
yes condition become true.
<> Examines the operand's value equal or not, if values (a<>b) is true
are not equal condition is true
> Examine the left operand value is greater than right (a>b) is not true
Operand, if yes condition becomes true
< Examines the left operand value is less than right (a<="" td="">
Operand, if yes condition becomes true
>= Examines that the value of left operand is greater than (a>=b) is not true
or equal to the value of right operand or not,if yes
condition become true
<= Examines that the value of left operand is less than or (a<=b) is true
equal to the value of right operand or not, if yes
condition becomes true
!< Examines that the left operand value is not less than (a!<="" td="">
the right operand value
!> Examines that the value of left operand is not greater (a!>b) is true
than the value of right operand
SQL Logical Operators:
Operator Description
ALL this is used to compare a value to all values in another value set.
ANY this operator is used to compare the value in list according to the
condition.
BETWEEN this operator is used to search for values, that are within a set of values
EXISTS the EXISTS operator is used to search for the presence of a row in a specified
table
LIKE this operator is used to compare a value to similar values using wildcard
operator