SlideShare a Scribd company logo
DataBase
What is Data?
 data can be Unprocessed,UnOrganized,Meaningless,raw facts related to any
object in consideration.
 For example your name, age, height, weight, etc are some data related to
you.
 A picture , image , file , pdf etc can also be considered data.
What is Database?
Database is a systematic collection of data.
Databases support storage and manipulation of data.
Each database has one or more distinct APIs for creating, accessing,
managing, searching and replicating the data it holds.
Databases make data management easy. Let's discuss few examples.
An online telephone directory would definitely use database to store
data pertaining to people, phone numbers, other contact details, etc.
Your electricity service provider is obviously using a database to
manage billing , client related issues, to handle fault data, etc.
10/31/2017 1Information Science Dept. DB Lab Class Note
Cont…
What is a Database Management System (DBMS)?
• Database Management System (DBMS) is a collection of programs
which enables its users to access database, manipulate data, reporting
/ representation of data and control access to the database.
Types of DBMS
1. Hierarchical
 It employs the "parent-child" relationship of storing data.
 Its has tree like structure.
 It is rarely used nowadays.
2. Relational DBMS
 It defines database relationships in the form of tables.
 This is the most popular DBMS type in the market.
 Eg:MySQL, Oracle, and Microsoft SQL Server database.
3. Object Oriented Relation DBMS
 It supports storage of new data types.
 The data to be stored is in the form of objects.
 The objects to be stored in the database have attributes(i.e.
gender…) .
 Eg:c++,java,c#,c… etc.
10/31/2017 2Information Science Dept. DB Lab Class Note
Cont…
What is SQL?
SQL stands for Structured Query language .
• MySQL is a fast, easy-to-use RDBMS being used
for many small and big businesses on the market.
• MySQL is developed,marketed, and supported by
MySQL AB, which is a Swedish company.
• MySQL is becoming so popular .
How can we use MYSQL in Database
Program.
10/31/2017 3Information Science Dept. DB Lab Class Note
Cont…
MYSQL Exercise
• Use MYSQL Command Line Client provided
by MYSQLServer5.0/MY SQL Query
Browser.
• mysql > SHOW DATABASES;
Creating a database
• mysql > CREATE DATABASE database_name;
Eg: create database Section;
10/31/2017 4Information Science Dept. DB Lab Class Note
Cont…
Deleting a database
• mysql > DROP DATABASE database_name;
– Eg: Drop database Section;
To select a particular database
• mysql > USE database_name;
– Eg: use section;
10/31/2017 5Information Science Dept. DB Lab Class Note
Cont…
TO see tables in a database
• mysql >SHOW TABLES;
To create a new table in mysql
• mysql > CREATE TABLE table_name;
– Eg:CREATE TABLE student (id INT(3) NOT NULL PRIMARY
KEY , name VARCHAR(20), address VARCHAR(30), mobile
INT(10));
To see structure of Table in a mysql database
• mysql > DESCRIBE/DESC table_name;
– Eg: desc student;
10/31/2017 6Information Science Dept. DB Lab Class Note
Cont…
To insert data into a table
• mysql > INSERT INTO table_name;
– Eg:INSERT INTO student (id,Name,Address,Mobile) VALUES (01,
"John", "America","098765431”);
To insert more than one record at once, we can do this,
with each set of field values separated by a comma.
• Mysql>INSERT INTO table_name (column_1, column_2,
column_3, other_value) VALUES (value1, value2, value3, …),
(value1, value2, value3, …), (value1, value2, value3, …);
– Eg:INSERT INTO student VALUES(03,”Teklay
Brhane”,”Axum”,094856095),(04,”Brhane
kfle”,”Mekelle”,0914008514),(…);
To Make a copy of table
– INSERT INTO table_name SELECT * FROM table_name;
10/31/2017 7Information Science Dept. DB Lab Class Note
Cont…
To See contents inside a table
• mysql > SELECT * FROM table_name;
• Eg:Select * from student;
To Update a record in a table
• mysql > UPDATE table_name SET column1=value,
column2=value2,...
WHERE some_column=some_value;
• Eg: UPDATE student SET name = “John Mc”
WHERE id ='01';
10/31/2017 8Information Science Dept. DB Lab Class Note
Cont…
To Delete a record from a table
• mysql > DELETE from [table_name] where [column
name]=[field text];
• Eg: delete from student where id=“02”;
To delete all records at the same way from a table
• for slowly deletting data use:
– mysql>delete from table_name;
• for quickly daletting data use:
– Mysql>TRUNCATE TABLE table_name;
• for deletting the hole data from the table:
– Mysql>Delete from table-name;
10/31/2017 9Information Science Dept. DB Lab Class Note
Cont…
To Add a column in a table
• mysql > ALTER TABLE table_name ADD email
VARCHAR(40) AFTER mobile;
• Eg: ALTER TABLE student ADD email
VARCHAR(40) AFTER mobile;
10/31/2017 10Information Science Dept. DB Lab Class Note
Cont..
To modify a column in a table
• mysql> ALTER TABLE table_name MODIFY
Column_name Column Definition
• Eg:ALTER TABLE student MODIFY name
varchar(20);
To Change a column in a table
• mysql> ALTER TABLE table_name change Column_name
Column Definition
– Eg:ALTER TABLE student change name F_Nmae
varchar(20);
10/31/2017 11Information Science Dept. DB Lab Class Note
Cont…
To drop a column in a table
• mysql > ALTER TABLE table_name DROP
column_name
– Eg: ALTER TABLE student DROP
Fname;
10/31/2017 12Information Science Dept. DB Lab Class Note
SQL Constraints
• SQL constraints are used to specify rules
for the data in a table.
• If there is any violation between the
constraint and the data action, the action is
aborted by the constraint.
• Constraints can be specified when the table
is created (inside the CREATE TABLE
statement) or after the table is created
(inside the ALTER TABLE statement).
10/31/2017 13Information Science Dept. DB Lab Class Note
Cont…
• In SQL, we have the following constraints:
• NOT NULL - Indicates that a column cannot store NULL
value
• UNIQUE - Ensures that each row for a column must have a
unique value
• PRIMARY KEY - A combination of a NOT NULL and
UNIQUE. Ensures that a column (or combination of two or
more columns) have a unique identity which helps to find a
particular record in a table more easily and quickly
• FOREIGN KEY - Ensure the referential integrity of the
data in one table to match values in another table
• CHECK - Ensures that the value in a column meets a
specific condition
• DEFAULT - Specifies a default value for a column
10/31/2017 14Information Science Dept. DB Lab Class Note
SQL Not Null
• By default, a table column can hold
NULL values.
• The NOT NULL constraint enforces
a column to NOT accept NULL values.
• Eg: CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255));
10/31/2017 15Information Science Dept. DB Lab Class Note
SQL DEFAULT
• To create a Default constraint on creating a table
– Eg: CREATE TABLE Persons
(
P_Id int NOT NULL default”0”,
LastName varchar(255) NOT NULL default”YES”,
FirstName varchar(255),
Address varchar(255),
City varchar(255));
• To create a DEFAULT constraint on the "City" column when the
table is already created, use the following SQL:
• Mysql>alter table table_name alter column_name set default””;
– Eg: ALTER TABLE student ALTER id SET DEFAULT “0”;
• To drop a DEFAULT constraint, use the following SQL:
• Mysql> alter table table_name alter column_name drop default;
– Eg: ALTER TABLE student ALTER id DROP DEFAULT;
10/31/2017 16Information Science Dept. DB Lab Class Note
SQL Primary Key
• The PRIMARY KEY constraint uniquely
identifies each record in a database table.
• Primary keys must contain UNIQUE
values.
• A primary key column cannot contain NULL
values.
• Most tables should have a primary key, and
each table can have only ONE primary key.
10/31/2017 17Information Science Dept. DB Lab Class Note
Cont…
• SQL PRIMARY KEY Constraint on CREATE TABLE
– Eg: CREATE TABLE Person(P_Id int NOT NULL,FirstName
varchar(255) NOT NULL,LastName varchar(255),PRIMARY KEY
(P_Id));
• SQL PRIMARY KEY Constraint on ALTER TABLE
• Mysql>alter table table_name add primary key(column_name);
– Eg: Alter table person add primary
key(p_id);
• To DROP a PRIMARY KEY Constraint
• Mysql>Alter table table-name drope primary key;
– Eg: Alter table person drop primary key;
10/31/2017 18Information Science Dept. DB Lab Class Note
SQL UNIQUE
• The UNIQUE constraint uniquely identifies each record in a
database table.
• SQL UNIQUE Constraint on CREATE TABLE
– Eg: CREATE TABLE Person(P_Id int NOT NULL,FirstName
varchar(255) NOT NULL,LastName varchar(255),UNIQUE
(P_Id));
• SQL UNIQUE Constraint on a already created TABLE
• Mysql> alter table table-name add unique(column_name);
– Eg:Alter table person add unique(FirstName);
• SQL To DROP a UNIQUE Constraint form a database
table
• Mysql> alter table table-name drop index (column_name);
– Eg:ALTER TABLE Person DROP INDEX FirstName;
10/31/2017 19Information Science Dept. DB Lab Class Note
SQL Foreign Key
• A FOREIGN KEY in one table points to a PRIMARY KEY in another
table
• SQL FOREIGN KEY Constraint on CREATE TABLE
• CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)
• SQL FOREIGN KEY Constraint on ALTER TABLE
• Mysql>alter table table_name add index fk_(column_name);
• Eg: alter table student add index fk_(section);
• To DROP a FOREIGN KEY Constraint
• Mysql>alter table table_name drop index(column_name);
• Eg: ALTER TABLE Orders
DROP FOREIGN KEY fk_PerOrders
10/31/2017 20Information Science Dept. DB Lab Class Note
Questions???
Field Type Null Key Default Extra
Stu_ID
firstname
lastname
Address
Email
mobile
int (4)
char(15)
char(15)
varchar(50)
varchar(30)
int(10)
NO
NO
YES
YES
YES
YES
PRIM
UNI
MUL
0
NO
YES
YES
YES
YES
Auto increment
1) Create a table called Mytable in database called Mydatabase (given below is definition of table)
2) Enter four rows of data into the tables
3) Delete column from above table
4) Drop primary key from above table
5) Modify column name firstname to fname and lastname to lname
6) Add new column grade after column mobile & make varchar datatype. Set your default
value “NO”. And then insert any value to Mobile column.
10/31/2017 21Information Science Dept. DB Lab Class Note
• Create table orders like below:
• Create table customers like below:
10/31/2017 Information Science Dept. DB Lab Class Note 22
SQL JOIN
• INNER JOIN: Returns all rows when
there is at least one match in BOTH tables
• LEFT JOIN: Return all rows from the left
table, and the matched rows from the
right table
• RIGHT JOIN: Return all rows from the
right table, and the matched rows from
the left table
• FULL JOIN: Return all rows when there is
a match in ONE of the tables
10/31/2017 Information Science Dept. DB Lab Class Note 23
SQL Inner Join
• The INNER JOIN keyword selects all
rows from both tables as long as there is a
match between the columns in both tables.
• PS! INNER JOIN is the same as JOIN.
10/31/2017 Information Science Dept. DB Lab Class Note 24
SQL Inner Join
• SQL INNER JOIN Syntax
• Mysql>SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
• Example
• SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
10/31/2017 Information Science Dept. DB Lab Class Note 25
SQL Left Join
• The LEFT JOIN keyword returns all rows
from the left table (table1), with the
matching rows in the right table (table2).
The result is NULL in the right side when
there is no match.
• PS! In some databases LEFT JOIN is called LEFT OUTER
JOIN.
10/31/2017 Information Science Dept. DB Lab Class Note 26
SQL Left Join
• SQL LEFT JOIN Syntax
• Mysql>SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
• Example
• SELECT Orders.order_id, Customers.CustomerName,
Orderd.OrderDate
FROM customers
LEFT JOIN orders
ON Orders.customer_id=Customers.customer_id order by
Customers.CustomerName;
10/31/2017 Information Science Dept. DB Lab Class Note 27
SQL Right Join
10/31/2017 Information Science Dept. DB Lab Class Note 28
• The RIGHT JOIN keyword returns all rows
from the right table (table2), with the
matching rows in the left table (table1).
The result is NULL in the left side when
there is no match.
• PS! In some databases RIGHT JOIN is called RIGHT OUTER
JOIN.
SQL RIGHT JOIN
• SQL RIGHT JOIN Syntax
• Mysql>SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
• Example
• SELECT Orders.order_id,
Customers.CustomerName, Orders.OrderDate
• FROM orders
• RIGHT JOIN customers
• ON Orders.customer_id=Customers.customer_id
order by Customers.CustomerName;
10/31/2017 Information Science Dept. DB Lab Class Note 29
SQL Full Join
• The FULL OUTER JOIN keyword
returns all rows from the left table
(table1) and from the right table
(table2).
• PS! FULL OUTER JOIN = LEFT + RIGHT joins.
10/31/2017 Information Science Dept. DB Lab Class Note 30
SQL FULL OUTER JOIN
• SQL FULL OUTER JOIN Syntax
• Mysql>SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
• Example
• SELECT Customers.CustomerName,
Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
•
10/31/2017 Information Science Dept. DB Lab Class Note 31
SQL Union
• The UNION operator is used to
combine the result-set of two or more
SELECT statements.
• Notice that each SELECT statement
within the UNION must have the same
number of columns.
• The columns must also have similar
data types. Also, the columns in each
SELECT statement must be in the
same order.
10/31/2017 Information Science Dept. DB Lab Class Note 32
SQL Union
• SQL UNION Syntax
• Mysql>SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
• All keyword allows dupicationof values but not union
keyword.
• Example
SELECT customer_id
FROM Customers
union
select customer_id from orders;
10/31/2017 Information Science Dept. DB Lab Class Note 33

More Related Content

What's hot (19)

ODP
Ms sql-server
Md.Mojibul Hoque
 
PPTX
What is SQL Server?
CPD INDIA
 
PPTX
SQL Commands
Sachidananda M H
 
PPT
MySql slides (ppt)
webhostingguy
 
PPTX
Introduction to database
Pongsakorn U-chupala
 
PPTX
Introduction to (sql)
Dattatray Ghorpade
 
PPTX
Avinash database
avibmas
 
PPT
Sql server T-sql basics ppt-3
Vibrant Technologies & Computers
 
PPTX
Data concepts
Sachidananda M H
 
PDF
Database Architecture and Basic Concepts
Tony Wong
 
PPTX
Getting Started with MySQL II
Sankhya_Analytics
 
PPTX
SQL SERVER Training in Pune Slides
enosislearningcom
 
PPTX
MySQL Essential Training
HudaRaghibKadhim
 
PDF
SQL Overview
Stewart Rogers
 
PDF
PT- Oracle session01
Karthik Venkatachalam
 
PPTX
Introduction to mysql part 1
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Chapter 4 Structured Query Language
Eddyzulham Mahluzydde
 
PPTX
Introduction to SQL
Ehsan Hamzei
 
Ms sql-server
Md.Mojibul Hoque
 
