Unit 3 RDBMS
Unit 3 RDBMS
1|Contents
UNIT–3
RDBMS
SESSION:1 APPRECIATE THE CONCEPT OF DATABASE MANAGEMENT SYSTEM
Database–A database is an organized collection of data. You can visualize it as a
container of information. The data is typically organized to model relevant aspects
of reality (for example, the availability of rooms in hotels), in a way that supports
processes requiring this information (for example, finding a hotel with facilities
such as Laundry, GYM etc…).
2|Contents
•Decision making with data is improved.
•Improved data access.
•Better data integration.
Features of DBMS • Query Ability.
•Backup & Replication
•Security
•Computation
•Automated optimization
Database Servers–Database servers are dedicated computers that hold the actual
databases and run only the DBMS and related software. Databases on the database
servers are accessed through command line or graphic user interface tools referred
to as Front ends; database servers are referred to as Back-ends. Such type of data
access is referred to as Client-server model.
3|Contents
4.Value:- Value is the actual text or numerical amount or date that you put in while
adding information to your database.
For example,
Database:-Employee
Emp_Co Emp_Na Emp_A Emp_Designat Emp_Cont Emp_Sal
de me dd ion act ary
E001 ABC Meerut Manager 987654321 Rs.50,000
5.KeyField:- Key Field is a value in a Field that uniquely identifies the record.
Eg.E001 which is unique to every employee.
Important Question:- How data is organized in a RDBMS?
Ans:- In RDBMS, data is organized in the form of interlinked tables.
KEYS:- A DBMS key is an attribute or set of an attribute which helps you to identify a
row(tuple) in a relation(table).
PRIMARY KEY:- A primary key is a unique value that identifies a row in a table.
These keys are also indexed in the database, making it faster for the database to
search a record. The Primary Key can't be a duplicate value.
A table can not have more than one primary key.
PROPERTIES OF PRIMARY KEY:-
•There can only be one primary key for a table.
•The primary key consists of one or more columns.
•The primary key enforces the entity integrity of the table.
•All columns defined must be defined as NOT NULL.
•The primary key uniquely identifies a row.
COMPOSITE PRIMARY KEY:- Composite key, or composite primary key, refers to
cases where more than one column is used to specify the primary key of a table.
4|Contents
It may be a candidate key or primary key.
ALTERNATE KEY:- It 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.
CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table.
Candidate Key is a super key with no repeated attributes. The Primary key should
be selected from the candidate keys. Every table must have at least a single
candidate key. A table can have multiple candidate keys but only a single primary
key.
PROPERTIES OF CANDIDATE KEY:• It must contain unique values
•Candidate key may have multiple attributes
•Must not contain null values
•It should contain minimum fields to ensure uniqueness
•Uniquely identify each record in a table
FOREIGN KEY:- The foreign key identifies a column or set of columns in one
(referencing) table that refers to a column or set of columns in another (referenced)
table. Or FOREIGN KEY is a column that creates a relationship between two tables.
The purpose of Foreign keys is to maintain data integrity and allow navigation
5|Contents
between two different instances of an entity. It acts as across-reference between
two tables as it references the primary key of another table.
Memorize it: one, parent, PK(Primary Key); many, child, FK(Foreign Key)
TABLE:- A table is a set of data elements that is organized using a model of vertical
columns and horizontal rows. Each row is identified by a unique key index or the
key field.
6|Contents
DATA TYPES:- Data types are used to identify the type of data we are going to store
in the database.
Categories of data types:- Data types in Open Office Base are broadly
classified in to five categories:
1.Numeric Types
2.Alphanumeric Types
3.Binary Types
4.Date Time
5.Other variable Types
NUMERIC TYPES:- They are used for describing numeric values like mobile number,
age, etc.
The different types of numeric data types available are-
1.Boolean(Yes/No) 6.Numeric
2.TinyInt(Tiny Integer) 7.Decimal
3.SmallInt(Small Integer) 8.Real
4.Integer 9.Float
5.BigInt(Big Integer) 10.Double
ALPHANUMERICTYPES:-
The list of different data types available in alphanumeric types are:
1.Long VarChar(Memo)(Long Text)
2.Char (Text-fix)(Small Text)
3.VarChar (Text) (Text of specified Length)
4.VarChar_IgnoreCase(Text)(Comparisons are not case sensitive)
7|Contents
BINARY TYPES:-
Binary types are used for storing data in binary formats. It can be used for storing
photos, music files or (in general file of any format)etc.
The list of different data types available in Binary types are:
1.LongVarBinary (Image)
2.Binary (Binary(fix))
3.VarBinary (Binary)
DATETIME:-
Datetime data types are used for describing date and time values for the field used
in the table of a database. It can be used for storing information such as date of
birth, date of admission etc.
The list of different data types available in DateTime type are:
1.Date(Stores month, day and year information)
2.Time(Store hour, minute and second information)
3.Timestamp(Stores date and time information)
OTHERDATATYPES:-
Other/Object-Store serialized Java objects.
LAUNCHING OPEN OFFICE BASE
Click Start>Programs>OpenOffice.org3.4.1>OpenOffice.orgBase.
A dialog box will appear. To create new Database, select option Create A New
Database.
To open an existing database, select the option Open an existing database file.
Then click Next, then Finish.
8|Contents
Save As dialog box will appear. Specify a name for the database in the File name:
field and click Save. Now that you have created a database, you can work with the
database as outlined in the next few sessions.
To Create Table in Design View:-
Click on Create Table in Design View… option available under Tasks and a Table
Design window appears as shown below.
Specify the field name and data type of the field to be created by selecting the
appropriate type available under Field type drop down list.
After specify the field name and data type for the field variable, save the table by
clicking on FILE>SAVE. Save as dialog box will appear. Specify the table name. The
default name is Table1. Click OK.
A dialog box appears, similar to the one displayed below.
If you click Yes, the application will set the primary key for the first field created
automatically. If you click No, you should see a window similar to the one displayed
below.
9|Contents
Figure11
Notice the table by the nameS Details created and visible under Tables section.
Start typing the records in the table and select File>Save current record to save data
in the table.
Create table using SQL DDL Command Example:
To create a student table, SQL command used is as follows:
Create table Student
(Stud_Id int Primary Key, Stud_Name varchar(20),Stud_Class varchar(3),
Stud_Address varchar(30));
10|Contents
SESSION3
PERFORM OPERATIONS ON TABLE
In a database we can define the structure of the data and manipulate the data
using some commands. There are two types of languages:-
1.DDL (Data Definition Language)
2.DML(Data Manipulation Language)
11|Contents
Common DML Statements are:-
1.SELECT:-Used for retrieval of information from the database.
2.INSERT:- Used for insertion of new information in to the database.
3.DELETE:- Used for deletion of information in the database.
4.UPDATE:- Used for modification of information in the database.
Types of DML:-
1.Procedural:- The user specifies what data is needed and how to get it.
2.NonProcedural:- The user only specifies what data is needed.
Note:-A popular data manipulation language is SQL(Structured Query Language.)
SQL commands are also used to manage and manipulate data in a database. A
query language is a part of DML involving information retrieval only. The terms
DML and query language are often used synonymously.
INSERT statement: It is used to add one or more records to a database.
Syntax: INSERT INTO ’’table_name’’ (column1,column2,column3….) VALUES
(value1,value2,value3….)
To add a record in the database created earlier, type the following and click
Execute.
Example: Insert into SDetails
(“ID”,“Name”,“Rollno”,“DOB”,“Class”,“Phone”,“Email”,“Color”,“Location”) values
(‘8’,‘RanjithSingh’,’67’,‘12-03-99’,’X’,‘435363’,‘[email protected]’,‘White’, ‘Bihar’);
After inserting the data in to the table, use select query to view the updated table.
DELETE statement
Delete Statement is used to remove one or more records in a database.
Syntax: DELETE FROM ‘’table_name’’ WHERE ‘’condition’’;
Example: Delete from SDetails where ‘ID’=‘8’;
12|Contents
Execute select query to view the updated table
SORTING DATA
Sorting means to arrange the data in either ascending order or descending order.
Select the column(s) then click on sort buttons. The data will be displayed
accordingly.
To display sorted data using sql command write as follows:
Select * from “tablename” order by “fieldname”;
Select * from “tablename” order by “fieldname” desc;
Referential Integrity
Referential integrity is used to maintain accuracy and consistency of data in a
relationship.
In Base, data can be linked between two or more tables with the help of primary
key and foreign key constraints.
Referential integrity helps to avoid:
• Adding records to a related table if there is no associated record available in the
primary key table.
• Changing values in a primary if any dependent records are present in associated
table(s).
• Deleting records from a primary key table if there are any matching related
records available in associated table(s).
Creating and Editing Relationships between Tables
A relationship refers to an association or connection between two or more tables.
Relationships between tables helps to:
• Save time as there is no need to enter the same data in separate tables.
• Reduce data-entry errors.
13|Contents
• Summarize data from related tables.
You can create a relationship between any two tables by selecting
Relationships…option from the Tools menu.
Add the tables in amongst which you want to create the relationship. Select the
tables and click on Add button.
14|Contents
One to Many Relationship
In this relationship, one of the table must have primary key column.
It signifies that one column of primary key table is associated with all the
columns of associated table.
15|Contents
The relationships applied on the tables can be removed also with the help of Delete
option. Right Click on the relationship thread and select Delete option.
16|Contents
Example- Select Name,Rollno from SDetails Where Class= ’10’ Order by ‘’Rollno’’ ;
(b)To retrieve all the columns in a table:
Syntax: SELECT * FROM table_name
Example- Select * from SDetails;
In order to execute queries click on the Queries option available on the left side
under database section, click Create Query in SQL View as shown below.
Awindowappearssimilartotheonedisplayedbelow.
You can type the query in the above window and execute it by using the F5 function
key or by clicking the icon in the window.
Grouping of Data
To display the records containing the same type of values “WHERE” clause can be
used with the Select SQL Command.
To get details about the list of students whose favorite color is blue, you can use:
select * from SDetails where Color=’Blue’;
After executing the select query the output will be shown similar to the one
displayed below.
17|Contents
To view records in ascending order of RollNo, from the table the select statement
will be: select * from SDetails order by Rollno ASC;
You can add, modify or delete records using the Insert, Update and Delete
commands.
To type and execute SQL commands, click on Tools>SQL. A window similar to the
one below will be displayed.
You can type the SQL Commands in the Command to execute space and click on
Execute.
UPDATE statement
Update statement is used for modifying records in a database.
SYNTAX: UPDATE ‘’table_name’’
SET column_name=value[,column_name=value...]
[WHERE‘’condition’’];
Example- Update SDetails set Location=‘Bhubaneswar’ where Rollno=14;
Type the following and click Execute. After execution you should see a updated
table.
Click Use Wizard to Create Form…option under Tasks group. The Form Wizard
dialog box appears as shown below.
You can select selective fields to be sent on to the form by selecting the field name
and clicking>button
To use all the fields in the table in a form, click the >> button.
Notice the fields displayed under Fields in the forms section(Figure below).
19|Contents
Click Next>. You see the Setup a subform step dialog box of the wizard as shown
below.
You can select the option AddSubform if you need to insert the contents in the
table in a separate form. Click Next>.
Select a style. Once you have selected a style, click Next>
A dialog box appears where in you can select the data entry model Click Next>. You
should see a dialog box where in you can specify the styles to be used in the form.
Click Next>. You see a dialog box where you can specify the name of the form. Click
Finish.
A form window appears. Notice that there cords in the table are displayed
automatically within the form that you just created.
20|Contents
You can add new records to the table using the form by clicking the symbol
located at the bottom as shown below.
21|Contents
You have learnt to create records using design view in the earlier sessions. You can
add records using the form as displayed below. Type the following data in the text
box provided in the box as shown below:
Now enter three more records using the form and view them using the above
mentioned procedure.
22|Contents
Now we can generate the report for the table created earlier.
Click on Use Wizard to Create Report…option available under Tasks.
Once you select the Use Wizard to Create Report…option. You should see a
window similar to one displayed below.
You have to select all the table fields by selecting the>>button, once you click the
button>>you should see a dialog box similar to the one displayed below.
Click Next>.
Once you click Next>. You should see a dialog box similar to the one displayed
below.
23|Contents
You can redefine the label of the fields in the reports or else you can set the
default name.
Click Next>.
Once you click Next>,you should see a dialog box similar to the one displayed
below.
24|Contents
You can define a name for the report or you can use the name of the table itself for
the report also.
Click Finish.
Once you click Finish you should see a window similar to the one displayed below
with the report.
Now create a report containing only Name, RollNo and Phonedetails. Use different
layouts for the report design.
25|Contents