0% found this document useful (0 votes)
116 views

Chapter 1: Creating Relational Database (8 Marks) : Data

The document provides information about relational database concepts including: - Relational databases store data in related tables with rows and columns. Structured Query Language (SQL) is used to manipulate the data. - A database management system (DBMS) allows users to define data structures and constraints, store and retrieve data, and perform other operations. - Relational database management systems (RDBMS) are a type of DBMS that uses the relational model. RDBMS software includes products like Oracle, SQL Server, and MySQL. - The document defines key relational database concepts and terminology like relations, attributes, domains, tuples, and cardinality. It also describes SQL data types and the purposes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Chapter 1: Creating Relational Database (8 Marks) : Data

The document provides information about relational database concepts including: - Relational databases store data in related tables with rows and columns. Structured Query Language (SQL) is used to manipulate the data. - A database management system (DBMS) allows users to define data structures and constraints, store and retrieve data, and perform other operations. - Relational database management systems (RDBMS) are a type of DBMS that uses the relational model. RDBMS software includes products like Oracle, SQL Server, and MySQL. - The document defines key relational database concepts and terminology like relations, attributes, domains, tuples, and cardinality. It also describes SQL data types and the purposes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter 1: Creating Relational Database (8 Marks)

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 (Relational Database Management System):


- RDBMS is a type of DBMS that stores the data in the form of related tables.
- RDBMS is a database based on the relational model developed by E.F. Codd.

Relational Database Design


- Relational database design (RDD) models information and data into a set of
tables with rows and columns.
- Each row of a relation/table represents a record, and each column represents
an attribute of data.
- The Structured Query Language (SQL) is used to manipulate relational
databases.

1 Prepared by:Mrs. M. P. Nawarkar [IF dept]


Different types of RDBMS Software:
- Oracle
- Informix
- Sybase
- PostgreSQL
- IBM DB2.
- Microsoft Access.
- Microsoft SQL Server.
- MySQL.
- QuickBase.

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.

2 Prepared by:Mrs. M. P. Nawarkar [IF dept]


- For e.g. If the STATUS of the student in class is between 10 and 30, then we
can define a set of values for the STATUS attribute of student asthe set of
integers between 10 and 30.
4. Tuple:
- Tuple can also be called as record.
- Record can be defined as any row of a table.
- For e.g. data of one student in one row in student table is tuple.
5. Cardinality:
- It is defined as the number of tuples or rows in a relation.
- The cardinality of a relation changes with time as more tuples get added or
deleted.
6. Degree:
- The number of attributes in a relation is called the degree of the relation.
- A relation having degree one is unary, a relation of degree two is called
binary.
- Above student relation has a degree of 4.
Introduction to SQL:
- Structure Query Language(SQL) is a database query language used for
storing and managing data in Relational DBMS.
- SQL was the first commercial language introduced for E.F
Codd's Relational model of database.
- Today almost all RDBMS(MySql, Oracle, Infomix, Sybase, MS Access)
use SQL as the standard database query language.
- SQL is used to perform all types of data operations in RDBMS.
Data Types in SQL:
Data type is nothing but the storage format of data. Different data types available in
SQL are as follows.
1. Char(size) –
- It is used for storing character strings values of fixed length.
- The size is used to specify the fix length of that particular field or attribute.
- The maximum no. of size is 255 characters.
- The syntax for char is:
Column_namechar(Size);
2. Varchar(Size) and Varchar2(Size) –
- Varchar stands for variable length character string.
- Both Varchar and Varchar2 are data types to store character strings for
particular column (field) in databases.
- The syntax for varchar and varchar2 data type is:
Column_name varchar(Size);
or
Column_name varchar2(Size);

3 Prepared by:Mrs. M. P. Nawarkar [IF dept]


Varchar Varchar2
Varchar can identify NULL and Varchar2 cannot identify both separately.
empty string separately. Both are considered as same.
Varchar can store minimum 1 and Varchar2 can store minimum and
maximum 2000 bytes of character maximum 4000 bytes of character data.
data.
It allocates fixed size of data It allocates variable size of data based
irrespective of the input. on input.
Ex: We defined varchar (15) and Ex: We defined varchar2 (15) and
entered only 10 characters. But it entered only 10 characters. Then
allocates space for entire 15 varchar2 will allocate space for 10
characters. characters only but not for 15.
For varchar data, extra spaces are For varchar2 extra spaces will be
padded to the right side. truncated.
Varchar is ANSI Sql standard. Varchar2 is Oracle standard.

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.

Purpose of DDL (Data Definition Language): DDL or Data Definition Language


actually consists of the SQL commands that can be used to define the database
schema. It simply deals with descriptions of the database schema and it is used to
create and modify the structure of database objects in database. Examples of DDL
commands are CREATE, DROP, ALTER, TRUNCATE and RENAME.
Purpose of DML (Data Manipulation Language):The SQL commands that deals
with the manipulation of data present in database belong to DML or Data
Manipulation Language and this includes most of the SQL statements.Examples of
DML are SELECT, INSERT, UPDATE and DELETE.

Purpose of DCL (Data Control Language): DCL includes commands such as

4 Prepared by:Mrs. M. P. Nawarkar [IF dept]


GRANT and REVOKE which mainly deals with the rights, permissions and other
controls of the database system.Examples of DCL commands are GRANT and
REVOKE.
Purpose of TCL(Transaction Control Language): TCL commands deals with
the transaction within the database.Examples of TCL commands are COMMIT,
ROLLBACK, SAVEPOINT and SET TRANSACTION.

DDL Commands:
1. Create:
- The CREATE TABLE command is used to create a table in a database.

- Syntax:

