Class - Xii-Mysql and It Application Notes
Class - Xii-Mysql and It Application Notes
It is freely available open source Relational Database Management System (RDBMS) that uses Structured
Query Language(SQL). In MySQL database , information is stored in Tables. A single MySQL database can contain
many tables at once and store thousands of individual records.
1. Relational data model : In this data model, the data is organized into tables (i.e. rows and columns). These
tables are called relations.
CLASSIFICATION OF SQL
STATEMENTS
SQL commands can be mainly divided into following
categories:
1. Data Definition Language(DDL)
Commands Commands that allow you to perform task, related to data definition
e.g; Creating, altering and dropping.
Granting and revoking privileges and
roles.
Maintenance commands.
2. Data Manipulation Language(DML)
Commands
Commands that allow you to perform data manipulation e.g., retrieval, insertion, deletion and
modification of data stored in a database.
DATA TYPES
Data types are means to identify the type of data and associated operations for handling it. MySQL data
types are divided into three categories:
Numeric
Date and
time
String types
Numeric Data
Type
1. int – used for number without
decimal.
2. Decimal(m,d) – used for floating/real numbers. m denotes the total length of number and d is number of
decimal digits.
Date and Time Data
Type
1. date – used to store date in YYYY-MM-DD
format.
2. time – used to store time in HH:MM:SS
format.
String Data
Types
1. char(m) – used to store a fixed length string. m denotes max. number of
characters.
2. varchar(m) – used to store a variable length string. m denotes max. no. of
characters. BETWEEN CHAR AND VARCHAR DATA
DIFFERENCE
TYPE
S.NO. Char Datatype Varchar Datatype
1. It specifies a fixed length character It specifies a variable length character string.
String.
2. When a column is given datatype as When a column is given datatype as
CHAR( n), then MySQL ensures that VARCHAR( n), then the maximum size a value in
all values stored in that column this column can have is n bytes. Each value that
have this length i.e. n bytes. If a is stored in this column store exactly as you
value is shorter than this length n specify it i.e. no blanks are added if the length is
then blanks are added, but the size shorter than maximum length n.
of value remains
n bytes.
NULL VALUE
If a column in a row has no value, then column is said to be null , or to contain a null. You should use a null value
when the actual value is not known or when a value would not be meaningful.
DATABASE
COMMNADS
1. VIEW EXISTING
DATABASE
To view existing database names, the command is : SHOW
DATABASES ;
2. CREATING DATABASE IN
MYSQLFor creating the database in MySQL, we write the following
command : CREATE DATABASE
e.g. In order to create a database<databasename>
Student, command;
is : CREATE DATABASE Student ;
3. ACCESSING
DATABASE
For accessing already existing database , we
write : USE <databasename> ;
e.g. to access a database named Student , we write command
as : USE Student ;
4. DELETING
DATABASE
For deleting any existing database , the command
is : DROP DATABASE
<databasename>
e.g. to delete a database ; we write command
, say student,
as ; DROP DATABASE Student ;
5. VIEWING TABLE IN
DATABASE
In order to view tables present in currently accessed database , command is : SHOW
TABLES ;
CREATING TABLES IN
MYSQL
- Tables are created with the CREATE TABLE command. When a table is created, its columns are named, data
types and sizes are supplied for each column.
Syntax of CREATE TABLE command
isCREATE
: TABLE <table-
name>
( <column name> <data type> ,
<column name> <data type> ,
……… ) ;
E.g. in order to create table EMPLOYEE given
below : ECODE ENAME GENDER GRADE
GROSS
We write the following
commandCREATE: TABLE
employee
( ECODE integer ,
ENAME varchar(20) ,
GENDER char(1) ,
GRADE char(2) ,
GROSS integer );
INSERTING DATA INTO
TABLE
- The rows are added to relations(table) using INSERT command of SQL. Syntax of INSERT
is : INSERT INTO <tablename> [<column
list>]
VALUE ( <value1> ,
<value2> , …..) ;
e.g. to enter a row into EMPLOYEE table (created above), we write command
as : INSERT INTO
employee
VALUES(1001 , ‘Ravi’ , ‘M’ , ‘E4’ ,
50000);
OR
INSERT INTO employee (ECODE , ENAME , GENDER , GRADE ,
GROSS)
VALUES(1001 , ‘Ravi’ , ‘M’ , ‘E4’ ,
50000);
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
INSERTING NULL
VALUES
- To insert value NULL in a specific column, we can type NULL without quotes and NULL will be inserted in
that column. E.g. in order to insert NULL value in ENAMEcolumn of above table, we write INSERT
command as :
INSERT INTO
EMPLOYEE
VALUES (1004 , NULL , ‘M’ , ‘B2’ ,
38965 ) ;
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
1002 Akash M A1 35000
1004 NULL M B2 38965
e.g.
In order to retrieve everything from Employee table, we write SELECT command
as : EMPLOYEE
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
1002 Akash M A1 35000
1004 NULL M B2 38965
SELECTING PARTICULAR
ROWS
We can select particular rows from a table by specifying a condition through WHERE clause along with
SELECT statement. E.g. In employee table if we want to select rows where Gender is female, then
command is : SELECT * FROM
EMPLOYEE
WHERE GENDER = ‘F’ ;
E.g.2. in order to select rows where salary is greater than 48000, then command
is : SELECT * FROM
EMPLOYEE
WHERE GROSS > 48000 ;
ELIMINATING REDUNDANT
DATA
The DISTINCT keyword eliminates duplicate rows from the results of a SELECT statement. For
example ,SELECT GENDER FROM
EMPLOYEE ;
GENDER
M
M
F
M
F
F
VIEWING STRUCTURE OF A
TABLE
- If we want to know the structure of a table, we can use DESCRIBE or DESC command, as per following
syntax : DESCRIBE | DESC
<tablename>;
e.g. to view the structure of table EMPLOYEE, command is : DESCRIBE EMPLOYEE ; OR DESC
EMPLOYEE ; USING COLUMN ALIASES
- The columns that we select in a query can be given a different name, i.e. column alias name for output
purpose.
Syntax :
SELECT <columnname> AS column alias , <columnname> AS column
alias
FROM …..<tablename> ;
e.g. In output, suppose we want to display ECODE column as EMPLOYEE_CODE in output , then
command isSELECT
: ECODE AS “EMPLOYEE_CODE”
FROM EMPLOYEE ;
CONDITION BASED ON A
RANGE
- The BETWEEN operator defines a range of values that the column values must fall in to make the condition true.
The range include both lower value and upper value.
e.g. to display ECODE, ENAME and GRADE of those employees whosesalary is between 40000 and 50000,
command is:
SELECT ECODE ,
ENAME ,GRADE
FROM
EMPLOYEE
WHERE GROSS BETWEEN 40000 AND
Output will50000
be : ;
CONDITION BASED ON A
LIST
- To specify a list of values, IN operator is used. The IN operator selects value that match any value in a given
list of values. E.g.
SELECT * FROM
EMPLOYEE
WHERE GRADE IN (‘A1’ , ‘A2’);
Output will be :
- The NOT IN operator finds rows that do not match in the list.
E.g. SELECT * FROM
EMPLOYEE
WHERE GRADE NOT IN (‘A1’ ,
‘A2’);
Output will be :
ENAME
Ravi
Ruby
Output will be :
Output will be :
SEARCHING FOR
NULL
- The NULL value in a column can be searched for in a table using IS NULL in the WHERE clause. E.g. to list
employee details whose salary contain NULL, we use the command :
SELECT *
FROM
EMPLOYEE
WHERE GROSS IS NULL ;
e.g.
STUDENT
Roll_No Name Marks
1 ARUN NULL
2 RAVI 56
4 SANJAY NULL
to display the names of those students whose marks is NULL, we use the
command : SELECT Name
FROM
EMPLOYEE
WHERE Marks IS NULL ;
Output will be :
Name
ARUN
SANJAY
SORTING
RESULTS
Whenever the SELECT query is executed , the resulting rows appear in a predecided order.The ORDER BY clause
allow sorting of query result. The sorting can be done either in ascending ordescendingorder, the default is
ascending.
The ORDER BY clause is used
as : SELECT <column name> , <column
name>….
FROM
<tablename>
WHERE
<condition>
ORDER BY <column name> ;
e.g. to display the details of employees in EMPLOYEE table in alphabetical order, we use
command : SELECT *
FROM
EMPLOYEE
ORDER BY ENAME ;
Output will be :
ECODE ENAME GENDER GRADE GROSS
1002 Akash M A1 35000
1004 Neela F B2 38965
1009 Neema F A2 52000
1001 Ravi M E4 50000
1006 Ruby F A1 45000
1005 Sunny M A2 30000
e.g. display list of employee in descending alphabeticalorder whose salary is greater than
40000. SELECT
ENAME
FROM
EMPLOYEE
WHERE GROSS >
40000
ORDER BY ENAME desc ;
Output will be :
ENAME
Ravi
Ruby
Neema
MODIFYING DATA IN
TABLES
you can modify data in tables using UPDATEcommand of SQL. The UPDATE command specifies the rows to be
changed using the WHERE clause, and the new data using the SET keyword. Syntax of update command is :
UPDATE
<tablename>
SET <columnname>=value ,
<columnname>=value
WHERE <condition> ;
e.g. to change the salary of employee of those in EMPLOYEE table having employee code 1009 to
55000. UPDATE
EMPLOYEE
SET GROSS =
55000
WHERE ECODE = 1009 ;
UPDATING MORE THAN ONE
COLUMNS
e.g. to update the salary to 58000 and grade to B2 for those employee whose employee code is
1001. UPDATE
EMPLOYEE
SET GROSS = 58000,
GRADE=’B2’
WHERE ECODE = 1009 ;
OTHER
EXAMPLES
e.g.1. Increase the salary of each employee by 1000 in the EMPLOYEE
table. UPDATE
EMPLOYEE
SET GROSS = GROSS +100 ;
e.g.2. Double the salary of employees having grade as ‘A1’ or
‘A2’ . UPDATE
EMPLOYEE
SET GROSS = GROSS * 2 ;
WHERE GRADE=’A1’ OR
GRADE=’A2’
e.g.3. Change the ; for those employees whose employee code is 1004 and name is
grade to ‘A2’
Neela. UPDATE
EMPLOYEE
SET GRADE=’A2’
WHERE ECODE=1004 AND
GRADE=’NEELA’ ;
DELETING DATA FROM
TABLES
To delete some data from tables, DELETE command is used. The DELETE command removes rows from a table.
The syntax of DELETE command is :
DELETE FROM
<tablename>
WHERE <condition> ;
For example, to remove the details of those employee from EMPLOYEE table whose grade is
A1. DELETE FROM
EMPLOYEE
WHERE GRADE =’A1’ ;
So if we do not specify any condition with WHERE clause, then all the rows of the table will be deleted. Thus
above line will delete all rows from employee table.
DROPPING
TABLES
The DROP TABLE command lets you drop a table from the database. The syntax of DROP TABLE
command is :DROP TABLE <tablename> ;
e.g. to drop a table employee, we need to
write : DROP TABLE employee ;
Once this command is given, the table name is no longer recognized and no more commands can be given on that
table. After this command is executed, all the data in the table along with table structure will be deleted.
However if you specify NOT NULL constraint while adding a new column, MySQL adds the new column with the
default value of that datatype e.g. for INT type it will add 0 , for CHAR types, it will add a space, and so on.
Now following commands are given for the table. Predict the table contents after each of the following
statements: (i) ALTER TABLE testt ADD col3 INT ;
(ii) ALTER TABLE testt ADD col4 INT NOT
NULL
(iii) ; ALTER TABLE testt ADD col5 CHAR(3) NOT
NULL
(iv) ; ALTER TABLE testt ADD col6
VARCHAR(3);
MODIFYING
COLUMNS
Column name and data type of column can be changed as per following
syntax :
ALTER TABLE <table
name>
CHANGE <old column name><new column name><new
If Only data type ofdatatype>;
column need to be changed ,
then
ALTER TABLE <table
name>
MODIFY <column name> <new
datatype>;
e.g.1. In table EMPLOYEE, change the column GROSS to
SALARY.
ALTER TABLE
EMPLOYEE
CHANGE GROSS SALARY
INTEGER;
e.g.2. In table EMPLOYEE , change the column ENAME to EM_NAME and data type from VARCHAR(20) to
VARCHAR(30).
ALTER TABLE
EMPLOYEE
CHANGE ENAME EM_NAME
VARCHAR(30);
e.g.3. In table EMPLOYEE , change the datatype of GRADE column from CHAR(2) to
VARCHAR(2).
ALTER TABLE
EMPLOYEE
MODIFY GRADE VARCHAR(2);
DELETING
COLUMNS
To delete a column from a table, the ALTER TABLE command takes the following
form :
ALTER TABLE <table
name>
DROP <column name>;
e.g. to delete column GRADEfrom table EMPLOYEE, we will
write : ALTER TABLE
EMPLOYEE
DROP GRADE ;
ADDING/REMOVING CONSTRAINTS TO A
TABLE
ALTER TABLE statement can be used to add constraints to your existing table by using it in following
manner:
TO ADD PRIMARY KEY
CONSTRAINT
ALTER TABLE <table
name>
ADD PRIMARY KEY (Column
name);
e.g. to add PRIMARY KEY constraint on column ECODE of table EMPLOYEE , the
command is : ALTER TABLE
EMPLOYEE
ADD PRIMARY KEY
(ECODE) ;
TO ADD FOREIGN KEY
CONSTRAINT
ALTER TABLE <table
name>
ADD FOREIGN KEY (Column name) REFERENCES Parent Table (Primary key of Parent
Table);
REMOVING
CONSTRAINTS
- To remove primary key constraint from a table, we use ALTER TABLE command
as : ALTER TABLE <table
name>
DROP PRIMARY KEY ;
- To remove foreign key constraint from a table, we use ALTER TABLE command
as : ALTER TABLE <table
name>
DROP FOREIGN KEY ;
ENABLING/DISABLING
CONSTRAINTS
Only foreign key can be disabled/enabled in
MySQL.
To disable foreign keys : SET FOREIGN_KEY_CHECKS =
0 ;
To enable foreign keys : SET FOREIGN_KEY_CHECKS =
1;
INTEGRITY
CONSTRAINTS/CONSTRAINTS
- A constraint is a condition or check applicable on a field(column) or set of
fields(columns).
- Common types of constraints
include :
S.No. Constraints Description
1 NOT NULL Ensures that a column cannot have NULL value
2 DEFAULT Provides a default value for a column when none is specified
3 UNIQUE Ensures that all values in a column are different
4 CHECK Makes sure that all values in a column satisfy certain criteria
5 PRIMARY KEY Used to uniquely identify a row in the table
6 FOREIGN KEY Used to ensure referential integrity of the data
NOT NULL
CONSTRAINT
By default, a column can hold NULL. It you not want to allow NULL value in a column, then NOT NULL constraint
must be applied on that column. E.g.
CREATE TABLE
Customer
( SID integer NOT NULL ,
Last_Name varchar(30) NOT
NULL ,
First_Name varchar(30) ) ;
Columns SID and Last_Name cannot include NULL, while First_Name can include
NULL.
An attempt to execute the following SQL
statement, INSERT INTO
Customer
VALUES (NULL , ‘Kumar’ , ‘Ajay’);
will result in an error because this will lead to column SID being NULL, which violates the NOT NULL constraint on
that column.
DEFAULT
CONSTARINT
The DEFAULT constraint provides a default value to a column when the INSERT INTO statement does not
provide a specific value. E.g.
CREATE TABLE
Student
( Student_ID integer ,
Name varchar(30) ,
Score integer DEFAULT 80);
Column SID has a unique constraint, and hence cannot include duplicate values. So, if the table already contains
the following rows :
CHECK
CONSTRAINT
- The CHECK constraint ensures that all values in a column satisfy certain conditions. Once defined, the table will
only insert a new row or update an existing row if the new value satisfies the CHECK constraint.
e.g.
CREATE TABLE
Customer
( SID integer CHECK (SID > 0),
Last_Name varchar(30) ,
First_Name varchar(30) ) ;
will result in an error because the values for SID must be greater than
0.
PRIMARY KEY
CONSTRAINT
- A primary key is used to identify each row in a table. A primary key can consist of one or more fields(column)
on a table. When multiple fields are used as a primary key, they are called a composite key.
- You can define a primary key in CREATE TABLE command through keywords PRIMARY KEY.
e.g.
CREATE TABLE
Customer
( SID integer NOT NULL PRIMARY
KEY, Last_Name varchar(30) ,
First_Name varchar(30) ) ;
Or
CREATE TABLE
Customer
( SID integer,
Last_Name varchar(30) ,
First_Name varchar(30),
PRIMARY KEY (SID) ) ;
- The latter way is useful if you want to specify a composite primary key,
e.g.
CREATE TABLE
Customer
( Branch integer NOT NULL,
SID integer NOT NULL ,
Last_Name varchar(30) ,
First_Name varchar(30),
PRIMARY KEY (Branch ,
SID) ) ;
FOREIGN KEY
CONSTRAINT
- Foreign key is a non key column of a table (child table ) that draws its values from primary key of another
table( parent table).
- The table in which a foreign key is defined is called a referencing table or child table. A table to which a foreign
key points is called referenced table or parent table.
e.g.
Parent Table
TABLE:
STUDENT
ROLL_NO NAME
CLASS Primary key
1 ABC XI
2 DEF XII
3 XYZ XI
Child
TABLE: Table
SCORE
ROLL_NO MARKS
1 55
2 83
3 90
Here column Roll_No is a foreign key in table SCORE(Child Table) and it is drawing its values from Primary
key (ROLL_NO) of STUDENT table.(Parent Key).
CREATE TABLE
STUDENT
( ROLL_NO integer NOT NULL PRIMARY
KEY NAME
, VARCHAR(30) ,
CLASS VARCHAR(3) );
CREATE TABLE
SCORE
( ROLL_NO integer ,
MARKS integer ,
FOREIGN KEY(ROLL_NO) REFERNCES
STUDENT(ROLL_NO) ) ;
* Foreign key is always defined in the child
table.
Syntax for using foreign
key
FOREIGN KEY(column name) REFERENCES Parent_Table(PK of Parent
Table);
REFERENCING
ACTIONS
Referencing action with ON DELETE clause determines what to do in case of a DELETE occurs in the parent
table. Referencing action with ON UPDATEclause determines what to do in case of a UPDATE occurs in the
parent table.
Actions:
1. CASCADE : This action states that if a DELETE or UPDATE operation affects a row from the parent table ,
then automatically delete or update the matching rows in the child table i.e., cascade the action to child
2. table.
SET NULL : This action states that if a DELETE or UPDATE operation affects a row from the parent table, then
set the foreign key column in the child table to NULL.
3. NO ACTION : Any attempt for DELETE or UPDATE in parent table is not
allowed.
4. RESTRICT : This action rejects the DELETE or UPDATE operation for the parent
table.
Q: Create two
tables Customer(customer_id, name)
Customer_sales(transaction_id, amount ,
customer_id)
Underlined columns indicate primary keys and bold column names indicate foreign
key.
Make sure that no action should take place in case of a DELETE or UPDATE in the parent
table.
Sol : CREATE TABLE Customer
( customer_id int Not Null Primary
Key
name, varchar(30) ) ;
Table : EMPL
1. AVG( )
This function computes the average of given
data.
e.g. SELECT AVG(SAL)
FROM EMPL ;
Output
AVG(SAL)
6051.6
2. COUNT( )
This function counts the number of rows in a given
column.
If you specify the COLUMN name in parenthesis of function, then this function returns rows where COLUMN is
not null.
If you specify the asterisk (*), this function returns all rows, including duplicates and
nulls.
e.g. SELECT COUNT(*)
FROM EMPL ;
Output
COUNT(*)
5
3. MAX( )
This function returns the maximum value from a given column or
expression.
e.g. SELECT MAX(SAL)
FROM EMPL ;
Output
MAX(SAL)
9870
4. MIN( )
This function returns the minimum value from a given column or
expression.
e.g. SELECT MIN(SAL)
FROM EMPL ;
Output
MIN(SAL)
2985
5. SUM( )
This function returns the sum of values in given column or
expression.
e.g. SELECT SUM(SAL)
FROM EMPL ;
Output
SUM(SAL)
30258
** One thing that you should keep in mind is that while grouping , you should include only those values in the
SELECT list that either have the same value for a group or contain a group(aggregate) function. Like in e.g. 2
given above, DEPTNO column has one(same) value for a group and the other expression SUM(SAL) contains a
group function.
NESTED
GROUP
- To create a group within a group i.e., nested group, you need to specify multiple fields in the GROUP BY
expression. e.g. To group records job wise within Deptno wise , you need to issue a query statement like :
MySQL
FUNCTIONS
A function is a special type of predefined command set that performs some operation and returns a single
value. Types of MySQL functions : String Functions , Maths Functions and Date & Time Functions.
Table : EMPL
STRING
FUNCTIONS
1. CONCAT( ) - Returns the Concatenated
String. Syntax : CONCAT(Column1 , Column2 ,
e.g. Column3,SELECT
…….) CONCAT(EMPNO , ENAME) FROM EMPL WHERE
DEPTNO=10;
Output
CONCAT(EMPNO , ENAME)
8369SMITH
8912SUR
e.g.2.
SELECT LENGTH(ENAME) FROM
Output EMPL;
LENGTH(ENAME)
5
4
4
4
3
NUMERIC
FUNCTIONS
These functions accept numeric values and after performing the operation, return numeric
value. 1. MOD( ) – Returns the remainder of given two numbers. e.g. SELECT MOD(11 , 4) ;
Output
MOD(11, 4 )
3
n
2. POW( ) / POWER( ) - This function returns m i.e , a number m raised to theth n
power. e.g. SELECT POWER(3,2) ;
Output
POWER(3, 2 )
9
e.g. 2. SELECT ROUND(15.193 , -1); - This will convert the number to nearest
ten’s .
Output
ROUND(15.193 , -1)
20
E.g. 2. SELECT TRUNCATE(15.79 , -1); - This command truncate value 15.79 to nearest ten’s
place.
Output
TRUNCATE(15.79 , -1)
10
3. MONTH( ) – This function returns the month from the date passed.
E.g. SELECT MONTH( ‘2016-02-
Output 09’) ;
MONTH( ‘2016-02- 09’)
02
6. DAYOFMONTH( ) – This function returns the day of month. Returns value in range of 1 to
31.E.g. SELECT DAYOFMONTH( ‘2016-12-
Output 14’) ;
DAYOFMONTH( ‘2016-12-14’)
14
7. DAYOFWEEK( ) – This function returns the day of week. Return the weekday index for date.
(1=Sunday, 2=Monday,……., 7=Saturday)
SELECT DAYOFWEEK( ‘2016-12-
14’) ;
Output
DAYOFWEEK( ‘2016-12-14’)
4
8. DAYOFYEAR( ) – This function returns the day of the year. Returns the value between 1 and 366.
E.g. SELECT DAYOFYEAR( ‘2016-02-
Output 04) ;
DAYOFYEAR( ‘2016-02-04’)
35
10. SYSDATE( ) – It also returns the current date but it return the time at which SYSDATE( ) executes. It differs
from the behavior for NOW( ), which returns a constant time that indicates the time at which the statement began
to execute.e.g. SELECT SYSDATE( ) ;
DATABASE
TRANSACTIONS
TRANSACTIO
N
A Transaction is a logical unit of work that must succeed or fail in its entirety. This statement means that a
transaction may involve many sub steps, which should either all be carried out successfully or all be ignored if
some failure occurs. A Transaction is an atomic operation which may not be divided into smaller operations.
Example of a
Transaction
Begin
transaction
Get balance from account
X
Calculate new balance as X –
1000
Store new balance into database
file
Get balance from account
YCalculate new balance as Y +
1000
Store new balance into database
End file
transaction
TRANSACTION PROPERTIES (ACID
PROPERTIES)
1. ATOMICITY(All or None Concept) – This property ensures that either all operations of the transaction are
carried out or none are.
2. CONSISTENCY – This property implies that if the database was in a consistent state before the start of
transaction execution, then upon termination of transaction, the database will also be in a consistent state.
3. ISOLATION – This property implies that each transaction is unaware of other transactions executing
concurrently in the system.
4. DURABILITY – This property of a transaction ensures that after the successful completion of a transaction,
the changes made by it to the database persist, even if there are system failures.
NO :
Success ? ROLLBACK
YES : COMMIT
Database after changes
SET
AUTOCOMMIT
By default, MySQL has autocommit ON, which means if you do not start a transaction explicitly through a BEGIN or
STATE TRANSACTION command, then every statement is considered one transaction and is committed there and
then.
You can check the current setting by executing the following
statement :
mysql > select @@autocommit ;
@@autocommit
1 1 means, autocommit is
enabled
SET AUTOCOMMIT = 0 ;
INSERT INTO Emp VALUES( 5 ,
‘Fazaria’);
COMMIT ;
UPDATE Emp SET NAME = ‘Farzziya’ WHERE ENO =
5;
SAVEPOINT A ;
INSERT INTO Emp VALUES(6 ,
‘Richards’);
SAVEPOINT B ;
INSERT INTO Emp VALUES(7,
‘Rajyalakshmi’);
SAVEPOINT C ;
ROLLBACK TO B ;
What will be the output of the following SQL query now ? SELECT * FROM
Emp ;
Q4. If you have not executed the COMMIT command , executing which command will reverse all updates made
during the current work session in MySQL ?
- A join is a query that combines rows from two or more tables. In a join- query, more
than one table are listed in FROM clause.
Table : empl
Table : dept
This query will give you the Cartesian product i.e. all possible concatenations are formed of all
rows of both the tables EMPL and DEPT. Such an operation is also known as Unrestricted Join.
It returns n1 x n2 rows where n1 is number of rows in first table and n2 is number of rows in
second table.
EQUI-JOIN
- The join in which columns are compared for equality, is called Equi - Join. In equi-join, all
the columns from joining table appear in the output even if they are identical.
e.g. SELECT * FROM empl,
dept WHERE empl.deptno =
dept.deptno ;
deptno column is appearing twice in output.
Q: with reference to empl and dept table, find the location of employee
SMITH.
ename column is present in empl and loc column is present in dept. In order to obtain the result,
we have to join two tables.
SELECT ENAME,
LOC
FROM EMPL,
DEPT
WHERE EMPL.DEPTNO = DEPT.DEPTNO AND
ENAME=’SMITH’;
SELECT EMPL.deptno,
dname,empno,ename,job,sal
FROM
EMPL,DEPT
WHERE
EMPL.DEPTNO=DEPT.DEPTNO
ORDER BY EMPL.DEPTNO;
QUALIFIED
NAMES
Did you notice that in all the WHERE conditions of join queries given so far, the field(column)
names are given as: <tablename>.<columnname>
This type of field names are called qualified field names. Qualified field names are very useful
in identifying a field if the two joining tables have fields with same time. For example, if we say
deptno field from joining tables empl and dept, you’ll definitely ask - deptno field of which
table ? To avoid such an ambiguity, the qualified field names are used.
TABLE ALIAS
- A table alias is a temporary label given along with table name in FROM
clause.
e.g.
SELECT E.DEPTNO,
DNAME,EMPNO,ENAME,JOB,SAL
FROM EMPL E, DEPT
DWHERE E.DEPTNO =
DEPT.DEPTNO
ORDER BY E.DEPTNO;
In above command table alias for EMPL table is E and for DEPT table , alias is
D.
Q: Display details like department number, department name, employee number,
employee name, job and salary. And order the rows by employee number with department
number. These details should be only for employees earning atleast Rs. 6000 and of SALES
department.
SELECT E.DEPTNO, DNAME,EMPNO, ENAME, JOB,
SAL
FROM EMPL E, DEPT
D
WHERE E.DEPTNO =
D.DEPTNO
AND DNAME=’SALES’
AND
SAL>=6000
ORDER BY E.DEPTNO;
NATURAL
JOIN
By default, the results of an equijoin contain two identical columns. One of the two
identical columns can be eliminated by restating the query. This result is called a Natural join.
empl.* means select all columns from empl table. This thing can be used with any
table.
The join in which only one of the identical columns(coming from joined tables) exists, is
called Natural Join.
LEFT, RIGHT
JOINS
When you join tables based on some condition, you may find that only some, not all rows
from either table match with rows of other table. When you display an equi join or natural join ,
it shows only the matched rows. What if you want to know which all rows from a table did not
match with other. In such a case, MySQL left or right JOIN can be very helpful.
LEFT JOIN
- You can use LEFT JOIN clause in SELECT to produce left join
i.e. SELECT <select-
list>
FROM <table1> LEFT JOIN
<table2>
ON <joining-condition>;
- When using LEFT JOIN all rows from the first table will be returned whether there
are matches in the second table or not. For unmatched rows of first table, NULL is
shown in columns of second table.
S1 S2
Roll_no Name Roll_no Class
1 A 2 III
2 B 4 IX
3 C 1 IV
4 D 3 V
5 E 7 I
6 F 8 II
SELECT S1.ROLL_NO,
NAME,CLASS
FROM S1 LEFT JOIN S2 ON
S1.ROLL_NO=S2.ROLL_NO;
RIGHT JOIN
- It works just like LEFT JOIN but with table order reversed. All rows from the second table
are going to be returned whether or not there are matches in the first table.
- You can use RIGHT JOIN in SELECT to produce right join
i.e. SELECT <select-
list>
FROM <table1> RIGHT JOIN <table2> ON <joining-
condition>;
e.g SELECT S1.ROLL_NO,
NAME,CLASS
FROM S1 RIGHT JOIN S2 ON
S1.ROLL_NO=S2.ROLL_NO;
(URL: http://passport.gov.in)
2. Income Tax Portal
- Income tax portal include: preparation and filling individual Income Tax returns and
TDS returns by tax deductore and filling and tracking of PAN/TAN applications.
(URL: http://www.incometaxindia.gov.in )
3. DRDO
- It is a network of more than 50 laboratories which are deeply engaged in developing
defence technologies.
(URL: drdo.nic.in)
4. Supreme Court of India
- It is the highest judicial body in India. It has also its web-presence in the form of a
website that can be used to know about Supreme Court Judgments.
(URL: http://supremecourtofindia.nic.in)
5. RTI Portal
Right to Information Act 2005 mandates timely response to citizen requests for
government information. (URL: rti.gov.in)
1. People living in rural and remote areas could not benefit from the e-governance
initiatives because of lack of computerization in these areas.
2. Lack of awareness about the e-governance programs has prevented people to
benefit from it.
3. Not all services are part of e-governance, so manual methods cannot be avoided.
4. Incompatibility of software and hardware has prevented people to benefit from it.
5. Some people find it inconvenient to make payments online using credit cards.
E-BUSINESS
1. IRCTC Portal
- The Indian Railways Catering and Tourism Corporation (IRCTC) is a subsidiary of the
Indian Railways.
(URL: www.irctc.co.in)
2. Online reservation site Yatra.com
- Yatra.com provides airline reservation, flight ticket booking service online.
(URL: www.yatra.com)
(URL:
http://www.statebankofindia.com)
4. Online store Amazon.com
- The amazon.com was launched online in 1995. It is an online store which sells wide
variety of products. (URL: www.amazon.com)
BENEFITS OF E-BUSINESS TO CUSTOMERS
- With the help of certain e-business sites, people are easily finding low cost flights.
3. CHANGE IN ONLINE SHOPPING HABITS
- Online offers are given at attractive discounts and prices. This convenience coupled
with better bargains have brought changes in the online shopping habits of buyers.
1. w3schools.com
- The w3schools.com hosts thousands of online web tutorials pertaining the domain of
web building. (URL : www.w3schools.com)
2. exe project
- It is developed as a freely available open source authoring application to assist
teachers and academics in the publishing of web content without the need to
become proficient in HTML or XML. (URL : exelearning.org)
3. Xerte Project
- It is open source e-learning developer tool. The Xerte Project provides a full suite of
tools for e-learning developer and content authors.
(URL: www.nottingham.ac.uk/xerte)
BENEFITS OF E-LEARNING
- The front-end (generally of a GUI) is the end that interacts with the user and collects
input from the user.
BACKEND
- The backend is the end that is not visible but that processes the user-requests as
received by the front-end.