Chapter 7 Relational Database and SQL
Chapter 7 Relational Database and SQL
What is Database:
Elimination of data redundancy: It removes duplication of data because data are kept at one
place and all the application refers to the centrally maintained database.
Sharing of data: Same database can be used by various platforms.
Manages large amounts of data: A database stores and manages a large amount of data on a
daily basis. This would not be possible using any other tool such as a spreadsheet.
Data is logically accurate: Through validation rule in database ,data accuracy can be
maintained.
Easy to update data: In a database, it is easy to update data using various Data Manipulation
languages (DML) available. One of these languages is SQL.
Security of data: There are user logins required before accessing a database. It allows only
authorized users to access the database.
What is DBMS:
A DBMS refers to a software that is responsible for storing, maintaining and utilizing database
in an efficient way.
A Database along with DBMS software is called Database System.
Example of DBMS software are Oracle, MS SQL Server, MS Access, Paradox, DB2 and MySQL
etc.
Advantages of DBMS or Need of DBMS or Importance of DBMS:
DBMS Model: Data model is a model or presentation which shows How data is organized ? or stored
in the database. Data models are categorized into four categories:
Keys in a Database:
Key plays an important role in relational database; it is used for identifying unique rows from table &
establishes relationship among tables on need.
Types of keys in DBMS:
Primary Key: A primary is a column or set of columns in a table that uniquely identifies tuples (rows)
in that table.
Candidate Key: A candidate key is one that is capable of becoming the primary key.
Alternate Key: Out of all candidate keys, only one gets selected as primary key, remaining keys are
known as alternate or secondary keys.
Foreign Key: Foreign keys are the columns of a table that points to the primary key of another table.
They act as a cross-reference between tables.
Interactive Language : This language can be used for communicating with the databases and
used to manage database.
Portability : SQL is compatible with other database programs like Dbase IV, FoxPro, MS Access,
DB2, MS SQL Server, Oracle, Sybase, MySQL.
No coding needed : It is very easy to manage the database systems without any need to write
the substantial amount of code by using the standard SQL.
Not case-sensitive language : SQL is not a case-sensitive language, both capital and small
letters are recognized.
Types of SQL:
1)Data Definition Languages (DDL) 2) Data Manipulation Languages (DML)
3) Data control Language (DCL) 4) Transaction Control Language (TCL)
Data Definition Languages Data Manipulation Languages
All the commands used to create, modify or All the commands used to create, modify contents
delete physical structure of an object like table of a table
Example of Command: create, alter, drop. Example of Command: Insert, Delete, Update.
3. Use: After database creation we can open the database using USE command.
Mysql> Use school;
4. Drop database: To physically remove or delete a database along with all its table, DROP command
is used.
Mysql> Drop database school;
9. DROP TABLE Command: To remove or delete any table permanently, DROP TABLE command is
used.
mysql> drop table student;
2. We can insert data by specifies both the column names and the values to be inserted:
mysql> insert into student (rollno, name, gender, marks,dob) values(2,NULL,'m',93,'2000-11-
17');
3. Inserting data into specific columns of table:
mysql> insert into student (rollno, name, gender,dob) values(2,NULL,'m','2000-11-17');
13. Modifying data in table: The UPDATE command in SQL is used to modify data in table using the
WHERE clause and the new data is written in place of old data using the SET keyword.
1. To Update single or multiple columns:
mysql> update students set city="Fatehpur" where rollno=1;
15. Removing all data from a table: The TRUNCATE command in SQL is used to delete all rows from a
table free the space containing table.
truncate table students2;
Logical Operators:
The SQL logical operators are the operators used to combine multiple conditions and filter data on the
basis of the condition specified in an SQL statement. Logical operators are also known as Boolean
operators.
There are three types of operators are:
AND operators: The AND operator displays a record and returns a true if all conditions (usually two
conditions) specified in the WHERE clause are true.
Example: mysql> select * from emp where salary > 20000 and age < 25;
OR operators: The OR operator displays a record and returns a true if either of the conditions (usually
two conditions) specified in the WHERE clause is true.
Example: mysql> select * from emp where salary > 20000 or age > 25;
NOT operators: NOT operators is also termed as a negation operator. This operator takes only one
condition and gives the reverse of it as the result.
Example: mysql> select * from emp where not age > 22;
18. Some Special Operators in MySQL:
Between
This operator defines the range of values that the column values must fall into make the condition
True. The range includes both the upper Values as well as Lower Values.
Example: select * from emp where salary between 15000 and 20000;
Not Between
The NOT BETWEEN operators works opposite to the BETWEEN operators. It retrieves the rows which
do not satisfy the BETWEEN condition.
The range not includes both the upper Values as well as Lower Values.
Example: select * from emp where salary not between 15000 and 20000;
IN
This operator selects values that match any values in the given list. The SQL IN condition is used to
help reduce the need for multiple OR conditions in a SELECT statement.
Example: select * from emp where salary in (15000,20000);
NOT IN
The NOT IN operators works opposite to IN operator. It matches, finds and returns the rows that do
not match the list.
Example: select * from emp where salary not in (15000,20000);
IS NULL
THE IS NULL is used to search for null values in a column.
mysql> select * from emp where salary is null;
IS NOT NULL
THE IS NOT NULL is used to search for not null values in a column.
mysql> select * from emp where salary is not null;
Like
The LIKE operators is used to search for a specified pattern in a column. This operator is used with the
columns of types CHAR and VARCHAR.
SQL provides two Wild Card Characters that are used while comparing the strings with LIKE operators: