You are on page 1of 51

Centurion University of Technology and Management

Shivani Nanda
Database Concepts

Database
Concepts
1
Database Concepts
Data, Information, Database
Centurion University of Technology and Management

Data Collection of facts and figures that can be


processed to produce information
Database Collection of related data organized
in a particular way so that data can be easily
accessed, managed and updated i.e. database is a
place where related pieces of information are
stored and various operations are performed on it
Marks obtained by all students in a class
Toppers in a class; Average marks secured;

Shivani Nanda 2
Database Concepts
DBMS
Centurion University of Technology and Management

Database Management System Software that


allows creation, definition and manipulation of a
database i.e. it is actually a tool used to perform
any kind of operation on the data in the database
Apart from providing protection and security to
the database it also provides consistency in case
of multiple users
DBMS Software Oracle, MySQL, Microsoft
Access etc

Shivani Nanda 3
Database Concepts
Relational Database
Centurion University of Technology and Management

It is a collection of organized set of tables


from which data can be accessed easily
A relational database uses the concept of
linked two-dimensional tables which
comprises of rows and columns
Relationships can be drawn between
multiple tables and the output can again be
produced as a table

Shivani Nanda 4
Database Concepts
RDBMS
Centurion University of Technology and Management

It is a software used to manage


relational databases
As a database management system an
RDBMS software works on a relational
model where data is represented in
terms of tuples (rows)

Shivani Nanda 5
Database Concepts
Structured Query Language
Centurion University of Technology and Management

A universal programming language for accessing


databases pronounced as SEQUEL

SQL is called as a non-procedural easy to learn


language because it does not have programming
elements such as if-then-else, do-while as present
in procedural languages

Shivani Nanda 6
Database Concepts
RDBMS Basics
Centurion University of Technology and Management

In Relational database, a table is a collection of


data elements organized in terms of rows and
columns i.e. a table is also considered as
convenient representation of relations

Shivani Nanda 7
Database Concepts
RDBMS Basics(contd..)
Centurion University of Technology and Management

Record - A single entry in a table is called


a Record or Row representing set of related data

Field - A table consists of several records(row),


each record can be broken into several smaller
entities known as Fields
The previous Employee table consists
of four fields, ID, Name, Age and Salary
Shivani Nanda 8
Database Concepts
RDBMS Basics(contd..)
Centurion University of Technology and Management

Column - In Relational table, a column is a set


of value of a particular type
The term Attribute is also used to represent a
column For example, in Employee table,
Name is a column that represent
names of employee.

Shivani Nanda 9
Database Concepts
SQL Language Statements
Centurion University of Technology and Management

SQL defines data languages to manipulate data in


a RDBMS i.e. SQL language statements perform
all types of data operations in RDBMS
The various categories of SQL statements are as
follows:-
DDL Data Definition Language
DML Data Manipulation Language
DCL Data Control Language
TCL Transaction Control Language
Shivani Nanda 10
Database Concepts
Centurion University of Technology and Management

Data Definition Language(DDL) Statements


They deal with database description or
structure and decides how data should
reside in a database
They handle design and storage of database
objects
All DDL commands are auto committed i.e.
they save all changes permanently to the
database

Shivani Nanda 11
Database Concepts
DDL Commands
Centurion University of Technology and Management

CREATE - to create objects(say a table) in the


database
ALTER - alters the structure of the database
DROP - delete objects(say a table) from the
database
TRUNCATE - remove all records from a table,
including all spaces allocated for the records are
removed
RENAME - rename an object(say a table)

Shivani Nanda 12
Database Concepts
Centurion University of Technology and Management

Data Manipulation Language(DML) Statements


These are used for managing data within
database objects (say a table)
DML statements affect records in a table as they
perform basic operations on data such as
selecting a few records from a table,
inserting new records,
deleting unnecessary records, and
updating/modifying existing records
They are not auto committed
Shivani Nanda 13
Database Concepts
DML Commands
Centurion University of Technology and Management

SELECT - retrieve data from the


database table
INSERT - insert data into a table
UPDATE - updates existing data
within a table
DELETE - deletes all records from a
table, the space for the records remain
Shivani Nanda 14
Database Concepts
Centurion University of Technology and Management

Data Control Language(DCL) Statements


These statements control the level of access that
users have on database objects i.e. they are
mostly concerned with rights, permissions, and
other controls of the database system
GRANT - gives user's access privileges to
database
REVOKE - withdraw access privileges given
with the GRANT command

Shivani Nanda 15
Database Concepts
Centurion University of Technology and Management