What is SQL Server?
CPD INDIA
 
SQL Commands
Sachidananda M H
 
MySql slides (ppt)
webhostingguy
 
Introduction to database
Pongsakorn U-chupala
 
Introduction to (sql)
Dattatray Ghorpade
 
Avinash database
avibmas
 
Sql server T-sql basics ppt-3
Vibrant Technologies & Computers
 
Data concepts
Sachidananda M H
 
Database Architecture and Basic Concepts
Tony Wong
 
Getting Started with MySQL II
Sankhya_Analytics
 
SQL SERVER Training in Pune Slides
enosislearningcom
 
MySQL Essential Training
HudaRaghibKadhim
 
SQL Overview
Stewart Rogers
 
PT- Oracle session01
Karthik Venkatachalam
 
Chapter 4 Structured Query Language
Eddyzulham Mahluzydde
 
Introduction to SQL
Ehsan Hamzei
 

Similar to Data base.ppt (20)

PPTX
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
WrushabhShirsat3
 
PPTX
unit-ii.pptx
NilamHonmane
 
DOCX
DBMS LAB M.docx
SuhaniSinha9
 
PPTX
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
deeptanshudas100
 
PPTX
SQL: Data Definition Language(DDL) command
sonali sonavane
 
PPTX
Getting Started with MySQL I
Sankhya_Analytics
 
PPTX
Mysql-overview.pptx
TamilHunt
 
PPTX
Lecture - MY-SQL/ SQL Commands - DDL.pptx
umershah0263
 
