MySQL Database / Session 8 / Slide 2 of 18 MySQL list of databases in the server with the help of SHOW command The ALTER command enables us to modify the characteristics or attribute
Trang 1Implementing the SQL Queries using
MySQL - II
Session 8
Trang 2MySQL Database / Session 8 / Slide 2 of 18
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
Review
Trang 3 Work with keys
Work with indexes
Manipulate tables
Control the output using ORDER BY
clause
Organize the output using the
GROUP BY command
Trang 4MySQL Database / Session 8 / Slide 4 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 process of altering database to be slower
Trang 5Working with Keys and Indexes - II
The syntax for creating an index for a table is:
CREATE INDEX index_name ON tablename
(column1,column2, , columnN)
To create an index INDEX1 on
EMP_DEPARTMENT, enter the
following at the command prompt:
CREATE INDEX INDEX1 ON
EMP_DEPARTMENT(DATE_OF_JOI
Trang 6MySQL Database / Session 8 / Slide 6 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
Trang 7Defining 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;
Trang 8MySQL Database / Session 8 / Slide 8 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
, ]
Trang 9INSERT 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’);
Trang 10MySQL Database / Session 8 / Slide 10 of 18
UPDATE Command
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;
The syntax for updating a table is:
UPDATE [LOW_PRIORITY] [IGNORE] table SET
column=value, [WHERE clause][LIMIT n]
Trang 11REPLACE 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
Trang 12MySQL Database / Session 8 / Slide 12 of 18
DELETE Command
To delete a row from the
EMP_DETAILS table, enter
the following at the
command prompt:
DELETE FROM EMP_DETAILS
WHERE E_ID=106;
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]
Trang 13Using 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
Trang 14MySQL Database / Session 8 / Slide 14 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;
Trang 15Using 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 ]
Trang 16MySQL Database / Session 8 / Slide 16 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;
Trang 17Summary - 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
Trang 18MySQL Database / Session 8 / Slide 18 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