Transaction Control Language(TCL) Statements


These statements are used to manage the changes
made by DML statements

COMMIT - save work done


SAVEPOINT - identify a point in a transaction to
which you can later roll back
ROLLBACK - restore database to original since
the last COMMIT

Shivani Nanda 16
Database Concepts
Terminal Basics
Centurion University of Technology and Management

>mysql u root -p
Enter password : CUTM
//syntax to create a database
mysql> create database student-name;
Query OK, 1 row affected
// to see the created database in the database list
mysql> show databases;
mysql> use student-name; // to change database
Database changed
Shivani Nanda 17
Database Concepts
Create table syntax
Centurion University of Technology and Management

create table table_name


(column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
.... );
create table Student(ID int, Name varchar(255),
Age int, Salary float);

Shivani Nanda 18
Database Concepts
Select Syntax
Centurion University of Technology and Management

Select * from table_name;


//displays only structure of the table if the table has
no data
//displays all the records if the table has data

Select column_name, column_name


from table_name;
//displays only selected columns from the table

Shivani Nanda 19
Database Concepts
Insert Syntax
Centurion University of Technology and Management

insert into table_name (column1,column2,


column3,...)values (value1,value2,value3,...);
//inserts records into the table

Variety one insert into Student(Id, Name, Age,


Salary) values(1,'Adam',34,13000);

Variety two insert into Student values


(2,'Alex',28,15000);
Shivani Nanda 20
Database Concepts
Insert NULL value to a column
Centurion University of Technology and Management

Suppose there is a Student table with a single


record as shown below:-

Now if we want to enter a record with NULL


value for age attribute then the syntax will be
insert into Student(id, name) values(102,'Alex');
OR
insert into Student values(102,'Alex',null);
Shivani Nanda 21
Database Concepts
Where Clause
Centurion University of Technology and Management

Where clause is used to specify condition while


retrieving data from table
Where clause is used mostly with Select, Update
and Delete query where if the condition specified
by where clause is true then only the result from
table is returned
select column-name1, column-name2..column-nameN
from table-name where [condition];

Shivani Nanda 22
Database Concepts
Where Clause Example
Centurion University of Technology and Management

select s_id, s_Name, age, address from Student


where s_id = 101;

Shivani Nanda 23
Database Concepts
Update Clause
Centurion University of Technology and Management

Update command is used to update an existing row


of a table
update table-name set column-name = value
where condition;

Shivani Nanda 24
Database Concepts
Update Clause
Centurion University of Technology and Management

update Student set age=18 where s_id=102;

update Student set s_name='Abhi',age=17


where s_id=103;

Shivani Nanda 25
Database Concepts
Delete Clause
Centurion University of Technology and Management

Delete command is used to delete data from a


table
Delete command can also be used with condition
to delete a particular row
delete from table-name;
//deletes all records from a table
delete from table-name where [condition];
//deletes a particular record from a table

Shivani Nanda 26
Centurion University of Technology and Management

Shivani Nanda
Database Concepts
Delete Clause

27
Database Concepts
Alter Clause
Centurion University of Technology and Management

alter command is used for alteration of table


structures and various uses of alter command are
to add a column to existing table
to rename any existing column
to change data type of any column or to
modify its size.
alter is also used to drop a column.

Shivani Nanda 28
Database Concepts
Alter Clause
Centurion University of Technology and Management

alter table table-name add(column-name datatype);


Ex:- alter table Student add(branch char);
alter table table-name rename old-column-name to
column-name;
Ex:- alter table Student rename address to Location;
alter table table-name modify(column-name datatype);
Ex:-alter table Student modify(address varchar(30));
alter table table-name drop(column-name);
Ex:- alter table Student drop(address);
Shivani Nanda 29
Database Concepts
truncate, drop and rename
Centurion University of Technology and Management

truncate command removes all records from a


table but it does not destroy the table's structure
truncate table table-name
drop query completely removes a table from
database and it also destroys the table structure
drop table table-name
rename command is used to rename a table
rename table old-table-name to new-table-
name
Shivani Nanda 30
Database Concepts
LIKE operator
Centurion University of Technology and Management

The LIKE operator is used to search for a


specified pattern in a column
select column_name(s) from table_name
There are two wildcard operators that are used in like clause:-
where
Percent signcolumn_name LIKE
% : represents zero, pattern;
one or more than one character
select *sign
Underscore _ : represents
from only ones_name
Student where characterlike 'A%';

select * from Student where s_name like '_d%';


