Implementing the SQL Queries using
MySQL - II
Session 8
Review
MySQL list of databases in the server with
the help of SHOW command
The ALTER command enables us to modify the
characteristics or attributes of a table or
a database
The
SELECT
command
is
used
for
data
retrieval
The DROP command is used to remove or delete
a table from a database. This command
removes all data from that table along with
the table definition from the database
MySQL Database / Session 8 / Slide 2 of 18
Objectives
Work with keys
Work with indexes
Manipulate tables
Control the output using ORDER BY
clause
Organize the output using the
GROUP BY command
MySQL Database / Session 8 / Slide 3 of 18
Working with Keys and Indexes - I
Indexes are used to improve the overall
performance of a database
Enables the searches on a database to be
faster
MySQL allows up to 32
indexes for each
table
Each index may consists up to 16 columns
Advantages of indexing:
Searching is faster
Sorting is faster
Indexing causes the
database to be slower
process
of
altering
MySQL Database / Session 8 / Slide 4 of 18
Working with Keys and Indexes - II
The syntax for creating an index for a
table is:
CREATE INDEX index_name ON tablename
(column1,column2,...,
To
create an index INDEX1 on columnN)
EMP_DEPARTMENT, enter the
following at the command prompt:
CREATE INDEX INDEX1 ON
EMP_DEPARTMENT(DATE_OF_JOI
N);
MySQL Database / Session 8 / Slide 5 of 18
Defining Keys - I
Keys are columns that helps to
identify a record in a table
Primary key is a unique identifier in
a table
Foreign key is a column in a table is
a primary key in a different table
In order to define a foreign key both
the table must of the type InnoDB
MySQL Database / Session 8 / Slide 6 of 18
Defining Keys - II
To add the foreign key, enter the
following at the command prompt:
ALTER TABLE EMP_SALARY ADD FOREIGN KEY
(E_ID) REFERENCES EMP_DETAILS;
MySQL Database / Session 8 / Slide 7 of 18
Manipulating the Tables
INSERT command is used to data in a
table
The command for the INSERT command
is:
INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name [(col_name,...)]
VALUES
({expression | DEFAULT},...),(...),... [ON
DUPLICATE KEY UPDATE col_name = expression
,...]
MySQL Database / Session 8 / Slide 8 of 18
INSERT Command
To insert rows in EMP_DEPARTMENT, enter
the following at the command prompt:
INSERT INTO EMP_DEPARTMENT VALUES
(101,’MARKETING’,’2000-05-15’,A01’);
MySQL Database / Session 8 / Slide 9 of 18
UPDATE Command
The syntax for updating a table is:
UPDATE [LOW_PRIORITY] [IGNORE] table SET
column=value,...[WHERE clause][LIMIT n]
To update a row in
EMP_DETAILS table,
enter the following at
the command prompt:
UPDATE EMP_DETAILS SET
EMAIL_ID=’peter102@cbase.
com’WHERE E_ID=102;
MySQL Database / Session 8 / Slide 10 of 18
REPLACE Command
The syntax for deleting from a table is:
REPLACE [DELAYED|LOW_PRIORITY] INTO table
[(column,…)] VALUES (value,...)
To update a row in EMP_DETAILS
table, enter the following at the
command prompt:
REPLACE INTO EMP_DETAILS
VALUES
(106,’GEORGE’,’BLAIR’,’CALIF
ORNIA’,
56474,’);
MySQL Database / Session 8 / Slide 11 of 18
DELETE Command
DELETE command is used to delete a row from a table
Syntax for deleting from a table is:
DELETE [LOW_PRIORITY|QUICK] FROM table
[WHERE clause] [ORDER BY column,...]
[LIMIT n]
To delete a row from the
EMP_DETAILS table, enter
the following at the
command prompt:
DELETE FROM EMP_DETAILS
WHERE E_ID=106;
MySQL Database / Session 8 / Slide 12 of 18
Using the ORDER BY Clause – I
ORDER BY Clause is used to sort the
table
The syntax for 0RDER BY is:
ORDER BY column [ASC|DESC]
[,column2[ASC|DESC],...]
If the ASC or DESC option is not
specified then by default the output
is sorted in the ascending order
MySQL Database / Session 8 / Slide 13 of 18
Using the ORDER BY Clause – II
To order the output
DATE_OF _JOIN field
in the ascending
order, enter the
following at the
command prompt:
SELECT * FROM
EMP_DEPARTMENT
ORDER BY
DATE_OF_JOIN ASC;
MySQL Database / Session 8 / Slide 14 of 18
Using the GROUP BY Command - I
Grouping facilitates the user to group
rows with similar values for a
specific column into a single row in
order to operate on them together
The syntax for ordering is:
GROUP BY column [,column2...]
MySQL Database / Session 8 / Slide 15 of 18
Using the GROUP BY Command - II
To group the output
of EMP_DEPARTMENT
table on the
DESIGNATION field,
enter the following
at the command
prompt:
SELECT * FROM
EMP_DEPARTMENT GROUP
BY DESIGNATION;
MySQL Database / Session 8 / Slide 16 of 18
Summary - I
Keys uniquely identify a record in a table
Primary key is a unique identifier in a
table
Foreign key is a primary key in a
different table
We can define a primary key as well as
foreign key while creating a table
We also define these keys using the ALTER
TABLE command
INSERT command enables to add data in a
table
MySQL Database / Session 8 / Slide 17 of 18
Summary - II
UPADTE command is used to modify data in
a table
REPALCE command is used to change data in
a table
DELETE command enables user to delete the
data from a table
ORBER BY option is used with the select
command to order the output
Grouping facilitates to group rows with
similar values for a specific column into
a single row
MySQL Database / Session 8 / Slide 18 of 18