Chapter 3 - Review of Database Concepts and SQL - PPT
Chapter 3 - Review of Database Concepts and SQL - PPT
It is an electronic or computerized
record keeping system which maintains
various pieces of information in an
integrated and summarized form
instead of keeping them in separate
independent files.
Use of Database in Real-life Applications
Banking
Crop Loan
Inventory Management
Organization Resource Management
Online Shopping
Hospital Management
Social Media
Rail Transport
A DBMS is a general purpose software
system that facilitates the process
of defining, constructing and
manipulating databases for various
applications
Advantages of a DBMS
1. Control of Data Redundancy
2. Data Consistency
3. Sharing of Data
4. Reduced programming effort
5. Improved Data Integrity
6. Database enforces Standards
7. Improved Backup and Recovery
System
Relational Data Model
Different types of DBMS are
available and the most commonly
used data model is Relational Data
Model.
Other types include – Object
Oriented data model, entity
relationship data model, document
model, hierarchical data model
etc.
Relational Data Model
Candidate Keys
Alternate Key
A candidate key that is not the Primary
key is called an Alternate Key
Example: if the field CITY is defined as CHAR(10) Example: if the field NAME is defined as
and the value entered is ‘Mumbai’, the VARCHAR(20) and the value entered is ‘SHALU’,
remaining 4 characters are filled with spaces, the length is 5 bytes and for the value
making the size 10. ‘GURVINDER’, the length is 9 bytes.
Char(N) – represents fixed length data.
Never use NULL to represent a value of ZERO, because they are not equivalent.
Any arithmetic expression containing a null always evaluates to NULL.
Comments
A comment is a text that is not executed.
It is only for documentation purpose.
It can be used in 3 ways:
/*………………………….
………………………..*/ [Used with multiple lines]
-- followed by space [Used with single
line]
#text of comment [Used with single line]
CONSTRAINTS
Constraints are certain types of
restrictions on the data values that an
attribute can have. They are used to
ensure accuracy and reliability of data.
SELECT
INSERT INTO
UPDATE
DELETE
Databases in MySQL
Before you start creating tables, you
need a database which is the container
of tables.
The actual data is stored in tables. To
create tables, either you can create a
new database or use an existing data
base.
Creating Databases
Syntax:
MYSQL>CREATE DATABASE <DATABASE NAME>;
To create a database called SCHOOL, we type
MYSQL>CREATE DATABASE SCHOOL;
Comma is used to
separate two attributes
Viewing the table structure
We can view the structure of an already created
table using the describe statement.
Syntax:
MYSQL> DESCRIBE <tablename>;
OR
MYSQL> DESC <tablename>;
DESCRIBE command
Example:
MYSQL> DESCRIBE Student;
OR
MYSQL> DESC Student;
To list all the tables in a database
To list the tables that are stored in a
database, we use the command:
SHOW TABLES;
Syntax / Example:
MYSQL> SHOW TABLES;
Example
MYSQL>SELECT * FROM Student;
SELECT – DML command
Syntax: Method #2 [To select specific
columns]
MYSQL>SELECT <columnname>, <columnname>…..]
FROM <tablename>;
SELECT – Example
MYSQL>SELECT Name, DOB
FROM Student;
WHERE The WHERE clause is used to retrieve data/rows that meet some specified
conditions.
IN Checks whether an attribute value matches any value with a given list
When WHERE clause is present, the program goes through the entire table row by
row and examines each row to determine if the given condition is true. If it is true,
the row is displayed in the output.
Selecting specific rows – WHERE clause
Example:
MYSQL> SELECT Name, DOB
Retrieves the name and date of birth of all
students from 12B
FROM student
WHERE class='12B';
MYSQL> SELECT EName, Dept Retrieves the name and department of all
employees whose salary is more than
FROM Employee 5000
WHERE Salary>5000;
Selecting specific rows – using multiple
conditions
MYSQL> SELECT Name, DOB Retrieves the name and date of birth of all
students from 12A and 12B
FROM student
WHERE class='12B' or class='12A'
MYSQL> SELECT EName, Dept
FROM Employee
WHERE Salary>5000 and Age < 40;
The above query will retrieve all the records of those students whose marks
fall in the range 60 to 80 including 60 and 80
Condition based on a range – NOT BETWEEN clause
MYSQL> SELECT *
FROM STUDENT
WHERE MARKS NOT BETWEEN 60 AND 80;
MYSQL> SELECT *
FROM EMPLOYEE
WHERE CITY IN ('DELHI','MUMBAI','CHENNAI');
The above query will retrieve all the records of those employees who are
from either DELHI, MUMBAI or CHENNAI
Condition based on a list – NOT IN operator
Used to retrieve rows that do not match the list
values.
MYSQL> SELECT *
FROM EMPLOYEE
WHERE CITY NOT IN ('DELHI','MUMBAI','CHENNAI');
The above query will display the details of all employees other than
from DELHI and MUMBAI
Condition based on pattern matching– LIKE clause
Many a times we come across situations
where we don’t want to query by matching
exact text or value. Rather, we are
interested to find matching of only a few
characters or values in column values. For
example, to find out names starting with
‘T’ or to find out pin codes starting with
‘60’. This is called pattern matching.
Condition based on pattern matching– LIKE clause
Example:
Example:
Syntax:
MYSQL> UPDATE <tablename>
SET <columnname>=<new value>
[WHERE <condition>];
Example:
MYSQL> UPDATE Student SET DOB='2007-04-14'
WHERE RollNo=1;
Example:
MYSQL> DELETE FROM EMPLOYEE
WHERE DEPT='SALES';
Example:
Syntax:
ALTER TABLE <tablename>
ADD <columnname> <data type>;
Example:
ALTER TABLE STUDENT Adds a new column PhoneNo with
ADD PHONENO CHAR(13); datatype char(13) to the table Student
ALTER TABLE command
Modifying datatype of an attribute
Syntax:
ALTER TABLE <tablename>
MODIFY <columnname> <new data type>;
Example:
ALTER TABLE EMPLOYEE Changes the existing datatype /
MODIFY DESIGNATION VARCHAR(20); increases the width of the column
ALTER TABLE command
Removing an attribute
Syntax:
ALTER TABLE <tablename>
DROP <columnname>;
Example:
ALTER TABLE EMPLOYEE Removes the column Commission from the
DROP COMMISSION; table Employee
MORE DDL commands
DROP TABLE command / DROP DATABASE command
Sometimes a table in a database or the
database itself needs to be removed. We
can use DROP command to remove a database
or a table permanently from the system.
However, one should be very cautious while using this statement as it cannot
be undone.
DROP TABLE command
Syntax:
MYSQL> DROP TABLE <tablename>;
Example:
MYSQL> DROP TABLE EMPLOYEE;
Example:
MYSQL> DROP DATABASE SCHOOL;