select * from Student where s_name like '%x';
select * FROM Customers where s_name like
'%es%';
Shivani Nanda 31
Database Concepts
Between, Not Between Operators
Centurion University of Technology and Management

The between operator selects values within a


range where the values can be numbers, text, or
dates
select column_name(s) from table_name
where column_name between value1 and value2;
select * from Products where
Price between 10 AND 20;
select * from Products where
Price not between 10 AND 20;
Shivani Nanda 32
Database Concepts
Avg( )
Centurion University of Technology and Management

Average returns average value after calculating from


values in a numeric column
select avg(column_name) from table_name
Ex:- select avg(salary) from Emp;

Shivani Nanda 33
Database Concepts
Count( )
Centurion University of Technology and Management

Count returns the number of rows present in the table either


based on some condition or without condition
select count(column_name) from table-name
Ex:- select count (name) from Emp where salary = 8000;

Shivani Nanda 34
Database Concepts
Count(distinct)
Centurion University of Technology and Management

select count(distinct salary) from emp;

Shivani Nanda 35
Database Concepts
First( ), Last( )
Centurion University of Technology and Management

First function returns first value of a selected


column
select first(column_name) from table-name
select first(salary) from Emp;

Last return the last value from selected column


select last(column_name) from table-name
select last(salary) from Emp;

Shivani Nanda 36
Centurion University of Technology and Management

Shivani Nanda
Database Concepts

37
Database Concepts
Max( ), Min( )
Centurion University of Technology and Management

max( ) function returns maximum value from


selected column of the table
select max(column_name) from table-name
select max(salary) from emp;
min( ) function returns minimum value from a
selected column of the table
select min(column_name) from table-name
select min(salary) from emp;

Shivani Nanda 38
Centurion University of Technology and Management

Shivani Nanda
Database Concepts

39
Database Concepts
Sum( )
Centurion University of Technology and Management

Sum( ) function returns total sum of a selected


columns numeric values
SELECT SUM(column_name) from table-name
SELECT SUM(salary) from emp;

Shivani Nanda 40
Database Concepts
AND / OR Operators
Centurion University of Technology and Management

The AND & OR operators are used to filter records


based on more than one condition
The AND operator displays a record if both the first
condition AND the second condition are true while the
OR operator displays a record if either the first
condition OR the second condition is true.

select * from Customers where Country='GermanyAND City='Berlin';


select * from Customers where City='Berlin OR City='London';

select * from Customers where (Country='Germany OR UK)


AND (City='Berlin' OR City='London');
Shivani Nanda 41
Database Concepts
IN / NOT IN
Centurion University of Technology and Management

The IN operator allows you to specify multiple


values in a WHERE clause
select column_name(s) from table_name
where column_name IN (value1,value2,...);

select * from Customers where City IN (Mexico


D.F.', 'London');
select * from Customers where City NOT IN
(Mexico D.F.', 'London');
Shivani Nanda 42
Database Concepts
The SQL SELECT TOP Clause
Centurion University of Technology and Management

The SELECT TOP clause is used to specify the


number of records to return
The SELECT TOP clause can be very useful on
large tables with thousands of records
Returning a large number of records can impact
on performance
Not all database systems support the SELECT
TOP clause
select TOP 2* from Employee;
Shivani Nanda 43
Database Concepts
Centurion University of Technology and Management

LIMIT - SQL SELECT TOP Equivalent in MySQL and Oracle


SELECT column_name(s) FROM table_name
LIMIT number;
select * from Employee limit 5;

Shivani Nanda 44
Database Concepts
Group By
Centurion University of Technology and Management

The GROUP BY statement is used in


conjunction with the aggregate functions to
group the result-set by one or more columns
SELECT column_name,
aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;

Shivani Nanda 45
Centurion University of Technology and Management

Shivani Nanda
Database Concepts
Order By

46
Centurion University of Technology and Management

Shivani Nanda
Database Concepts

47
Centurion University of Technology and Management

Shivani Nanda
Database Concepts

48
Centurion University of Technology and Management

Shivani Nanda
Database Concepts

49
Database Concepts
Order By
Centurion University of Technology and Management

The SQL ORDER BY clause is used to sort the data


in ascending or descending order, based on one or
more columns
SELECT column-list FROM table_name [WHERE
condition] [ORDER BY column1, column2, ..
columnN] [ASC | DESC];
SELECT * FROM CUSTOMERS ORDER
BY NAME, SALARY;
SELECT * FROM CUSTOMERS ORDER
BY NAME DESC;
Shivani Nanda 50
Centurion University of Technology and Management

Shivani Nanda
Database Concepts

51

You might also like