PPT
Database management and System Development ppt
michaelkasule
 
PPTX
PostgreSQL Database Slides
metsarin
 
DOC
Module 3
cs19club
 
PPTX
SQL.pptx for the begineers and good know
PavithSingh
 
PPTX
SQL basics.pptx
ZakReeceJames
 
PPTX
Unit - II.pptx
MrsSavitaKumbhare
 
PPT
Les10.ppt
AlhassanFederated
 
PDF
MySQL for beginners
Saeid Zebardast
 
PDF
SQL for Data Science Tutorial | Data Science Tutorial | Edureka
Edureka!
 
PPTX
database language ppt.pptx
Anusha sivakumar
 
PDF
Steps towards of sql server developer
Ahsan Kabir
 
PPT
My sql
Muhammad Umar
 
xjtrutdctrd5454drxxresersestryugyufy6rythgfytfyt
WrushabhShirsat3
 
unit-ii.pptx
NilamHonmane
 
DBMS LAB M.docx
SuhaniSinha9
 
SQL _UNIT_DBMS_PRESENTSTATION_SQL _UNIT_DBMS_PRESENTSTATION
deeptanshudas100
 
SQL: Data Definition Language(DDL) command
sonali sonavane
 
Getting Started with MySQL I
Sankhya_Analytics
 
Mysql-overview.pptx
TamilHunt
 
Lecture - MY-SQL/ SQL Commands - DDL.pptx
umershah0263
 
Database management and System Development ppt
michaelkasule
 
PostgreSQL Database Slides
metsarin
 
Module 3
cs19club
 
SQL.pptx for the begineers and good know
PavithSingh
 
SQL basics.pptx
ZakReeceJames
 
Unit - II.pptx
MrsSavitaKumbhare
 
MySQL for beginners
Saeid Zebardast
 
SQL for Data Science Tutorial | Data Science Tutorial | Edureka
Edureka!
 
database language ppt.pptx
Anusha sivakumar
 
Steps towards of sql server developer
Ahsan Kabir
 
Ad

Recently uploaded (20)

PPTX
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
PPTX
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
PPTX
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
PPTX
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
PDF
Merits and Demerits of DBMS over File System & 3-Tier Architecture in DBMS
MD RIZWAN MOLLA
 
PPTX
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
PPTX
ER_Model_Relationship_in_DBMS_Presentation.pptx
dharaadhvaryu1992
 
PPTX
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
PPTX
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
PDF
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
PPTX
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
PDF
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
PDF
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
PDF
Building Production-Ready AI Agents with LangGraph.pdf
Tamanna
 
PPTX
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
PDF
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
PPTX
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
PPTX
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
PDF
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
PPTX
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
Numbers of a nation: how we estimate population statistics | Accessible slides
Office for National Statistics
 
apidays Helsinki & North 2025 - From Chaos to Clarity: Designing (AI-Ready) A...
apidays
 
Merits and Demerits of DBMS over File System & 3-Tier Architecture in DBMS
MD RIZWAN MOLLA
 
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
ER_Model_Relationship_in_DBMS_Presentation.pptx
dharaadhvaryu1992
 
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
apidays Helsinki & North 2025 - APIs in the healthcare sector: hospitals inte...
apidays
 
Advanced_NLP_with_Transformers_PPT_final 50.pptx
Shiwani Gupta
 
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
Building Production-Ready AI Agents with LangGraph.pdf
Tamanna
 
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
Aict presentation on dpplppp sjdhfh.pptx
vabaso5932
 
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
Ad

