SlideShare a Scribd company logo
1 of 106
SQL : Day 1
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
Contents
▪ Database Management System (DBMS) - Definition and importance
▪ Introduction to Structured Query Language (SQL)
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
SQL: Structured Query Language
▪ SQL (Structured Query Language): Standard language for
interacting with relational databases
▪ Key characteristics:
▪ Declarative language
▪ Uses English-like syntax
▪ Supports data definition, manipulation, and control
© Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
SQL: Basics
▪ SQL Statements:
▪ Data Definition Language (DDL): CREATE, ALTER, DROP
▪ Data Manipulation Language (DML): SELECT, INSERT, UPDATE, DELETE
▪ Data Control Language (DCL): GRANT, REVOKE
▪ SQL Queries:
▪ SELECT statement: Retrieves data from a database table
▪ Example: SELECT * FROM Customers WHERE Country = 'USA';
SQL: SQL Database Operations
Creating Database and Tables:
• CREATE DATABASE database_name;
• CREATE TABLE table_name (column1 datatype, column2 datatype, ...);
Inserting Data:
• INSERT INTO table_name (column1, column2, ...) VALUES (value1,
value2, ...);
Updating Data:
• UPDATE table_name SET column_name = new_value WHERE
condition;
Deleting Data:
• DELETE FROM table_name WHERE condition;
SQL: SQL Database Operations
Joins:
Combine rows from multiple tables based on related columns
Types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN
Aggregation Functions:
Perform calculations on groups of rows
Examples: SUM, AVG, COUNT, MAX, MIN
Constraints:
Rules applied to columns to enforce data integrity
Examples: PRIMARY KEY, FOREIGN KEY, NOT NULL
Summary
• Database Management System (DBMS) is essential for efficient data
management.
• SQL is a powerful language for interacting with relational databases.
• Understanding DBMS and SQL enables effective data storage, retrieval, and
manipulation.
Questions …Any?
Day 2:
Entity Relationship Model
Precap
▪ Database Management System (DBMS) - Definition and importance
▪ Introduction to Structured Query Language (SQL)
Contents
▪ Entity Relationship Model
▪ Relationships
▪ ER Diagram Representations
▪ ER Diagram Examples
Entity Relationship Model
• An Entity Relationship Diagram (ERD) is a visual
representation of different data using conventions that
describe how these data are related to each other.
For example, the elements writer, novel, and consumer may be
described using ER diagrams this way:
•
The elements inside rectangles are
called entities while the items inside
diamonds denote the relationships
between entities.
Entity
• An entity can be a real-world object, either animate or inanimate,
that can be easily identifiable.
– For example, in a school database, students, teachers,
classes, and courses offered can be considered as entities.
– All these entities have some attributes or properties that give
them their identity.
An entity set is a collection of similar types of entities.
– An entity set may contain entities with attribute sharing
similar values.
– For example,
– a Students set may contain all the students of a school;
– likewise a Teachers set may contain all the teachers of a
school from all faculties. Entity sets need not be disjoint.
•
Weak Entity
• A weak entity is an entity that depends on the existence of
another entity.
In more technical terms it can defined as an entity that cannot be
identified by its own attributes.
It uses a foreign key combined with its attributed to form the
primary key.
An entity like order item is a good example for this.
– The order item will be meaningless without an order so it
depends on the existence of order.
•
•
•
Attributes
• Entities are represented by means of their properties,
called attributes.
• All attributes have values.
• For example, a student entity may have name, class,
and age as attributes.
• There exists a domain or range of values that can be
assigned to attributes.
• For example, a student's name cannot be a numeric
value. It has to be alphabetic. A student's age cannot
be negative, etc.
Types of Attributes
• Simple attribute − Simple attributes are atomic values, which
cannot be divided further. For example, a student's phone
number is an atomic value of 10 digits.
• Composite attribute − Composite attributes are made of more
than one simple attribute. For example, a student's complete
name may have first_name and last_name.
Types of Attributes (contd.,)
• Derived attribute − Derived attributes are the attributes that do not exist in
the physical database, but their values are derived from other attributes
present in the database.
• For example, average_salary in a department should not be saved directly in
the database, instead it can be derived.
•
•
For another example, age can be derived from data_of_birth.
For example for a circle the area can be derived from the radius.
Types of Attributes (contd.,)
• Single-value attribute − Single-value attributes contain single
value. For example − Social_Security_Number.
• Multi-value attribute − Multi-value attributes may contain more
than one values. For example, a person can have more than one
phone number, email_address, etc.
• For example a teacher entity can have multiple subject values.
• These attribute types can come together in a
way like −
• simple single-valued attributes
• simple multi-valued attributes
• composite single-valued attributes
• composite multi-valued attributes
Entity-Set and Keys
• Key is an attribute or collection of attributes that uniquely
identifies an entity among entity set.
For example, the roll_number of a student makes him/her
identifiable among students.
Super Key − A set of attributes (one or more) that collectively
identifies an entity in an entity set.
•
•
• Candidate Key − A minimal super key is called a candidate key.
An entity set may have more than one candidate key.
• Primary Key − A primary key is one of the candidate keys chosen
by the database designer to uniquely identify the entity set.
Relationship
•
•
The association among entities is called a relationship.
For example, an employee works_at a department, a
student enrolls in a course.
• Here, Works_at and Enrolls are called relationships.
For example, the entity “carpenter” may be related to the entity
“table” by the relationship “builds” or “makes”. Relationships
are represented by diamond shapes and are labeled using verbs.
•
Degree of Relationship
• The number of participating entities in a
relationship defines the degree of the
relationship.
• Binary = degree 2
• Ternary = degree 3
• n-ary = degree
Mapping Cardinalities
• Cardinality defines the number of entities in one entity set, which
can be associated with the number of entities of other set via
relationship set.
• One-to-one − One entity from entity set A can be associated with
at most one entity of entity set B and vice versa.
• One-to-many − One entity from entity set A can be associated
with more than one entities of entity set B however an entity
from entity set B, can be associated with at most one entity.
• Many-to-one − More than one entities from entity set A can
be associated with at most one entity of entity set B, however
an entity from entity set B can be associated with more than
one entity from entity set A.
• Many-to-many − One entity from A can be associated with more than one
entity from B and vice versa.
ER Diagram Representation
• Let us now learn how the ER Model is represented by means of
an ER diagram.
Any object, for example, entities, attributes of an entity,
relationship sets, and attributes of relationship sets, can be
represented with the help of an ER diagram.
•
Entity
• Entities are
Rectangles
represent.
represented by means
are named with the entity
of rectangles.
set they
Attributes
• Attributes are the properties of entities. Attributes are
represented by means of ellipses.
Every ellipse represents one attribute and is directly
connected to its entity (rectangle).
•
• If the attributes are composite, they are further divided in a
tree like structure. Every node is then connected to its
attribute. That is, composite attributes are represented by
ellipses that are connected with an ellipse.
• Multivalued attributes are depicted by double
ellipse.
• Derived attributes are depicted by dashed ellipse.
Relationship
• Relationships are represented by diamond-shaped box.
Name of the relationship is written inside the diamond-box.
All theentities (rectangles) participating in a relationship,
are connected to it by a line.
•
•
• Binary Relationship and Cardinality
• A relationship where two entities are participating is called a binary
relationship. Cardinality is the number of instance of an entity from
a relation that can be associated with the relation.
• One-to-one − When only one instance of an entity is
associated with the relationship, it is marked as '1:1'. The
following image reflects that only one instance of each entity
should be associated with the relationship. It depicts one-to-
one relationship.
• One-to-many − When more than one instance of an entity is
associated with a relationship, it is marked as '1:N'. The
following image reflects that only one instance of entity on
the left and more than one instance of an entity on the right
can be associated with the relationship. It depicts one-to-
many relationship.
• Many-to-one − When more than one instance of entity is
associated with the relationship, it is marked as 'N:1'. The
following image reflects that more than one instance of an
entity on the left and only one instance of an entity on the right
can be associated with the relationship. It depicts many-to-one
relationship.
• Many-to-many − The following image reflects that more
than one instance of an entity on the left and more than one
instance of an entity on the right can be associated with the
relationship. It depicts many-to-many relationship.
• Participation Constraints
• Total Participation − Each entity is involved in the
relationship. Total participation is represented by double lines.
• Partial participation − Not all entities are involved in the
relationship. Partial participation is represented by single lines.
Generalization Aggregation
• The ER Model has the power of expressing database entities in a conceptual
hierarchical manner.
As the hierarchy goes up, it generalizes the view of entities, and as we go
deep in the hierarchy, it gives us the detail of every entity included.
•
• Going up in this structure is called generalization, where entities are
clubbed together to represent a more generalized view.
• For example, a particular student named Mira can be generalized along with
all the students.
• The entity shall be a student, and further, the student is a person. The
reverse is called specialization where a person is a student, and that student
is Mira.
Generalization
• The process of generalizing entities, where the generalized entities contain
the properties of all the generalized entities, is called generalization.
• In generalization, a number of entities are brought together into one
generalized entity based on their similar characteristics. For example,
pigeon, house sparrow, crow and dove can all be generalized as Birds.
Specialization
• Specialization is the opposite of generalization. In
specialization, a group of entities is divided into
sub-groups based on their characteristics.
•
• Take a group ‘Person’ for example. A person has
name, date of birth, gender, etc. These
properties are common in all persons, human
beings. But in a company, persons can be
identified as employee, employer, customer, or
vendor, based on what role they play in the
company.
Similarly, in a school database, persons can be
specialized as teacher, student, or a staff, based
on what role they play in school as entities.
Inheritance
• We use all the above features of ER-
Model in order to create classes of
objects in object-oriented
programming.
The details of entities are generally
hidden from the user; this process
known as abstraction.
Inheritance is an important feature of
Generalization and Specialization. It
allows lower-level entities to inherit the
attributes of higher-level entities.
For example, the attributes of a Person
class such as name, age, and gender can
be inherited by lower-level entities
such as Student or Teacher.
•
•
•
ER Diagram Example
• Suppose you are given the following requirements for a simple database for the
National Hockey League (NHL):
•
•
•
•
the NHL has many teams,
each team has a name, a city, a coach, a captain, and a set of players,
each player belongs to only one team,
each player has a name, a position (such as left wing or goalie), a skill level,
and a set of injury records,
•
•
a team captain is also a player,
a game is played between two teams (referred to as host_team and guest_team)
and has a date (such as May 11th, 1999) and a score (such as 4 to 2).
• Construct a clean and concise ER diagram for the NHL database.
• Question 2:
• A university registrar’s office maintains data about the following
entities:
• courses, including number, title, credits, syllabus, and
prerequisites;
• course offerings, including course number, year, semester, section
number, instructor(s), timings, and classroom;
• students, including student-id, name, and program;
• instructors, including identi-cation number, name, department,
and title.
• Further, the enrollment of students in courses and grades
awarded to students in each course they are enrolled for must be
appropriately modeled. Construct an E-R diagram for the
registrar’s of-ce.Document all assumptions that you make about
the mapping constraints.
• Question 3:
(a)Construct an E-R diagram for a car-insurance
company whose customers own one or more cars
each. Each car has associated with it zero to any
number of recorded accidents.
(b)Construct appropriate tables for the above ER
Diagram ?
Questions …Any?
Day 3:
Introduction to MySQL
Recap
▪ Entity Relationship Model
▪ Relationships
▪ ER Diagram Representations
▪ ER Diagram Examples
Contents
▪ DIY Solutions of Day 2
▪ Installing MySQL Workbench
▪ Working with MySQL command line client
▪ Working with MySQL Workbench
• Question 2:
• A university registrar’s office maintains data about the following
entities:
• courses, including number, title, credits, syllabus, and
prerequisites;
• course offerings, including course number, year, semester, section
number, instructor(s), timings, and classroom;
• students, including student-id, name, and program;
• instructors, including identi-cation number, name, department,
and title.
• Further, the enrollment of students in courses and grades
awarded to students in each course they are enrolled for must be
appropriately modeled. Construct an E-R diagram for the
registrar’s of-ce.Document all assumptions that you make about
the mapping constraints.
• Question 3:
(a)Construct an E-R diagram for a car-insurance
company whose customers own one or more cars
each. Each car has associated with it zero to any
number of recorded accidents.
(b)Construct appropriate tables for the above ER
Diagram ?
Car insurance tables:
•person (driver-id, name, address)
•car (license, year,model)
•accident (report-number, date, location)
•participated(driver-id, license, report-number,
damage-amount)
Installing MySQL Workbench and Command Line Client
• Follow the link : https://dev.mysql.com/downloads/installer/
• Download MySQL and install.
Questions …Any?
Day 4:
Working with MySQL
Recap
▪ DIY Solutions of Day 2
▪ Installing MySQL Workbench
▪ Working with MySQL command line client
▪ Working with MySQL Workbench
Contents
▪ Working with MySQL
▪ Create Table
▪ Insert into
▪ Update Table
▪ Alter table
Executing Statements
CREATE DATABASE Statement
• The CREATE DATABASE statement is used to create a new SQL database.
Syntax
• CREATE DATABASE databasename;
CREATE DATABASE Example
• The following SQL statement creates a database called "test":
Example
• CREATE DATABASE test;
Executing Statements
CREATE TABLE Statement
The CREATE TABLE statement is used to create a new table in a database.
Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Executing Statements
MySQL CREATE TABLE Example
The following example creates a table called "Persons" that contains five columns: PersonID,
LastName, FirstName, Address, and City:
Example
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Executing Statements
MySQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
INSERT INTO Syntax
It is possible to write the INSERT INTO statement in two ways:
1. Specify both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
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:
INSERT INTO table_name VALUES (value1, value2, value3, ...);
Executing Statements
INSERT INTO Example
• The following SQL statement inserts a new record in the "Customers" table:
Example
• INSERT INTO Customers (CustomerName, ContactName, Address, City,
PostalCode, Country) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen
21', 'Stavanger', '4006', 'Norway');
Executing Statements
The MySQL UPDATE Statement
• The UPDATE statement is used to modify the existing records in a table.
UPDATE Syntax
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE
condition;
Executing Statements
UPDATE Table
The following SQL statement updates the first customer (CustomerID = 1)
with a new contact person and a new city.
Example
• UPDATE Customers
SET ContactName = 'Alfred Schmidt', City = 'Frankfurt'
WHERE CustomerID = 1;
Executing Statements
• MySQL ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.
• The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
ALTER TABLE - ADD Column
• To add a column in a table, use the following syntax:
• ALTER TABLE table_name ADD column_name datatype;
Executing Statements
• ALTER TABLE - DROP COLUMN
• To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):
• ALTER TABLE table_name
DROP COLUMN column_name;
• The following SQL deletes the "Email" column from the "Customers" table:
• Example
• ALTER TABLE Customers
DROP COLUMN Email;
Examples – Create table
• Write a SQL statement to create a simple table countries including columns
country_id, country_name and region_id.
• Write a SQL statement to create a table named jobs including columns
job_id, job_title, min_salary, max_salary and check whether the max_salary
amount exceeding the upper limit 25000.
• Write a SQL statement to create a table countries including columns
country_id, country_name and region_id and make sure that the column
country_id will be unique and store an auto incremented value.
Day 5:
Working with MySQL
Recap
▪ Working with MySQL
▪ Create Table
▪ Insert into
▪ Update Table
▪ Alter table
Contents
▪ Working with MySQL
▪ Update Table
▪ Alter table
▪ Examples on databases
Examples – Insert into
• Write a SQL statement to insert a record with your own value into the table
countries against each columns.
• Write a SQL statement to insert 3 rows by a single insert statement.
Examples – Update Table
• Write a SQL statement to change the email column of employees table with
'not available' for all employees.
• Employee(id, name, email, salary)
• Write a SQL statement to change salary of employee to 80000 whose ID is
5, if the existing salary is less than 50000.
Examples – Alter Table
Write a SQL statement to rename the table countries to country_new.
Write a SQL statement to drop the column city from the table locations.
MySQL - SELECT statement
Write a query to display the names (Name, Address) using alias name “Person
Name", “City Name“
Write a query to get unique address from person table.
Write a query to get the details of all person according to name in descending order.
Write a query to add a column salary and update all records
Write a query to get the names, salary, PF of all the employee (15% of the salary)
Day 6:
Working with MySQL
Recap
▪ Working with MySQL
▪ Update Table
▪ Alter table
▪ Examples on databases
Contents
▪ Working with MySQL
▪ Creating databases
▪ Writing Queries
MySQL Examples
MySQL Examples
Questions
Q-1. Write an SQL query to fetch “FIRST_NAME” from the
Worker table using the alias name <WORKER_NAME>.
Q-2. Write an SQL query to fetch unique values of DEPARTMENT
from the Worker table.
Q-3. Write an SQL query to print all Worker details from the
Worker table order by FIRST_NAME Ascending.
Q-4. Write an SQL query to print all Worker details from the
Worker table order by FIRST_NAME Ascending and
DEPARTMENT Descending.
Q-5 Write an SQL query to print details for Workers with the first
names “Vipul” and “Satish” from the Worker table.
Questions
Q-6 Write an SQL query to print details of workers excluding first
names, “Vipul” and “Satish” from the Worker table.
Q-7 Write an SQL query to print details of the Workers whose
SALARY lies between 100000 and 500000.
Q-8 Write an SQL query to print details of the Workers who joined
in Feb’2014.
Q-9 Write an SQL query to fetch the count of employees working in
the department ‘Admin’.
Q-10 Write an SQL query to fetch worker names with salaries >=
50000 and <= 100000.
Questions
Q-11 Write an SQL query to fetch the no. of workers for each
department in descending order.
Q-12 Write an SQL query to show the top n (say 10) records of a
table.
Questions …Any?
SQL Day 1 Guide: DBMS, SQL Basics & Database Operations

More Related Content

Similar to SQL Day 1 Guide: DBMS, SQL Basics & Database Operations (20)

E_R-Diagram (2).pptx
E_R-Diagram (2).pptxE_R-Diagram (2).pptx
E_R-Diagram (2).pptx
 
ER Model in DBMS
ER Model in DBMSER Model in DBMS
ER Model in DBMS
 
18306_lec-2 (1).ppt
18306_lec-2 (1).ppt18306_lec-2 (1).ppt
18306_lec-2 (1).ppt
 
Database design
Database designDatabase design
Database design
 
ERD(2).ppt
ERD(2).pptERD(2).ppt
ERD(2).ppt
 
Unit iv dbms
Unit   iv dbmsUnit   iv dbms
Unit iv dbms
 
10287 lecture5(2)
10287 lecture5(2)10287 lecture5(2)
10287 lecture5(2)
 
ER-Diagram.pptx
ER-Diagram.pptxER-Diagram.pptx
ER-Diagram.pptx
 
RDBMS ERD
RDBMS ERDRDBMS ERD
RDBMS ERD
 
ER Modeling and Introduction to RDBMS
ER Modeling and Introduction to RDBMSER Modeling and Introduction to RDBMS
ER Modeling and Introduction to RDBMS
 
Er model
Er modelEr model
Er model
 
Data model and entity relationship
Data model and entity relationshipData model and entity relationship
Data model and entity relationship
 
Data Models
Data ModelsData Models
Data Models
 
rdbmsdol.pptx
rdbmsdol.pptxrdbmsdol.pptx
rdbmsdol.pptx
 
rdbmsdol.pptx
rdbmsdol.pptxrdbmsdol.pptx
rdbmsdol.pptx
 
Entity Relationship Diagram – ER Diagram in DBMS.pptx
Entity Relationship Diagram – ER Diagram in DBMS.pptxEntity Relationship Diagram – ER Diagram in DBMS.pptx
Entity Relationship Diagram – ER Diagram in DBMS.pptx
 
Database part3-
Database part3-Database part3-
Database part3-
 
ER MODEL.pptx
ER MODEL.pptxER MODEL.pptx
ER MODEL.pptx
 
Entity Relationship Model
Entity Relationship ModelEntity Relationship Model
Entity Relationship Model
 
Data model
Data modelData model
Data model
 

Recently uploaded

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 

Recently uploaded (20)

(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 

SQL Day 1 Guide: DBMS, SQL Basics & Database Operations

  • 2. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved. Contents ▪ Database Management System (DBMS) - Definition and importance ▪ Introduction to Structured Query Language (SQL)
  • 3. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
  • 4. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
  • 5. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
  • 6. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
  • 7. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
  • 8. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
  • 9. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
  • 10. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
  • 11. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved.
  • 12. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved. SQL: Structured Query Language ▪ SQL (Structured Query Language): Standard language for interacting with relational databases ▪ Key characteristics: ▪ Declarative language ▪ Uses English-like syntax ▪ Supports data definition, manipulation, and control
  • 13. © Brain4ce Education Solutions Pvt. Ltd. All rights reserved. SQL: Basics ▪ SQL Statements: ▪ Data Definition Language (DDL): CREATE, ALTER, DROP ▪ Data Manipulation Language (DML): SELECT, INSERT, UPDATE, DELETE ▪ Data Control Language (DCL): GRANT, REVOKE ▪ SQL Queries: ▪ SELECT statement: Retrieves data from a database table ▪ Example: SELECT * FROM Customers WHERE Country = 'USA';
  • 14. SQL: SQL Database Operations Creating Database and Tables: • CREATE DATABASE database_name; • CREATE TABLE table_name (column1 datatype, column2 datatype, ...); Inserting Data: • INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); Updating Data: • UPDATE table_name SET column_name = new_value WHERE condition; Deleting Data: • DELETE FROM table_name WHERE condition;
  • 15. SQL: SQL Database Operations Joins: Combine rows from multiple tables based on related columns Types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN Aggregation Functions: Perform calculations on groups of rows Examples: SUM, AVG, COUNT, MAX, MIN Constraints: Rules applied to columns to enforce data integrity Examples: PRIMARY KEY, FOREIGN KEY, NOT NULL
  • 16. Summary • Database Management System (DBMS) is essential for efficient data management. • SQL is a powerful language for interacting with relational databases. • Understanding DBMS and SQL enables effective data storage, retrieval, and manipulation.
  • 18.
  • 20. Precap ▪ Database Management System (DBMS) - Definition and importance ▪ Introduction to Structured Query Language (SQL)
  • 21. Contents ▪ Entity Relationship Model ▪ Relationships ▪ ER Diagram Representations ▪ ER Diagram Examples
  • 22. Entity Relationship Model • An Entity Relationship Diagram (ERD) is a visual representation of different data using conventions that describe how these data are related to each other. For example, the elements writer, novel, and consumer may be described using ER diagrams this way: • The elements inside rectangles are called entities while the items inside diamonds denote the relationships between entities.
  • 23. Entity • An entity can be a real-world object, either animate or inanimate, that can be easily identifiable. – For example, in a school database, students, teachers, classes, and courses offered can be considered as entities. – All these entities have some attributes or properties that give them their identity. An entity set is a collection of similar types of entities. – An entity set may contain entities with attribute sharing similar values. – For example, – a Students set may contain all the students of a school; – likewise a Teachers set may contain all the teachers of a school from all faculties. Entity sets need not be disjoint. •
  • 24. Weak Entity • A weak entity is an entity that depends on the existence of another entity. In more technical terms it can defined as an entity that cannot be identified by its own attributes. It uses a foreign key combined with its attributed to form the primary key. An entity like order item is a good example for this. – The order item will be meaningless without an order so it depends on the existence of order. • • •
  • 25. Attributes • Entities are represented by means of their properties, called attributes. • All attributes have values. • For example, a student entity may have name, class, and age as attributes. • There exists a domain or range of values that can be assigned to attributes. • For example, a student's name cannot be a numeric value. It has to be alphabetic. A student's age cannot be negative, etc.
  • 26. Types of Attributes • Simple attribute − Simple attributes are atomic values, which cannot be divided further. For example, a student's phone number is an atomic value of 10 digits. • Composite attribute − Composite attributes are made of more than one simple attribute. For example, a student's complete name may have first_name and last_name.
  • 27. Types of Attributes (contd.,) • Derived attribute − Derived attributes are the attributes that do not exist in the physical database, but their values are derived from other attributes present in the database. • For example, average_salary in a department should not be saved directly in the database, instead it can be derived. • • For another example, age can be derived from data_of_birth. For example for a circle the area can be derived from the radius.
  • 28. Types of Attributes (contd.,) • Single-value attribute − Single-value attributes contain single value. For example − Social_Security_Number. • Multi-value attribute − Multi-value attributes may contain more than one values. For example, a person can have more than one phone number, email_address, etc. • For example a teacher entity can have multiple subject values.
  • 29. • These attribute types can come together in a way like − • simple single-valued attributes • simple multi-valued attributes • composite single-valued attributes • composite multi-valued attributes
  • 30. Entity-Set and Keys • Key is an attribute or collection of attributes that uniquely identifies an entity among entity set. For example, the roll_number of a student makes him/her identifiable among students. Super Key − A set of attributes (one or more) that collectively identifies an entity in an entity set. • • • Candidate Key − A minimal super key is called a candidate key. An entity set may have more than one candidate key. • Primary Key − A primary key is one of the candidate keys chosen by the database designer to uniquely identify the entity set.
  • 31. Relationship • • The association among entities is called a relationship. For example, an employee works_at a department, a student enrolls in a course. • Here, Works_at and Enrolls are called relationships. For example, the entity “carpenter” may be related to the entity “table” by the relationship “builds” or “makes”. Relationships are represented by diamond shapes and are labeled using verbs. •
  • 32. Degree of Relationship • The number of participating entities in a relationship defines the degree of the relationship. • Binary = degree 2 • Ternary = degree 3 • n-ary = degree
  • 33. Mapping Cardinalities • Cardinality defines the number of entities in one entity set, which can be associated with the number of entities of other set via relationship set. • One-to-one − One entity from entity set A can be associated with at most one entity of entity set B and vice versa.
  • 34. • One-to-many − One entity from entity set A can be associated with more than one entities of entity set B however an entity from entity set B, can be associated with at most one entity.
  • 35. • Many-to-one − More than one entities from entity set A can be associated with at most one entity of entity set B, however an entity from entity set B can be associated with more than one entity from entity set A.
  • 36. • Many-to-many − One entity from A can be associated with more than one entity from B and vice versa.
  • 37. ER Diagram Representation • Let us now learn how the ER Model is represented by means of an ER diagram. Any object, for example, entities, attributes of an entity, relationship sets, and attributes of relationship sets, can be represented with the help of an ER diagram. •
  • 38. Entity • Entities are Rectangles represent. represented by means are named with the entity of rectangles. set they
  • 39. Attributes • Attributes are the properties of entities. Attributes are represented by means of ellipses. Every ellipse represents one attribute and is directly connected to its entity (rectangle). •
  • 40. • If the attributes are composite, they are further divided in a tree like structure. Every node is then connected to its attribute. That is, composite attributes are represented by ellipses that are connected with an ellipse.
  • 41. • Multivalued attributes are depicted by double ellipse.
  • 42. • Derived attributes are depicted by dashed ellipse.
  • 43. Relationship • Relationships are represented by diamond-shaped box. Name of the relationship is written inside the diamond-box. All theentities (rectangles) participating in a relationship, are connected to it by a line. • • • Binary Relationship and Cardinality • A relationship where two entities are participating is called a binary relationship. Cardinality is the number of instance of an entity from a relation that can be associated with the relation.
  • 44. • One-to-one − When only one instance of an entity is associated with the relationship, it is marked as '1:1'. The following image reflects that only one instance of each entity should be associated with the relationship. It depicts one-to- one relationship.
  • 45. • One-to-many − When more than one instance of an entity is associated with a relationship, it is marked as '1:N'. The following image reflects that only one instance of entity on the left and more than one instance of an entity on the right can be associated with the relationship. It depicts one-to- many relationship.
  • 46. • Many-to-one − When more than one instance of entity is associated with the relationship, it is marked as 'N:1'. The following image reflects that more than one instance of an entity on the left and only one instance of an entity on the right can be associated with the relationship. It depicts many-to-one relationship.
  • 47. • Many-to-many − The following image reflects that more than one instance of an entity on the left and more than one instance of an entity on the right can be associated with the relationship. It depicts many-to-many relationship.
  • 48. • Participation Constraints • Total Participation − Each entity is involved in the relationship. Total participation is represented by double lines. • Partial participation − Not all entities are involved in the relationship. Partial participation is represented by single lines.
  • 49. Generalization Aggregation • The ER Model has the power of expressing database entities in a conceptual hierarchical manner. As the hierarchy goes up, it generalizes the view of entities, and as we go deep in the hierarchy, it gives us the detail of every entity included. • • Going up in this structure is called generalization, where entities are clubbed together to represent a more generalized view. • For example, a particular student named Mira can be generalized along with all the students. • The entity shall be a student, and further, the student is a person. The reverse is called specialization where a person is a student, and that student is Mira.
  • 50. Generalization • The process of generalizing entities, where the generalized entities contain the properties of all the generalized entities, is called generalization. • In generalization, a number of entities are brought together into one generalized entity based on their similar characteristics. For example, pigeon, house sparrow, crow and dove can all be generalized as Birds.
  • 51. Specialization • Specialization is the opposite of generalization. In specialization, a group of entities is divided into sub-groups based on their characteristics. • • Take a group ‘Person’ for example. A person has name, date of birth, gender, etc. These properties are common in all persons, human beings. But in a company, persons can be identified as employee, employer, customer, or vendor, based on what role they play in the company. Similarly, in a school database, persons can be specialized as teacher, student, or a staff, based on what role they play in school as entities.
  • 52. Inheritance • We use all the above features of ER- Model in order to create classes of objects in object-oriented programming. The details of entities are generally hidden from the user; this process known as abstraction. Inheritance is an important feature of Generalization and Specialization. It allows lower-level entities to inherit the attributes of higher-level entities. For example, the attributes of a Person class such as name, age, and gender can be inherited by lower-level entities such as Student or Teacher. • • •
  • 53. ER Diagram Example • Suppose you are given the following requirements for a simple database for the National Hockey League (NHL): • • • • the NHL has many teams, each team has a name, a city, a coach, a captain, and a set of players, each player belongs to only one team, each player has a name, a position (such as left wing or goalie), a skill level, and a set of injury records, • • a team captain is also a player, a game is played between two teams (referred to as host_team and guest_team) and has a date (such as May 11th, 1999) and a score (such as 4 to 2). • Construct a clean and concise ER diagram for the NHL database.
  • 54.
  • 55. • Question 2: • A university registrar’s office maintains data about the following entities: • courses, including number, title, credits, syllabus, and prerequisites; • course offerings, including course number, year, semester, section number, instructor(s), timings, and classroom; • students, including student-id, name, and program; • instructors, including identi-cation number, name, department, and title. • Further, the enrollment of students in courses and grades awarded to students in each course they are enrolled for must be appropriately modeled. Construct an E-R diagram for the registrar’s of-ce.Document all assumptions that you make about the mapping constraints.
  • 56. • Question 3: (a)Construct an E-R diagram for a car-insurance company whose customers own one or more cars each. Each car has associated with it zero to any number of recorded accidents. (b)Construct appropriate tables for the above ER Diagram ?
  • 58.
  • 60. Recap ▪ Entity Relationship Model ▪ Relationships ▪ ER Diagram Representations ▪ ER Diagram Examples
  • 61. Contents ▪ DIY Solutions of Day 2 ▪ Installing MySQL Workbench ▪ Working with MySQL command line client ▪ Working with MySQL Workbench
  • 62. • Question 2: • A university registrar’s office maintains data about the following entities: • courses, including number, title, credits, syllabus, and prerequisites; • course offerings, including course number, year, semester, section number, instructor(s), timings, and classroom; • students, including student-id, name, and program; • instructors, including identi-cation number, name, department, and title. • Further, the enrollment of students in courses and grades awarded to students in each course they are enrolled for must be appropriately modeled. Construct an E-R diagram for the registrar’s of-ce.Document all assumptions that you make about the mapping constraints.
  • 63.
  • 64. • Question 3: (a)Construct an E-R diagram for a car-insurance company whose customers own one or more cars each. Each car has associated with it zero to any number of recorded accidents. (b)Construct appropriate tables for the above ER Diagram ?
  • 65. Car insurance tables: •person (driver-id, name, address) •car (license, year,model) •accident (report-number, date, location) •participated(driver-id, license, report-number, damage-amount)
  • 66.
  • 67. Installing MySQL Workbench and Command Line Client • Follow the link : https://dev.mysql.com/downloads/installer/ • Download MySQL and install.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 76.
  • 78. Recap ▪ DIY Solutions of Day 2 ▪ Installing MySQL Workbench ▪ Working with MySQL command line client ▪ Working with MySQL Workbench
  • 79. Contents ▪ Working with MySQL ▪ Create Table ▪ Insert into ▪ Update Table ▪ Alter table
  • 80. Executing Statements CREATE DATABASE Statement • The CREATE DATABASE statement is used to create a new SQL database. Syntax • CREATE DATABASE databasename; CREATE DATABASE Example • The following SQL statement creates a database called "test": Example • CREATE DATABASE test;
  • 81. Executing Statements CREATE TABLE Statement The CREATE TABLE statement is used to create a new table in a database. Syntax CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... );
  • 82. Executing Statements MySQL CREATE TABLE Example The following example creates a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City: Example CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) );
  • 83. Executing Statements MySQL INSERT INTO Statement The INSERT INTO statement is used to insert new records in a table. INSERT INTO Syntax It is possible to write the INSERT INTO statement in two ways: 1. Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); 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: INSERT INTO table_name VALUES (value1, value2, value3, ...);
  • 84. Executing Statements INSERT INTO Example • The following SQL statement inserts a new record in the "Customers" table: Example • INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway');
  • 85. Executing Statements The MySQL UPDATE Statement • The UPDATE statement is used to modify the existing records in a table. UPDATE Syntax UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
  • 86. Executing Statements UPDATE Table The following SQL statement updates the first customer (CustomerID = 1) with a new contact person and a new city. Example • UPDATE Customers SET ContactName = 'Alfred Schmidt', City = 'Frankfurt' WHERE CustomerID = 1;
  • 87. Executing Statements • MySQL ALTER TABLE Statement The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. • The ALTER TABLE statement is also used to add and drop various constraints on an existing table. ALTER TABLE - ADD Column • To add a column in a table, use the following syntax: • ALTER TABLE table_name ADD column_name datatype;
  • 88. Executing Statements • ALTER TABLE - DROP COLUMN • To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column): • ALTER TABLE table_name DROP COLUMN column_name; • The following SQL deletes the "Email" column from the "Customers" table: • Example • ALTER TABLE Customers DROP COLUMN Email;
  • 89. Examples – Create table • Write a SQL statement to create a simple table countries including columns country_id, country_name and region_id. • Write a SQL statement to create a table named jobs including columns job_id, job_title, min_salary, max_salary and check whether the max_salary amount exceeding the upper limit 25000. • Write a SQL statement to create a table countries including columns country_id, country_name and region_id and make sure that the column country_id will be unique and store an auto incremented value.
  • 91. Recap ▪ Working with MySQL ▪ Create Table ▪ Insert into ▪ Update Table ▪ Alter table
  • 92. Contents ▪ Working with MySQL ▪ Update Table ▪ Alter table ▪ Examples on databases
  • 93. Examples – Insert into • Write a SQL statement to insert a record with your own value into the table countries against each columns. • Write a SQL statement to insert 3 rows by a single insert statement.
  • 94. Examples – Update Table • Write a SQL statement to change the email column of employees table with 'not available' for all employees. • Employee(id, name, email, salary) • Write a SQL statement to change salary of employee to 80000 whose ID is 5, if the existing salary is less than 50000.
  • 95. Examples – Alter Table Write a SQL statement to rename the table countries to country_new. Write a SQL statement to drop the column city from the table locations.
  • 96. MySQL - SELECT statement Write a query to display the names (Name, Address) using alias name “Person Name", “City Name“ Write a query to get unique address from person table. Write a query to get the details of all person according to name in descending order. Write a query to add a column salary and update all records Write a query to get the names, salary, PF of all the employee (15% of the salary)
  • 98. Recap ▪ Working with MySQL ▪ Update Table ▪ Alter table ▪ Examples on databases
  • 99. Contents ▪ Working with MySQL ▪ Creating databases ▪ Writing Queries
  • 102. Questions Q-1. Write an SQL query to fetch “FIRST_NAME” from the Worker table using the alias name <WORKER_NAME>. Q-2. Write an SQL query to fetch unique values of DEPARTMENT from the Worker table. Q-3. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME Ascending. Q-4. Write an SQL query to print all Worker details from the Worker table order by FIRST_NAME Ascending and DEPARTMENT Descending. Q-5 Write an SQL query to print details for Workers with the first names “Vipul” and “Satish” from the Worker table.
  • 103. Questions Q-6 Write an SQL query to print details of workers excluding first names, “Vipul” and “Satish” from the Worker table. Q-7 Write an SQL query to print details of the Workers whose SALARY lies between 100000 and 500000. Q-8 Write an SQL query to print details of the Workers who joined in Feb’2014. Q-9 Write an SQL query to fetch the count of employees working in the department ‘Admin’. Q-10 Write an SQL query to fetch worker names with salaries >= 50000 and <= 100000.
  • 104. Questions Q-11 Write an SQL query to fetch the no. of workers for each department in descending order. Q-12 Write an SQL query to show the top n (say 10) records of a table.