DBMS
DBMS
DBMS:
A database management system (DBMS) is a collection of interrelated data and a set
of programs to access those data. The collection of data, usually referred to as the
database, contains information relevant to an enterprise. The DBMS provides a way
to store and retrieve database information that is both convenient and efficient.
Some examples of open source and commercial DBMS include MySQL, Oracle,
PostgreSQL, SQL Server, Microsoft Access, MongoDB.
b) Airlines
c) Universities
d) Telecommunications
e) Sales
f) Finance, etc.
A data model describes the structure of the database, including how data are defined
and represented, relationships among data, and the constraints.
Database schema: Database Schema is the design of a database. It is the skeleton of the
database that represents the structure (table names and their fields/columns), the type
of data each column can hold, constraints on the data to be stored (if any), and the
relationships among the tables.
Database schema is also called the visual or logical architecture as it tells us how the data
are organized in a database.
Data constraint: Restrictions or limitations are put on the type of data that can be
inserted in one or more columns of a table to ensure accuracy and reliability of data in
the database.
Meta – Data or Dictionary: A database catalog or dictionary that has data about data.
Database instance: The state of database at any time, after loading of data. A database
schema can have different instances at different times.
Relational model:
The relational model uses a collection of tables to represent both data and the relationships
among those data.
Each table has multiple columns, and each column has a unique name. The relational model
is an example of a record – based model.
Record- based models are so named because the database is structured in fixed-
format records of several types. Each table contains records of particular type. Each
record type defines a fixed number of fields/attributes.
The columns of the table correspond to the attributes of the record type.
A row in a table represents a relationship among a set of values. The relational data
model is the most widely used data model.
Other types of data models include object-oriented data model, entity-relationship data
model, document model and hierarchical data model.
Domain: It is collection of values from which the value is derived for a column.
Candidate Key –It is an attribute or a set of attributes or keys participating for Primary
Key, to uniquely identify each record in that table.
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.
By using SQL commands, one can search for any data in the database and perform
other functions like creating tables, adding records, modifying data, removing rows,
dropping tables etc.
● High speed
Using the SQL queries, the user can quickly and efficiently retrieve a large amount of
records from a database.
● No coding needed
In the standard SQL, it is very easy to manage the database system. It doesn't require a
substantial amount of code to manage the database system.
● Well defined standards
Long established are used by the SQL databases that are being used by ISO and ANSI.
● Portability
SQL can be used in laptop, PCs, server and even some mobile phones.
● Interactive language
SQL is a domain language used to communicate with the database. It is also used to
receive answers to the complex questions in seconds.
Using the SQL language, the users can make different views of the database structure
SQL commands are instructions. It is used to communicate with the database. It is also
used to perform specific tasks, functions, and queries of data.
SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.
There are five types of SQL commands: DDL, DML, DCL, TCL, and DQL.
1.5.1 DDL or 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 is used to create and modify the structure of database objects in
the database. DDL is
a set of SQL commands used to create, modify, and delete database structures but not data.
TRUNCATE: This is used to remove all records from a table, including all spaces
allocated for the records are removed.
1.6 DATATYPES
Date , Time
1.7 CONSTRAINT
A Constraint is a condition or check applicable on a field or set of fields.
Types of Constraints:
⮚ Unique Constraint :-This ensures that no rows have the same value in
the specified column(s) Syntax:
Unique constraint applied on ecode of EMP table ensures that no rows have the same
ecode value
⮚ Primary key Constraint:- This declares a column as the primary key of the table
This is similar to unique constraint except that one column (or one group of
columns)can be applied in this constraint .
The primary key cannot allow NULL values but Unique key allows NULL values.
The following SQL creates a PRIMARY KEY on the "ID" column when the
"Persons" table is created:
Syntax:
Create table EMP(ecode integer Not null unique, ename char(20),sex char(1),grade
char(2));
⮚ CREATE DATABASE
CREATE DATABASE is the SQL command used for creating a database in MySQL.
Imagine you need to create a database with name “movies”. You can create a
database in MySQL by executing following SQL command
⮚ SHOW DATABASES
You can see list of existing databases by running following SQL command.
⮚ USE
⮚ DROP DATABASE
CREATE TABLE
The CREATE TABLE statement is used to create a new table in a database.
Example: The following example creates a table called "Persons" that contains five
columns:
⮚ SHOW TABLES;
We can get the number of table information of a database using the following
statement:
⮚ DESCRIBE TABLE
Use the DESCRIBE command to show the structure of the table, such as column names,
constraints on column names, etc. The DESC command is a short form of the DESCRIBE
command. Both DESCRIBE and DESC commands are equivalent.
Syntax The following are the syntax to display the table structure:
To create a PRIMARY KEY constraint on the "ID" column when the table is already
created, use the following SQL:
ALTER TABLE table_name ADD PRIMARY KEY (Column_name);
2. If you are adding values for all the columns of the table, you do not need to
specify the column names in the SQL query. However, make sure the order of the
values is in the same order as the columns in the table. Here, the INSERT INTO
syntax would be as follows:
DELETE Syntax:
DELETE FROM table_name WHERE condition;
Note: Be careful when deleting records in a table! Notice the WHERE clause in the
DELETE statement. The WHERE clause specifies which record(s) should be deleted.
If you omit the WHERE clause, all records in the table will be deleted!
The following SQL statement deletes all rows in the "Customers" table, without deleting
the table:
Here, column1, column2, ... are the field names of the table you want to select data
from. If you want to select all the fields available in the table, use the following
syntax:
SELECT * FROM
Operator Description
= Equal
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition:
● The AND operator displays a record if all the conditions separated by AND are
TRUE. ● The OR operator displays a record if any of the conditions separated by OR
is TRUE.
AND Syntax
FROM table_name
OR Syntax
FROM table_name
NOT Syntax
FROM table_name
IN Syntax
SELECT column_name(s)
FROM table_name
or:
SELECT column_name(s)
FROM table_name
The BETWEEN operator selects values within a given range. The values can be
numbers, text, or dates.
The BETWEEN operator is inclusive: begin and end values are included.
BETWEEN Syntax
SELECT column_name(s)
FROM table_name
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.
ORDER BY Syntax
FROM table_name
Example
ORDER BY Country;
The following SQL statement selects all customers from the "Customers" table, sorted
DESCENDING by the "Country" column:
The following SQL statement selects all customers from the "Customers" table,
sorted by the "Country" and the "CustomerName" column. This means that it orders
by Country, but if some rows have the same Country, it orders them by
CustomerName: Eg: SELECT * FROM Customers ORDER BY Country,
CustomerName;
SQL Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name.
Alias Table
WHERE condition;
UPDATE Table
The following SQL statement updates the first customer (CustomerID = 1) with a new
contact person and a new city.
UPDATE Customers
WHERE CustomerID = 1;
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only
want to list the different (distinct) values.
The following SQL statement selects all (including the duplicates) values from the
"Country" column in the "Customers" table:
The following SQL statement selects only the DISTINCT values from the "Country"
column in the "Customers" table:
SELECT DISTINCT Country FROM Customers;
The following SQL statement lists the number of different (distinct) customer countries:
The LIKE operator is used in a WHERE clause to search for a specified pattern in a
column.
There are two wildcards often used in conjunction with the LIKE operator:
The percent sign and the underscore can also be used in combinations!
LIKE Syntax
FROM table_name
WHERE CustomerName LIKE 'a%' Finds any values that start with "a"
WHERE CustomerName LIKE '%a' Finds any values that end with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second
position
WHERE CustomerName LIKE 'a_%' Finds any values that start with "a" and are at least
2 characters in length
WHERE CustomerName LIKE 'a__%' Finds any values that start with "a" and are at least
3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that start with "a" and ends with
"o"
What is a NULL Value?
It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
We will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax
SELECT column_names
FROM table_name
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
The IS NULL operator is used to test for empty values (NULL values).
The following SQL lists all customers with a NULL value in the "Address" field:
FROM Customers
The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).
The following SQL lists all customers with a value in the "Address" field:
SELECT CustomerName, ContactName, Address
FROM Customers
Q: Find the sum, average, minimum, maximum value of salaries of employees in the
employee table
Count() function
Count(*)
This function returns the number of rows in the table that satisfy the criteria of select
statement.
In its counting, it includes duplicate rows and rows with NULL values in any of the
column
Example:
● This function returns the number of not null values in the specified column, but
includes duplicate values in counting
Example
This function returns the number of unique, not null values in the specified column.
Example
Q. Display the no of employees in each zone whose salary is greater than 32000
Having clause
Q. Display only whose departments with sum of salaries whose total salary is greater
than 70000
Cartesian Product (Cross Join or unrestricted join)
• Returns all the rows in the two tables listed in the query.
• Each row of the first table is paired with all the rows in the second table.
• This happens when there is no relationship between two tables. It is rarely useful.
● Specified columns from the joining tables are checked for equality.
● Values from joining tables are retrieved only if the condition in where clause is
satisfied. SYNTAX:-
SELECT <column_name (s)>
FROM <table_name1>, <table_name2>, ...., <table_nameN>
WHERE <table_name1>.<column_name> = <table_name2>.<column_name>;
Note-
To join n tables together minimum of n-1 join conditions are to be satisfied.
Results of Equi joins are the same as simple joins or inner joins. Inner join (simply
join) uses on clause.
You should always qualify the columns when joining tables having the same name as
corresponding columns. To qualify the columns we use “.” (dot) operator.
Natural Join
This clause is based on all the columns in the two tables that have the same name.
It selects the rows from two tables that have equal values in the matched columns.
SYNTAX:-
SELECT [column_names | *]
FROM table_name1
Note-No need to specify the column names to join. Works with same column name in
both the tables. The Resulting table has unique columns.