CREATE TABLE table_name (


columnname1 datatype(size),
columnname2 datatype(size),
columnname3 datatype(size),
...);
- At the end of SQL statement Semicolon (;) is given to terminate it.

- The rules while naming the table and the attributes are as follows:

a) Alphabets (A-Z and a-z) as well as numbers (0-9) are allowed.

b) The name should begin with alphabet and not with numbers.

c) The special symbol generally allowed is ‘_’ i.e. underscore.

d) Some reserved words of SQL like insert, update, create etc are not allowed for
naming the table.

For e.g. CREATE TABLE STUD


(RNO NUMBER(2), NAME VARCHAR2(50), MARKS NUMBER(4,2), BDATE
DATE);
Output – Table created.

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);

ii) To drop a column in a table, use the following syntax:

5 Prepared by:Mrs. M. P. Nawarkar [IF dept]


- ALTER TABLE table_name DROP COLUMN column_name;
 For e.g. ALTER TABLE STUD DROP COLUMN GRADE;
iii) To modify a column in a table, use the following syntax:

- ALTER TABLE table_name MODIFY column_name datatype(size);

- For e.g. ALTER TABLE STUD MODIFY marks number(5,2);

iv) To rename/change existing name of a column, use the following syntax:


- ALTER TABLE table_name RENAME COLUMN oldcolumnname TO
newcolumnname;
- For e.g. ALTER TABLE stud RENAME COLUMN rno TO rollno;
3. Drop:
-This command is used to delete the table.
- It deletes the table along with its data.
- Once the table is dropped, the records or data within it cannot be recovered.
- So drop table should be executed with caution.
- Syntax-
Drop table tablename;
- For e.g. drop table stud;
- Output- Table dropped.

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-

6 Prepared by:Mrs. M. P. Nawarkar [IF dept]


6. Rename:
- Rename command gives new name to the table.
- User can use rename command to specify the new name to the table.
- Syntax-
Rename oldtablename to newtablename;
- For e.g. rename stud to stud_info;
Output- Table renamed.

Use of Data constraints:


- Constraints are the rules that we can apply on the type of data in a table.
- That is, we can specify the limit on the type of data that can be stored in a
particular column in a table using constraints.
The available constraints in SQL are:
1. NOT NULL:
- This constraint tells that we cannot store a null value in a column.
- That is, if a column is specified as NOT NULL then we will not be able to store
null in this particular column.
- For example, the following query creates a table Student with the fields ID and
NAME as NOT NULL. That is, we are bound to specify values for these two
fields every time we wish to insert a new row.
CREATE TABLE Student
(
ID number(6) NOT NULL,
NAME varchar2(10) NOT NULL,
ADDRESS varchar2(20)
);

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)
);

7 Prepared by:Mrs. M. P. Nawarkar [IF dept]


3. PRIMARY KEY:
- A primary key is a field which can uniquely identify each row in a table.
- This constraint is used to specify a field in a table as primary key.
- A table can have only one field as primary key.
- For e.g. following query will create a table named Student and specifies the
field ID as primary key.
CREATE TABLE Student
(
ID number(6) PRIMARY KEY,
NAME varchar2(10) NOT NULL,
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));

- ID column in customer table is primary key.


- Order_ID column from order table which references to ID column of customer
table is foreign key.

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)
);

8 Prepared by:Mrs. M. P. Nawarkar [IF dept]


Different Relational Algebra Operator:
- Relational algebra is a procedural query language, which takes
instances of relations as input and gives instances of relations as output.

- It uses operators to perform queries.

- An operator can be either unary or binary.

- Relational algebra is performed recursively on a relation and


intermediate results are also considered relations.

The fundamental operators of relational algebra are as follows −

 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.

1) Select Operation (σ – Greek Symbol Sigma):


- It selects tuples that satisfy the given predicate from a relation.

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;

9 Prepared by:Mrs. M. P. Nawarkar [IF dept]


Relational Algebra Query: σ Price>2000 (Book)

- Example :
SQL Query: Select * from Book where price=1000 and Author=‘korth’;
Relational algebra query: σ Price=1000 ^ Author=‘korth’ (Book)

2) Project Operation (∏ - Greek Symbol Pi):


- It is a unary operation which operates on single relation.
- It projects or displays columns that satisfy a given predicate.

Notation − ∏A1, A2, An (r)


Where A1, A2 , An are attribute names of relation r.
For example −

∏subject, author (Book)


Selects and projects columns named as subject and author from the relation Book.
SQL Query: Select subject, author from Book;

3) Union Operation (∪):


- It is a binary operation.
- Union operation performs union between two given relations.
Notation − R U S
Where R and S are either database relations or relation result set
For a union operation to be valid, the following conditions must hold:
- R, S must have same number of attributes.

Example:
SQL Query: Select Author from Book union select Author from Article;

Relational Algebra Query: ∏ Author (Book) U ∏ Author (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.

10 Prepared by:Mrs. M. P. Nawarkar [IF dept]


5) Cartesian Product (Χ):
- It combines information of two different relations into one.
- It is an operation where one relation will be multiplied by other relation.
- The Cartesian product of two tables combines each row in one table with each row
in the other table.
Notation − r Χ s
Where r and s are relations
Example:
SQL Query: Select bid from Book Cross join Select bid from Article;
Relational Algebra: ∏ Author (Book) X ∏ Author (Article)

6) Rename Operation (ρ – Greek Symbol Rho)


- The rename operation allows us to rename the output relation.

- Rename operation is denoted with small Greek letter (rho) ρ

Notation: ρ x (E)

Where the result of expression E is saved with name of x.

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;

Relational Algebra Query: ∏ Author (Book) ∩ ∏ Author (Article)

Output − Displays the names of the authors which are common from book and
article.

11 Prepared by:Mrs. M. P. Nawarkar [IF dept]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy