Chapter - 8 MySQL - REVISION TOUR 1. Database: A database is an organised collection of data. 2. DBMS : Software used to manage database is called Data Base Management System. 3. Relational Database: A database in which the data is stored in the form of relations (also called tables) is called a Relational Database. In other words a Relational Database is a collection of one or more tables. 3. RDBMS: A DBMS used to manage Relational Databases is called an RDBMS (Relational Data Base Management System). Some popular RDBMS software available are: Oracle, MySQL. 4. Benefits of using a DBMS are: a. Redundancy can be controlled b. Inconsistence can be avoided c. Data can be shared d. Security restrictions can be applied. 5. MySQL: It is an Open Source RDBMS Software. It is available free of cost. 6. Relation/Table: A table refers to a two dimensional representation of data arranged in columns and rows. 7. Primary Key: The group of one or more columns used to uniquely identify each row of a relation is called its Primary Key. 8. Candidate Key: A Candidate Key is the one that is capable of becoming Primary key i.e., a field or attribute that has unique value for each row in the relation. 9. Alternate Key: A candidate key that is not primary key is called alternate key. 10. Foreign Key: A non-key attribute, whose values are derived from the primary key of some other table. 11. Degree : Number of columns in the table. 12. Cardinality: Number of rows in a table is called its cardinality.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Chapter - 8
MySQL - REVISION TOUR
1. Database: A database is an organised collection of data.
2. DBMS : Software used to manage database is called Data Base
Management System.
3. Relational Database: A database in which the data is stored in the form
of relations (also called tables) is called a Relational Database. In other
words a Relational Database is a collection of one or more tables.
3. RDBMS: A DBMS used to manage Relational Databases is called an
RDBMS (Relational Data Base Management System). Some popular
RDBMS software available are: Oracle, MySQL.
4. Benefits of using a DBMS are:
a. Redundancy can be controlled
b. Inconsistence can be avoided
c. Data can be shared
d. Security restrictions can be applied.
5. MySQL: It is an Open Source RDBMS Software. It is available free of
cost.
6. Relation/Table: A table refers to a two dimensional representation of
data arranged in columns and rows.
7. Primary Key: The group of one or more columns used to uniquely
identify each row of a relation is called its Primary Key.
8. Candidate Key: A Candidate Key is the one that is capable of becoming
Primary key i.e., a field or attribute that has unique value for each row in
the relation.
9. Alternate Key: A candidate key that is not primary key is called
alternate key.
10. Foreign Key: A non-key attribute, whose values are derived from the
primary key of some other table.
11. Degree : Number of columns in the table.
12. Cardinality: Number of rows in a table is called its cardinality.
13. Tuple: A row of a relation.
14. Attribute: A column of relation.
Working with MySQL
SQL (Structured Query Language): It is the language used to
manipulate and manage databases and tables within them using an RDBMS.
1. DDL (Data Definition Language): All the commands which are used to
create, destroy, or restructure databases and tables come under this
category. Examples - CREATE, DROP, ALTER.
2. DML (Data Manipulation Language): All the commands which are
used to manipulate data within tables come under this category. Examples -
INSERT, UPDATE, DELETE.
3. DCL (Data Control Language): All the commands which are used to
control the access to databases and tables fall under this category.
Examples - GRANT, REVOKE.
1. Command: SELECT DATABASE()
Purpose: Shows the name of the current database
2. Command: SHOW TABLES
Purpose: Shows a list of tables present in the current database.
3. Command: ALTER TABLE
Purpose: Modifies the structure of a table
Syntax: (i) ALTER TABLE <tablename>
ADD <columnname> <datatype>;
(ii) ALTER TABLE <tablename>
DROP <columnname>;
(iii) ALTER TABLE <tablename>
MODIFY <column> <new_definition>;
4. Command : USE
Syntax : USE <databasename>;
Purpose : Open the specified database for use.
5. Command : SHOW TABLES
Purpose : Show a list of tables present in the current database.
6. Eliminating duplicate data – DISTINCT keyword eliminates
duplicate rows form the result of a SELECT command.
(i) Display name of all department (non-redundant)
mysql>Select DISTINCT dept from emp;
7. Command: DESCRIBE
Purpose: Shows the structure of a table.
Example : DESC emp;
Example : DESCRIBE emp;
8. Command: UPDATE
Purpose: Updates/Modifies data in a table
(i) Update emp
SET basic = 16000;
(ii) Update emp
SET basic = 16000
Where emono = 5623;
9. Command: DELETE
Purpose: Deletes data from a table
(i) DELETE FROM EMP ;
(II) DELETE FROM EMP
WHERE EMPNO = 4125;
10 . Performing simple calculations
(i) To calculate 2*8*5
mysql> SELECT 2*8*5;
(ii) To calculate 10% DA of basic
mysql > SELECT basic*10/100 from emp;
11. Using Column Aliases
Example : Select empno As ‘Employee Number’ From emp;
12. Condition based on a Range – The MySQL BETWEEN Condition is
used to retrieve values within a range in a SELECT, UPDATE, or DELETE
statement.
Syntax :
expression BETWEEN value1 AND value2;
Range includes both values.
Example :
(i) Select * from employee
Where basic BETWEEN 15000 AND 20000;
(ii) SELECT rollno, name FROM student
WHERE rollno BETWEEN 10 AND 20;
11. Condition based on a List – The IN operator used to select values
that match any value in a list of Specified values.
Example :
(i) Select ename, basic from emp
Where dept IN ( ‘Accounts’, ‘Sales’, Purchase’);
(ii) SELECT rollno, name FROM student WHERE rollno IN (10, 20, 60);
The NOT IN operator finds rows that do not match in the list
(i) Select ename, basic from emp
Where dept NOT IN ( ‘Accounts’, ‘Sales’, Purchase’);
12. Condition based on Pattern Matches – Keyword LIKE is used for
pattern matching of string data using wildcard characters % and _ .
Percent ( % ) – Matches any substring.
Underscore ( _ ) – to match on a single character.
Q- (To display name of those employee whose name are 4 characters long)
(iii) Select ename, basic from employee
Where ename LIKE ‘_ _ _ _’;
(iv) Select ename, basic from employee
Where ename LIKE ‘_J%’;
Q- To Display name of those employees where second last character is “r”
(v) Select ename, basic from employee
Where ename LIKE ‘%r_’;
(vi) Select * from contact
Where mobile LIKE ‘8%’;
13. Searching for NULL
Keyword IS NULL is used to select rows in which the specified column
is NULL.
(i) Select empno, ename, design
From emp
Where Design IS NULL;
(ii) Select empno, ename, design
From emp
Where Design IS NOT NULL;
(Relational operator can’t be used with NULL)
14. Sorting Results – ORDER BY Clause - Used to arrange the selected
rows in ascending or in descending order.
Example
1. To arrange/sort the list of employees in the alphabetical order of their
names.
SELECT * FROM emp;
ORDER BY ename;
2. SELECT * FROM student
WHERE marks>50
ORDER BY name;
Functions in MySQL
Numeric Functions:
1. POWER(x,y) OR POW (x,y) - Returns the value of x raised to the power of y.
Example :
(i) mysql> SELECT POWER (3,2) "RAISED";
RAISED
9
2. ROUND(x) Rounds the argument x to the nearest INTEGER.
Example
(i) mysql> SELECT ROUND(180.55);
ROUND(180.55)
181
3. ROUND(x,d) Rounds the argument x to d decimal places.
mysql> SELECT ROUND (15.193,1)
ROUND (15.193,1)
15.2
4. TRUNCATE(x,d) Truncates the argument x to d decimal places.
mysql> select truncate(150.292,1);
truncate(150.292,1)
150.2
String Functions: LENGTH(str) Returns the length of a column or a string in bytes.
2. CONCAT(str1,str2,...) Returns the string that results from
concatenating the arguments. May have one or more arguments.
3. INSTR(str,substr) Returns the position of the first occurrence of substring <substr> in the string <str>.
4. LOWER(STR) OR LCASE(STR) - Returns the argument <str> in
lowercase. i.e., It changes all the characters of the passed string to
lowercase.
5. UPPER(STR) OR UCASE(STR) - Returns the argument <str> in
uppercase. i.e., It changes all the characters of the passed string to
uppercase.
6. LEFT (STR,N) - Returns the first <n> characters from the string <str> 7. RIGHT(STR,N) - Returns the last <n> characters from the string <str>
8. LTRIM(str) Removes leading spaces, i.e., removes spaces from the left side of the string <str>.
9. RTRIM(str) Removes trailing spaces, i.e., removes spaces from the right side of the string <str>.
10. TRIM(str) Removes both leading and trailing spaces from the string str.
With Leading Space
11. SUBSTRING(str,m,n) or SUBSTR(str, m, n) or MID(str,m,n) –
12. ASCII(str) Returns the ASCII value of the first character of the string
<str>. Returns 0 if <str> is the empty string. Returns NULL if <str> is
NULL.
Date and Time Functions 1. CURDATE() - Returns the current date in YYYY-MM-DD format.
2. NOW() - Returns the current date and time in 'YYYY-MM-DD HH:MM:SS'.
3. SYSDATE() - Returns the current date and time in 'YYYY-MM-DD HH:MM:SS'. 4. DATE(expr) - Extracts the date part of a date or datetime expression.
5. MONTH(date) - Returns the numeric month from the date passed.
6. YEAR(date) - Returns the year for date passed.
7. DAYNAME (date) - It returns the name of the weekday for the specified date.
8. DAYOFMONTH(date) - Returns the day of the month in the range 0 to 31. 9. DAYOFWEEK (date) - Returns the day of week in number as 1 for Sunday, 2 for Monday and so on.
10. DAYOFYEAR (date) - Return the day of the year for the given date in
numeric format in the range 1 to 366.
11. Difference between NOW() and SYSDATE()
NOW()- function returns a constant date and time at which the statement started executing.
Sleep(argument) - pauses for the number of seconds given by the
argument.
Sysadate () - to get exact time at which the statement executes.
Q1. If a database “Employee” exists, which MySql command helps you to
start working in that database?
Q2. Sahil created a table in MySql. Later on, he found that there should
have been another column in the table. Which command should he use
to add another column to the table?
Q3. Pooja a student of class XI , created a table “Book” . Price is a column
of this table. To find the details of books whose prices have not been
entered she wrote the following query :
Select * from Books where Price = NULL;
Help Pooja to run the query by removing the errors from the query and
rewriting it.
Q4. Rama is not able to change a value in a column to NULL. What
constraints did she specify when she created the table?
Q5. Distinguish between a Primary key and candidate key with the help of
suitable example of each.
Q6. The LastName column of a table “Directory” is given below :
Last Name
Batra
Sehgal
Bhatia
Sharma
Mehta
Based on this information, find the output of the following quries :
(i) SELECT lastname FROM Directory WHERE lastname like ‘_a%’ ;
(ii) SELECT lastname FROM Directory WHERE lastname not like ‘%a’;
Q7. A table “Stock” in a database has 5 columns and contains 17 records.
What is the degree and cardinality of this table?
Ans5. Candidate key is a column or a group of columns that is
capable of becoming the primary key. A table can have multiple
candidates key but it can have only one primary key.
For example , a table STUDENT contains columns AdmNo, RollNo,
Address, PhoneNo. In the table AdmNo and RollNo (both are
unique for every row in the table.) are candidate keys. Out of
these, any one can be selected as the primary key of the table.
Q8. Explain the purpose of DDL and DML commands used in SQl.
Also give two examples of each.
Ans: DDL (Data Definition Language) – DDL commands are
used to create , destroy and to restructure the database objects.
For example , CREATE, ALTER
DML- DML commands are used to insert , delete, and change data
in the tables.
For Example SELECT, DELETE.
Q9. Write the output of the following SQL queries :
(i) SELECT ROUND(6.5675, 2);
(ii) SELECT TRUNCATE(5.3456,1);
(iii) SELECT DAYOFMONTH(‘2009-08-25);
(iv) SELECT MID(‘Class 12, 2, 3);
Q10. Write an SQL query to create the ‘Menu’ with the following structure.