Chapter 1: Creating Relational Database (8 Marks) : Data
Chapter 1: Creating Relational Database (8 Marks) : Data
Data:
- Data is single piece of information.
- In other words, the input on which different operations are performed is called
as data.
Database:
- Database is collection of data related to specific organization.
- For example: i) College contain information about students, staff.
ii) Railway reservation system contains information about
passengers, railway time-table, reservation details, waiting list
etc.
Database Management System (DBMS):
- DBMS is collection of interrelated data and a set of programs to access those
data.
- Using DBMS, user can:
o Specify data types, structures and constraints for data.
o Store the data.
o Perform operations on data such as retrieving data, updating database.
RDBMS Terminology:
1. Relation:
- A Relation is a 2-dimensional table i.e. the information is arranged in rows and
columns.
- It is called relation because the data values in the table are not homogeneous
i.e. not same type.
2. Attribute:
- An attribute is a named column of a relation.
- For e.g. Student table has S#, SNAME, STATUS, CITY as attributes.
3. Domain:
- A domain is the set of permitted values for one or more attributes.
- It defines the potential values that an attribute may hold.
3. Date:
- To represent date, DATE datatype is used.
- The standard format used to store the date is as follows:
DD-Mon-YY.for e.g. 01-Jul-17
4. Number(P,S):
- To store the numbers, number datatype can be used.
- P is the precision which determines the maximum length of data and S (Scale)
specifies the maximum length of decimal places on the right side or scale.
- The maximum 38 precisions can be stored.
- If the scale is not considered, then by default it is zero.
- The syntax for Number data type is:
Column_nameNumber (P,S);
- For E.g. Marks number(5,2) : means it has 2 digits after decimal.
5. Long:
- This datatype is used to store the variable length character string containing
data upto 2GB.
- Long datatype can be used to store array of binary data in ASCII format.
- One limitation of long datatype is that they cannot use in sub queries,
functions, and expressions.
DDL Commands:
1. Create:
- The CREATE TABLE command is used to create a table in a database.
- Syntax:
- The rules while naming the table and the attributes are as follows:
b) The name should begin with alphabet and not with numbers.
d) Some reserved words of SQL like insert, update, create etc are not allowed for
naming the table.
2. Alter:
- The ALTER TABLE statement is used to add, delete, modify or change the
name of columns in an existing table.
i) To add a column in a table, use the following syntax:
ALTER TABLE table_name ADD column_name datatype(size);
For e.g. ALTER TABLE STUD ADD GRADE VARCHAR2(10);
4. Truncate:
- This command is used to delete the whole rows of the table, the structure of
table remains as it is.
- Truncate table is much faster than delete rows command.
- When rows or records are deleted, command does not return the number of
rows deleted.
- Syntax-
Truncate table tablename;
For e.g. truncate table stud;
Output- Table truncated.
5. Desc:
- As the name suggests, DESCRIBE is used to describe something.
- Since in database we have tables,DESCRIBE or DESCcommand is used to
describe the structure of a table.
- Syntax:
Desctablename;
OR
Describe tablename;
For e.g. desc stud;
- Output-
2. UNIQUE:
- This constraint specifies that values in the column must be unique.
- That is, the values in any row of a column must not be repeated.
- We can have more than one UNIQUE columns in a table.
- For example, following query creates a table Student where the field ID is
specified as UNIQUE. i.e, no two students can have the same ID.
CREATE TABLE Student
(
ID number(6) NOT NULL UNIQUE,
NAME varchar2(10),
ADDRESS varchar2(20)
);
4. FOREIGN KEY:
- A FOREIGN KEY is a key used to link two tables together.
- A FOREIGN KEY is a field (or collection of fields) in one table that refers to
the PRIMARY KEY in another table.
- The table containing the foreign key is called the child table, and the table
containing the candidate key is called the referenced or parent table.
- For example:
- Customer Table
Create Table Customer(
ID Number(2)Primary Key,
Name Varchar2(20),
Age Number(2));
- Order Table:
Create Table Order (
ID Number(2)Not Null,
Order_Date Date,
Order_Id Number(2) References Customer(ID));
5. CHECK:
- Using the CHECK constraint we can specify a condition for a field, which
should be satisfied at the time of entering values for this field.
- For example, following query creates Student table and specifies the
condition for the field AGE as (AGE >= 18 ).
- That is, the user will not be allowed to enter any record in the table with AGE
< 18.
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int NOT NULL CHECK (AGE >= 18)
);
Select
Project
Union
Set difference
Cartesian product
Rename
Set Intersection
- Select and project operations: Unary operations.
- Union, set difference, Cartesian product, Rename and Set Intersection : Binary
operations.
Notation − σp(r)
Where
σ stands for selection predicate.
r stands for relation.
p is prepositional logic formula which may use connectors like and (^) , or
(V ), and not (¬). These terms may use relational operators like: =, !=, >=, <
>, <=
For example −
σsubject = "database"(Books)
Output − Selects tuples from books where subject is 'database'.
- Example:
SQL Query: Select * from Book where price>2000;
- Example :
SQL Query: Select * from Book where price=1000 and Author=‘korth’;
Relational algebra query: σ Price=1000 ^ Author=‘korth’ (Book)
Example:
SQL Query: Select Author from Book union select Author from Article;
Output − Projects the names of the authors who have either written a book or an
article or both.
4) Set Difference (−):
- It is used to find out the difference between two relations.
- The result of set difference operation is tuples, which are present in one relation but
are not in the second relation.
Notation: r − s
Example: Finds all the tuples that are present in r but not in s.
SQL Query: select author from book minus select author from article;
Relational Algebra Query: ∏ author (Book) − ∏ author (Article)
Output − Provides the name of authors who have written books but not articles.
Notation: ρ x (E)
Example: ρ s1 (student)
In the above example, student relation is renamed to s1.
7) Set Intersection (∩):
- It is a binary operator which operates on two relations.
- It is denoted by ‘∩’.
- It is used to find out common tuples present in both relations who are intersected.
Notation − R ∩S
Where R and S are relations
Example:
SQL Query: Select Author from Book intersect select Author from Article;
Output − Displays the names of the authors which are common from book and
article.