Data base.ppt

  • 1. DataBase What is Data?  data can be Unprocessed,UnOrganized,Meaningless,raw facts related to any object in consideration.  For example your name, age, height, weight, etc are some data related to you.  A picture , image , file , pdf etc can also be considered data. What is Database? Database is a systematic collection of data. Databases support storage and manipulation of data. Each database has one or more distinct APIs for creating, accessing, managing, searching and replicating the data it holds. Databases make data management easy. Let's discuss few examples. An online telephone directory would definitely use database to store data pertaining to people, phone numbers, other contact details, etc. Your electricity service provider is obviously using a database to manage billing , client related issues, to handle fault data, etc. 10/31/2017 1Information Science Dept. DB Lab Class Note
  • 2. Cont… What is a Database Management System (DBMS)? • Database Management System (DBMS) is a collection of programs which enables its users to access database, manipulate data, reporting / representation of data and control access to the database. Types of DBMS 1. Hierarchical  It employs the "parent-child" relationship of storing data.  Its has tree like structure.  It is rarely used nowadays. 2. Relational DBMS  It defines database relationships in the form of tables.  This is the most popular DBMS type in the market.  Eg:MySQL, Oracle, and Microsoft SQL Server database. 3. Object Oriented Relation DBMS  It supports storage of new data types.  The data to be stored is in the form of objects.  The objects to be stored in the database have attributes(i.e. gender…) .  Eg:c++,java,c#,c… etc. 10/31/2017 2Information Science Dept. DB Lab Class Note
  • 3. Cont… What is SQL? SQL stands for Structured Query language . • MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses on the market. • MySQL is developed,marketed, and supported by MySQL AB, which is a Swedish company. • MySQL is becoming so popular . How can we use MYSQL in Database Program. 10/31/2017 3Information Science Dept. DB Lab Class Note
  • 4. Cont… MYSQL Exercise • Use MYSQL Command Line Client provided by MYSQLServer5.0/MY SQL Query Browser. • mysql > SHOW DATABASES; Creating a database • mysql > CREATE DATABASE database_name; Eg: create database Section; 10/31/2017 4Information Science Dept. DB Lab Class Note
  • 5. Cont… Deleting a database • mysql > DROP DATABASE database_name; – Eg: Drop database Section; To select a particular database • mysql > USE database_name; – Eg: use section; 10/31/2017 5Information Science Dept. DB Lab Class Note
  • 6. Cont… TO see tables in a database • mysql >SHOW TABLES; To create a new table in mysql • mysql > CREATE TABLE table_name; – Eg:CREATE TABLE student (id INT(3) NOT NULL PRIMARY KEY , name VARCHAR(20), address VARCHAR(30), mobile INT(10)); To see structure of Table in a mysql database • mysql > DESCRIBE/DESC table_name; – Eg: desc student; 10/31/2017 6Information Science Dept. DB Lab Class Note
  • 7. Cont… To insert data into a table • mysql > INSERT INTO table_name; – Eg:INSERT INTO student (id,Name,Address,Mobile) VALUES (01, "John", "America","098765431”); To insert more than one record at once, we can do this, with each set of field values separated by a comma. • Mysql>INSERT INTO table_name (column_1, column_2, column_3, other_value) VALUES (value1, value2, value3, …), (value1, value2, value3, …), (value1, value2, value3, …); – Eg:INSERT INTO student VALUES(03,”Teklay Brhane”,”Axum”,094856095),(04,”Brhane kfle”,”Mekelle”,0914008514),(…); To Make a copy of table – INSERT INTO table_name SELECT * FROM table_name; 10/31/2017 7Information Science Dept. DB Lab Class Note
  • 8. Cont… To See contents inside a table • mysql > SELECT * FROM table_name; • Eg:Select * from student; To Update a record in a table • mysql > UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value; • Eg: UPDATE student SET name = “John Mc” WHERE id ='01'; 10/31/2017 8Information Science Dept. DB Lab Class Note
  • 9. Cont… To Delete a record from a table • mysql > DELETE from [table_name] where [column name]=[field text]; • Eg: delete from student where id=“02”; To delete all records at the same way from a table • for slowly deletting data use: – mysql>delete from table_name; • for quickly daletting data use: – Mysql>TRUNCATE TABLE table_name; • for deletting the hole data from the table: – Mysql>Delete from table-name; 10/31/2017 9Information Science Dept. DB Lab Class Note
  • 10. Cont… To Add a column in a table • mysql > ALTER TABLE table_name ADD email VARCHAR(40) AFTER mobile; • Eg: ALTER TABLE student ADD email VARCHAR(40) AFTER mobile; 10/31/2017 10Information Science Dept. DB Lab Class Note
  • 11. Cont.. To modify a column in a table • mysql> ALTER TABLE table_name MODIFY Column_name Column Definition • Eg:ALTER TABLE student MODIFY name varchar(20); To Change a column in a table • mysql> ALTER TABLE table_name change Column_name Column Definition – Eg:ALTER TABLE student change name F_Nmae varchar(20); 10/31/2017 11Information Science Dept. DB Lab Class Note
  • 12. Cont… To drop a column in a table • mysql > ALTER TABLE table_name DROP column_name – Eg: ALTER TABLE student DROP Fname; 10/31/2017 12Information Science Dept. DB Lab Class Note
  • 13. SQL Constraints • SQL constraints are used to specify rules for the data in a table. • If there is any violation between the constraint and the data action, the action is aborted by the constraint. • Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the table is created (inside the ALTER TABLE statement). 10/31/2017 13Information Science Dept. DB Lab Class Note
  • 14. Cont… • In SQL, we have the following constraints: • NOT NULL - Indicates that a column cannot store NULL value • UNIQUE - Ensures that each row for a column must have a unique value • PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have a unique identity which helps to find a particular record in a table more easily and quickly • FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table • CHECK - Ensures that the value in a column meets a specific condition • DEFAULT - Specifies a default value for a column 10/31/2017 14Information Science Dept. DB Lab Class Note
  • 15. SQL Not Null • By default, a table column can hold NULL values. • The NOT NULL constraint enforces a column to NOT accept NULL values. • Eg: CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255)); 10/31/2017 15Information Science Dept. DB Lab Class Note
  • 16. SQL DEFAULT • To create a Default constraint on creating a table – Eg: CREATE TABLE Persons ( P_Id int NOT NULL default”0”, LastName varchar(255) NOT NULL default”YES”, FirstName varchar(255), Address varchar(255), City varchar(255)); • To create a DEFAULT constraint on the "City" column when the table is already created, use the following SQL: • Mysql>alter table table_name alter column_name set default””; – Eg: ALTER TABLE student ALTER id SET DEFAULT “0”; • To drop a DEFAULT constraint, use the following SQL: • Mysql> alter table table_name alter column_name drop default; – Eg: ALTER TABLE student ALTER id DROP DEFAULT; 10/31/2017 16Information Science Dept. DB Lab Class Note
  • 17. SQL Primary Key • The PRIMARY KEY constraint uniquely identifies each record in a database table. • Primary keys must contain UNIQUE values. • A primary key column cannot contain NULL values. • Most tables should have a primary key, and each table can have only ONE primary key. 10/31/2017 17Information Science Dept. DB Lab Class Note
  • 18. Cont… • SQL PRIMARY KEY Constraint on CREATE TABLE – Eg: CREATE TABLE Person(P_Id int NOT NULL,FirstName varchar(255) NOT NULL,LastName varchar(255),PRIMARY KEY (P_Id)); • SQL PRIMARY KEY Constraint on ALTER TABLE • Mysql>alter table table_name add primary key(column_name); – Eg: Alter table person add primary key(p_id); • To DROP a PRIMARY KEY Constraint • Mysql>Alter table table-name drope primary key; – Eg: Alter table person drop primary key; 10/31/2017 18Information Science Dept. DB Lab Class Note
  • 19. SQL UNIQUE • The UNIQUE constraint uniquely identifies each record in a database table. • SQL UNIQUE Constraint on CREATE TABLE – Eg: CREATE TABLE Person(P_Id int NOT NULL,FirstName varchar(255) NOT NULL,LastName varchar(255),UNIQUE (P_Id)); • SQL UNIQUE Constraint on a already created TABLE • Mysql> alter table table-name add unique(column_name); – Eg:Alter table person add unique(FirstName); • SQL To DROP a UNIQUE Constraint form a database table • Mysql> alter table table-name drop index (column_name); – Eg:ALTER TABLE Person DROP INDEX FirstName; 10/31/2017 19Information Science Dept. DB Lab Class Note
  • 20. SQL Foreign Key • A FOREIGN KEY in one table points to a PRIMARY KEY in another table • SQL FOREIGN KEY Constraint on CREATE TABLE • CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) ) • SQL FOREIGN KEY Constraint on ALTER TABLE • Mysql>alter table table_name add index fk_(column_name); • Eg: alter table student add index fk_(section); • To DROP a FOREIGN KEY Constraint • Mysql>alter table table_name drop index(column_name); • Eg: ALTER TABLE Orders DROP FOREIGN KEY fk_PerOrders 10/31/2017 20Information Science Dept. DB Lab Class Note
  • 21. Questions??? Field Type Null Key Default Extra Stu_ID firstname lastname Address Email mobile int (4) char(15) char(15) varchar(50) varchar(30) int(10) NO NO YES YES YES YES PRIM UNI MUL 0 NO YES YES YES YES Auto increment 1) Create a table called Mytable in database called Mydatabase (given below is definition of table) 2) Enter four rows of data into the tables 3) Delete column from above table 4) Drop primary key from above table 5) Modify column name firstname to fname and lastname to lname 6) Add new column grade after column mobile & make varchar datatype. Set your default value “NO”. And then insert any value to Mobile column. 10/31/2017 21Information Science Dept. DB Lab Class Note
  • 22. • Create table orders like below: • Create table customers like below: 10/31/2017 Information Science Dept. DB Lab Class Note 22
  • 23. SQL JOIN • INNER JOIN: Returns all rows when there is at least one match in BOTH tables • LEFT JOIN: Return all rows from the left table, and the matched rows from the right table • RIGHT JOIN: Return all rows from the right table, and the matched rows from the left table • FULL JOIN: Return all rows when there is a match in ONE of the tables 10/31/2017 Information Science Dept. DB Lab Class Note 23
  • 24. SQL Inner Join • The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables. • PS! INNER JOIN is the same as JOIN. 10/31/2017 Information Science Dept. DB Lab Class Note 24
  • 25. SQL Inner Join • SQL INNER JOIN Syntax • Mysql>SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name=table2.column_name; • Example • SELECT Customers.CustomerName, Orders.OrderID FROM Customers INNER JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY Customers.CustomerName; 10/31/2017 Information Science Dept. DB Lab Class Note 25
  • 26. SQL Left Join • The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match. • PS! In some databases LEFT JOIN is called LEFT OUTER JOIN. 10/31/2017 Information Science Dept. DB Lab Class Note 26
  • 27. SQL Left Join • SQL LEFT JOIN Syntax • Mysql>SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name; • Example • SELECT Orders.order_id, Customers.CustomerName, Orderd.OrderDate FROM customers LEFT JOIN orders ON Orders.customer_id=Customers.customer_id order by Customers.CustomerName; 10/31/2017 Information Science Dept. DB Lab Class Note 27
  • 28. SQL Right Join 10/31/2017 Information Science Dept. DB Lab Class Note 28 • The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match. • PS! In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
  • 29. SQL RIGHT JOIN • SQL RIGHT JOIN Syntax • Mysql>SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name; • Example • SELECT Orders.order_id, Customers.CustomerName, Orders.OrderDate • FROM orders • RIGHT JOIN customers • ON Orders.customer_id=Customers.customer_id order by Customers.CustomerName; 10/31/2017 Information Science Dept. DB Lab Class Note 29
  • 30. SQL Full Join • The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table (table2). • PS! FULL OUTER JOIN = LEFT + RIGHT joins. 10/31/2017 Information Science Dept. DB Lab Class Note 30
  • 31. SQL FULL OUTER JOIN • SQL FULL OUTER JOIN Syntax • Mysql>SELECT column_name(s) FROM table1 FULL OUTER JOIN table2 ON table1.column_name=table2.column_name; • Example • SELECT Customers.CustomerName, Orders.OrderID FROM Customers FULL OUTER JOIN Orders ON Customers.CustomerID=Orders.CustomerID ORDER BY Customers.CustomerName; • 10/31/2017 Information Science Dept. DB Lab Class Note 31
  • 32. SQL Union • The UNION operator is used to combine the result-set of two or more SELECT statements. • Notice that each SELECT statement within the UNION must have the same number of columns. • The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order. 10/31/2017 Information Science Dept. DB Lab Class Note 32
  • 33. SQL Union • SQL UNION Syntax • Mysql>SELECT column_name(s) FROM table1 UNION ALL SELECT column_name(s) FROM table2; • All keyword allows dupicationof values but not union keyword. • Example SELECT customer_id FROM Customers union select customer_id from orders; 10/31/2017 Information Science Dept. DB Lab Class Note 33