Database Management Systems: Module - 4
Database Management Systems: Module - 4
Database Management Systems: Module - 4
Database Concepts,
Web Designing
23
In the previous lesson you have learnt that data processing concepts. Now you
know that computers are used to process data. You can feed data into the
computers and give the instructions (in the form of programs) to process the
data. Computers can process the data by following the specified instructions.
Computers are able to process data at extremely high speeds. There are lots
and lots of data which are processed by the computers. For easy and efficient
processing of data it is necessary that the data is stored in an organized way.
There are specialized software, called Database Management Systems, which
facilitate organized storage of data. In this lesson you will learn about Database
Management Systems.
OBJECTIVES
After reading this lesson, you will be able to:
z define database;
z design simple databases;
z create databases and tables within them using MySQL;
z write and execute SQL commands to perform various database operations.
23.1 TERMINOLOGY
To understand the concepts of Database Management System, you must be
familiar with a few terms which are given below:
Database: A database is an organized collection of data. From daily life we
can take many examples of simple databases. A few are: An index of a book,
Notes
Schema – It refers to the organization of data or skeleton structure that
represents the logical flow of the entire database.
Domain: A domain (of a column) is a pool of values from which that column
draws its actual values. For example, domain of the column RollNumber may
be integers from 1 to 50.
Primary Key: The group of one or more columns used to uniquely identify
each row of a relation is called its Primary Key. For example, in a students table
AdmissionNumber can be the primary key. A table cannot have more than one
primary key.
Foreign Key: A group of one or more columns in a table which is used to set
relationship of this table with some other table.
Cartesian Product (of two tables): It is a new table which contains the columns
of both the given tables. The rows of the new table are obtained by pairing each Notes
row of the first table with each row of the second table.
Notes
Student Course
Result
Marks
z No attribute is specified.
Logical data Model: Logical data model can be thought of as a closer view
of the database. In the conceptual model we look at the entities and their
relationships only. In logical data model we describe the attributes of these
entities in as much details as possible.
In our example of school, the entities are: Students, Courses, Marks, and
Results.
Notes
Foreign Keys:
Table Foreign Key(s)
Course None
Student Course Code, Subject Codes
Marks Admission Number, Course Code, Subject Code
Physical data Model: Physical data model represents the actual implementation
of the Logical Data Model. A physical data model shows all the table structures
including column names, column data types, column constraints, primary key,
foreign key and relationship between tables.
In our example of School, the physical data model can be represented as follows:
23.3 MYSQL
Once we have designed the physical data model, it is time to actually create
the database in the computer and store data in it. To create the database we
have to have some RDBMS (Relational DataBase Management System)
software. There are many RDBMS software available like Oracle, MySQL, MS
SQL Server, PostgreSQL etc.
MySQL is an open source free software. It can be downloaded free of cost from
http://dev.mysql.com/downloads/mysql and requires no cost for its usage.
After downloading MySQL software, you can easily install it. After installing
MySQL you have to locate the file.
Most probably you will find this file in the folder (Assuming that MySQL is
installed in C:\ drive)
C:\Program Files\MySQL\MySQL Server 5.1\bin
If you do not find the file here then you can search for it. Once you find it,
you should create its shortcut on the desktop so that you can easily run it.
Now you can use MySQL to create and manage databases. To exit from MySQL
you have to give the command QUIT or EXIT at the MySQL prompt.
sp decimal(6,2) NO NULL
Similarly, you can use the command to create the BILLS table and then display
its structure.
After creating a table you have to enter data in the table. The process of
entering data in a table is also called populating the table. You enter data in
a table row-wise. To enter a row in a table you use the command INSERT INTO
as follows:
INSERT INTO <TableName>
VALUES (<Value1>,<Value2>,… ,<ValueN>);
For example, to enter a record (row) in the ITEMS table, you use the command:
insert into items values (“A001”, “Curd - 1Kg Pack”, 50,60);
Here you should notice that the character (CHAR and VARCHAR) values are
placed in double quotation marks. They can also be placed in single quotation
marks. Similarly date (DATE) values must also be placed within quotation
marks. Numeric values (INT and DECIMAL) must not be placed in quotation
marks.
Let us insert some more rows in the ITEMS table by the commands:
insert into items values (‘A002’, “Curd - 0.5Kg Pack”, 27,35);
insert into items values (‘B010’, “Wheat Flour”, 18,20);
insert into items values (‘B011’, “Basmati Rice”, 35,42);
SELECT is the most widely used command in SQL and has many variations.
Now we will discuss this in detail.
SELECT
SELECT command is used to retrieve data from table(s). In its simplest form
SELECT command is used to retrieve complete data from a table as follows:
SELECT * FROM <TableName>;
For example, to view all the data from ITEMS table we use the command:
select * from items;
If you want to view only specific columns of a table, you can specify the column
names instead of asterisk (*) in the SELECT command as follows:
SELECT <ColumnName1>,<ColumnName2>. . <ColumnName n>
FROM <table name>;
For example, to view only the item description and selling price of items you
use the following command:
SELECT Item_Desc, sp FROM ITEMS;
It will display the following:
Item_Desc sp
Curd - 1Kg Pack 60.00
Curd - 0.5Kg Pack 35.00
Wheat Flour 20.00
Basmati Rice 42.00
item_desc sp-cp
Curd - 1Kg Pack 10.00
Curd - 0.5Kg Pack 8.00
Wheat Flour 2.00
Basmati Rice 7.00
Data can be fetched from tables by using SELECT command. Conditioned data
can be fetched by using WHERE clause. For example, if we want to fetch all
the items that cost more than 30, we can use:-
Select * from items where CP > 30;
This will display the following result:
item_code item_dese CP SP
A001 curd 1Kg Pack 50.00 60.00
We can specify more than one condition in where clause to get more specified
data. e.g., if we need to fetch the items whose CP is more than 30 but SP is
less than 50, we can use:-
Select * from item where CP > 30 and SP <50;
As shown, we have to use ‘and’ or ‘or’ a combination of both to get any type
of data that we want. Another example:-
Select * from item where (CP > 30 or SP > 30) and SP < 50;
It is important to use the braces for correct order of execution. It is interesting
to see that we can fetch data from more than one table simultaneously by using
these conditions in where clause and mentioning table names in from clause.
For example:-
Select field1, field2, ....., fieldN from table1, table2 where condition1 AND/OR
condition2 AND/OR condition....
Please notice, if the number of conditins are more then more time will be taken
by the query for processing and fetching the results.
You can update your data values of the table by using Update command as
follows:
UPDATE table_name SET field1=new-value1, field2=new-value2;
Or
UPDATE table_name SET field1=new-value1, field2=new-value2
Notes
[WHERE Clause];
Update command is use to update either all (without where clause) or selected
data values (By using the where clause) of the table. One can update one or
more fields of a same table under a single command by specifying the WHERE
clause. It is a data manipulation command.
For example, to update the SP of the item B010 i.e. wheat flour from 20 to
22, you use the update command as follows:
This query will update the record whose id is B010. Its SP will be set to new
value 22.
You can delete the data of a table by using Delete command as follows:
Or
Delete command is used to remove rows from the table. It can be used to delete
either all the rows from the table or selected set of rows from the tables using
where clause.
For example, to delete all the rows from the ITEMS table you can use the Delete
command as follows:
The above command will delete all the rows from the table ‘ITEMS’ table.
The above command will delete the rows from the table ‘ITEMS’ that have CP
greater than 40.
Syntax:
TERMINAL EXERCISE
1. Create a following table:
a) Supplier
Field Type Null Key
b) Parts
Field Type Null Key
Notes
d) SPJ
Field Type Null Key
b) Parts table
PNO PName Color Weight City
P1 Nut Red 12 London
P2 Bolt Green 17 Paris
P3 Screw Blue 17 Rome
P4 Screw Red 14 London
P5 Cam Blue 12 Paris
J7 Tape London
d) SPJ table
SNO PNO PNO QTY
S1 P1 J1 200
S1 P1 J4 700
S2 P3 J1 400
S2 P3 J2 200
S2 P3 J6 400
S2 P3 J7 800
S3 P3 J1 200
S3 P4 J2 500
S4 P2 J2 200
S4 P6 J3 300
S4 P6 J7 300
S5 P1 J4 100
S5 P2 J4 100
S5 P3 J4 200
S5 P4 J4 800
23.1
1. (a) Database
(b) Redundancy
(c) Relational
(d) Foreign
2 (a) False (b) True
(c) False (d) False
(e) True
23.2
1. (a) RDBMS
(b) Structured Query Language
(c) SQL, relational
(d) DDL (Data Definition Language)
(e) ; (or) semicolon
2. (a) True (b) False
(c) False (d) False
(e) True