Relational Database
Management
system.(RDBMS)
(E.F.codd) IBM 1970
RDBMS is the basis for SQL and for
all modern database systems such as
(MS SQL SERVER,IBM
DB2,ORACLE,MYSQL, MICROSOFT
ACCESS)
 The data in RDBMS is store in database
objects called tables . A tables is a
collection of related data entries and it
consists of columns and rows.
 Alternative terminology for Relational
mode. Formal terms Alternative 1 Alternative 2
Relation Table File
Tuple Row Record
Attribute Column Field
PROPERTIES OF RELATIONAL
 Each relation has unique name.
 Each tuple/row is unique NO duplicate row.
 Entries in any column have the same
domain.
 Each attribute/column has a unique name
 Order of the column or row is irrelevant
i.e relation are unordered.
 Each cell of relation contain exactly one
value i.e attribute value are required to atomic.
SQL :- Structured Query Language.
SQL is a standard language for accessing and
manipulating databases.
1)SQL stands for structured Query Language.
2)SQL is a declarative programming
language designed for creating and
querying RDBMS.
3)SQL become a standard of the “American
national standards institute(ANSI) in
1986,and of the international organization
for standardization(ISO) in 1987.
What can SQL do ?
 SQL can execute queries against a database.
 SQL can retrieve data from a database.
 SQL can insert records in a database.
 SQL can update records in a database.
 SQL can delete records from a database.
 SQL can create new database.
 SQL can create new tables in a database.
 SQL can create stored procedures in a database.
 SQL can create views in a database.
 SQL can set permission on tables, procedures, and views.
 DATABASE TABLES :- A database most
often contains one or more tables .each
tables is identified by a name (e.g.
“students” or “employees”).tables contain
records (ROWS) with data.
Roll no Name Age Email City
1 praveen 19
sonipraveen220@gmail.com
Bhopal
2 shivam 20
joshShivam8547@gmai.com
Indore
3 aman 18
Aman.aman440@gmail.com
Jabalpur
 SEMICOLON AFTER SQL
STATEMENTS?
Some database systems require a
semicolon at the end of SQL each
statement.
Semicolon is the standard way to
separate each SQL statement in database
systems that allow more than one SQL
statement to be executed in the same call
to the server.
Some of The Most Important SQL
Command
 SELECT - extracts data from a database
 UPDATE - updates data in a database
 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database
 CREATE DATABASE - creates a new database
 ALTER DATABASE - modifies a database
 CREATE TABLE - creates a new table
 ALTER TABLE - modifies a table
 DROP TABLE - deletes a table
 CREATE INDEX - creates an index (search key)
 DROP INDEX - deletes an index
SQL DATA TYPES
 CHARACTER (length)or CHAR(length):-
The CHAR data type accepts character
string ,including unicode , of a fixed
length.
 VARCHAR (length):- The VARECHAR data
type accepts character string , including
unicode , of a variable length is up to the
maximum length specified in the data
type declaration.
CHARE(20) AMAN 4 - 16 16 HOLED
VARCHAR (20)
AMAN 4 – 16 16 FREE
 INTEGER OR INT :- The INTEGER data
type accepts numeric values with an
implied scale of zero. 12345
 DATE :- The DATE data type accepts date
value and date value should be specified
in the form: YYYY-MM-DD.
 TIME :- The TIME data type accepts time
value and time value should be specified
in the form: HH:MM:SS.
CREATE TABLE SYNTAX
 CREATE TABLE table _name (column1
data type,column2 data type,column3
data type, column4 data type,column5
data type);
 FOR EXAMPLE:- create table
student(SID INT(12),SNAME
VARCHAR(30),AGE INT(4),ADDRESS
VARCHAR(50),MOBILE INT(16));
SID SNAME AGE ADDRESS MOBILE
INSERT INTO SYNTAX
 INSERT INTO table_name
(column1,column2,column3,column4,colu
mn5)VALUES(value1,value2,value3,value4
,value5));
 INSERT INTO student( sid,sname,age,
address,mobile)VALUES(1,PRAVEEN,19,K
ATNI,8818858401);
SID SNAME AGE ADDRESS MOBILE
1 PRAVEEN 19 KATNI 881885840
1
INSERT INTO SYNTAX 2
 INSERT INTO table_name
VALUES(value1,value2,value3,value4,valu
e5);
 INSERT INTO student VALUES
(2,“SHIVAM”,18, “INDORE”,8959959372);
SID SNAME AGE ADDRESS MOBLIE
2 SHIVAM 18 INDORE 8959959372
UPDATE SYNTAX
 UPDATE table-name SET column1= value1
, column2 = value2, WHERE condition;
 UPDATE student SET name = “ mohit ” ,
mobile = 8269968401,WHERE sid = 1 ;
SID NAME AGE ADDRESS MOBILE
1 MOHIT 19 KATNI 8269968401
SID NAME AGE ADDRESS MOBILE
1 PRAVEEN 19 KATNI 8818858401
DELETE SYNTAX
 DELETE FROM table_ name WHERE
condition;
 DELETE FROM student WHERE name =
mohit ;
SID NAME AGE ADDRESS MOBILE
2 SHIVAM 18 INDORE 8959959372
SID NAME AGE ADDRESS MOBILE
1 MOHIT 19 KATNI 8269968401
2 SHIVAM 18 INDORE 8959959372
DELETE ALL RECORDS SYNTAX
 DELETE FROM table_ name;
 DELETE FROM student;
SID NAME AGE ADDRESS MOBILE
- - - - -
- - - - -
SID NAME AGE ADDRESS MOBILE
1 MOHIT 19 KATNI 8269968401
2 SHIVAM 18 INDORE 8959959372
DROP TABLE SYNTAX
 DROP TABLES table _name;
 DROP TABLES student;
SID NAME AGE ADDRESS MOBILE EMAIL
1 PRAVEEN 19 KATNI 8818858401 SONIPRAVEEN220@
GMAIL.COM
2 SHIVAM 18 INDORE 8959959372 joshShivam8547@g
mai.com
3 AMAN 17 JABLPUR 7024348361
Aman.aman440@g
mail.com
- - - - - -
- - - - - -
- - - - - -
- - - - - -
CONSTRAINTS ARE COMMONLY USED IN SQL
 NOT NULL - Ensures that a column cannot have a
NULL value
 UNIQUE - Ensures that all values in a column are
different
 PRIMARY KEY - A combination of a NOT NULL and
UNIQUE. Uniquely identifies each row in a table
 FOREIGN KEY - Uniquely identifies a row/record in
another table
 CHECK - Ensures that all values in a column satisfies
a specific condition
 DEFAULT - Sets a default value for a column when no
value is specified
 INDEX - Used to create and retrieve data from the
database very quickly
Key constraint
 A key is an attribute or set of attribute
that uniquely identifies any record
(or tuple) from the table.
 PURPOSE:-1) key is use to identify any
record or row of data from table.
2) It is also used to establish and identify
relationship between tables.
TYPES OF KEY IN SQL
 SUPER KEY
 CANDIDATE KEY
 PRIMARY KEY
 ALTERNATE KEY
 FOREIGN KEY
 COMPOSITE KEY
SUPER KEY
 Super key is a combination of all possible
attributes that can uniquely identify the
row (or tuple) in the given relation.
➢ super key is superset of a combination key.
➢ a table can have many super keys.
➢ A super key have addition attribute that are not
needed for unique identify.
CANDIDATE KEY
 A candidate key is an attribute or set of
an attribute which can uniquely identify a
tuple.
 Candidate key are not allowed to have
null value .
 Candidate keys are defined as distinct set
of attributes from which primary key can
be selected.
 A candidate key is a minimal super key
or a super key with no redundant
attribute .
PRIMRY KEY
 Primary key is one of the candidate key
chosen by the database designer to
unique identify the tuple in relation.
➢ The value of primary key can never be NULL.
➢ The value of primary key must always be unique (not
duplicate).
➢ The null values of primary key never be changed i.e
no updation is possible .
➢ The vale of primary key must be assigned when
inserting a record.
➢ A relation is allowed to have only one primary key.
ALTERNATE KEY
 Out of all candidate key ,only one gets
selected as primary key ,remaining key
are known as alternate key.
 In student table:-
o “Id” is best suited for the primary key.
o Rest of the attribute like “email”, are
considered as a alternate key.
FOREIGN KEYS
 A foreign key is:-
➢ A key used to link two tables together.
➢An attribute (or set attributes) in one
table that refers to the primary key in
another table .
• The purpose of the foreign key is:-
➢ To ensure or maintain REFERENTIAL
integrity of the data.
FOREIGN KEY
 Foreign key reference the primary key of the table .
 Foreign key can take only those value which are present in the
primary key of the referenced relation.
 Foreign key may have a name other than that of primary key .
 Foreign key can take the null value.
 There is no restriction on a foreign key to be unique.
 In fact, foreign key is not unique most of the time.
 Referenced relation may also be called as the master tables or
primary tables.
 Referencing relation my also be calld as the foreign table.
COMPOSITE KEY
 A key that more than one attributes is
known as composite key is also known as
compound key.
 composite key:-{customer _id ,
product _code}
Customer _ id Order _ id Product _code Product _
count
CUST01 1 PRO11 10
CUST01 14 PRO12 9
CUST03 14 PRO11 5
SQL CONSTRAINT
 PRIMARY KEY:- A combination of NOT NULL
AND UNIQUE uniquely identified each row in
a table.
 PRIMARY KYE SYNTAX :-CREATE TABLE
student(ID INT(12) PRIMARY KEY, NAME
VARCHAR(30),AGE INT(5),EMAIL
VARCHAR(50));
 MYSQL PRIMARY KYE SYNTAX:- CREATE
TABLE student (ID INT(12),NAME
VARCHAE(30),AGE INT(5),PRIMARY
KEY(ID));
UNIQUE CONSTRAIN CREATE
 UNIQUE :- unique constraint Ensures that
all values a column are different and only
one (1) value must be null.
 SYNTAX :- CREATE TABLE student (ID
INT(12) UNIQUE,NAME VARCHAR(30),AGE
INT(5),EMAIL VARCHAR(50));
 DROP a UNIQUE constrain :- ALTER
TABLE DROP CONSTRAINT UC_student;
FOREIGN KEY CONSTRAINT
 CONSIDER a NEW TABLE ACCOUNT is
created and it is used id(primary key)of
table student as a foreign key.
 RULES :- 1)table values must match
values of a parent(p.k)table.
 2)
 3)
 SYNTAX :-CREATE TABLE account(id
int(12)primary key, name varchar(30) unique, age
int(5),email varchar(50),sid int(12) FOREIGN KEY
REFERENCES student(sid));
NOT NULL CONSTRAINT
 BY default ,a column can hold null values.
 The not null constraint enforces a column
to NOT accept NULL values.
 SYNTAX:- CREATE TABLE student(ID
INT(12)NOT NULL,NAME VARCHAR(30)
NOT NULL,AGE INT(5) NOT NULL,EMAIL
VARCHAR(50) NOT NULL unique);
CHECK CONSTRAINT SYNTAX.
 The check constraint is use to limit the
value range that can be place in a column.
 CHECK: constraint on a single column it
allows only certain values for this column.
 CHECK SYNTAX :- CREATE TABLE student(
id int (12) NOT NULL primary key, name
varchar(30)NOT NULL ,age int (5) CHECK
(AGE>18)AND(AGE<45),city
varchar(20),pin code int (10));
CHECK CONSTRAINT
 Simple check syntax:- CREATE TABLE
student( id int (12) NOT NULL primary
key, name varchar(30)NOT NULL ,age int
(5) CHECK (AGE>18),email varchar(50));
 ORACLE SYNTAX:- CHREATE TABLE
table_name( id int(12),name
varchar(30),age int(5) check(age>18
and(age<30));
DEFAULT constraint
The default constraint is use to provide a
default value for a column.
SYNTAX:- CREATE TABLE student(ID
INT(12)NOT NULL,NAME VARCHAR(30)
NOT NULL,AGE INT(5) NOT NULL, CITY
VARCHAR(20) DEFAULT “INDORE”);
DROP A DEFAULT SYNTAX:- ALTER TABLE
student ALTER city DROP DEFAULT ;
SQL CREATE INDEX SYNTAX
 THE CREATE INDEX statement is used to
create indexes in tables.
 Index are used to retrieve data from the
database more quickly than other wish. The
user cannot see the indexes ,they are just
used to speed up searches/queries.
 Syntax:- CREATE INDEX index _name ON
TABLE _NAME(COLUMN1,COLUMN2,….);
 DROP ORACLE:- DROP INDEX INDEX_NAME;
AUTO INCREMENT
 AUTO_INCREMENT allows a unique
number o be generated automatically
when a new row(record) is inserted into
table. Often this is primary key field that
we would like to be create automatically
every time a new record is inserted.
 Ms syntax:-CREATE TABLE student(id INT
(12) AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30),age INT(5));
SELECT SYNTAX
 The SELECT statement is used to select
data from a database.
 The data returned is stored is stored in a
result table, called the result-set.
 ( * ) :- ALL ATTRIBUTE
 SELECT:- ATTRIBUTE NAME
 FROM :- TABLE NAME
 WHERE:- CONDITIONS
SELECT SYNTAX 2
• SYNTAX:- SELECT * FROM table _name;
▪ SELECT * FROM student;
STUDENT
ID NAME AGE CLASS FEES CITY
1 AMAN 19 B.COM 9,000 KATNI
2 SHIVAM 18 BBA 15,000 JABLPUR
3 MOHIT 19 DCA 16,000 BHOPAL
4 TARUN 20 BCA 10,000 UJJAN
5 ROHIT 21 B.SC 12,000 INDORE
6 PRAVEEN 19 B.COM 9,000 GWALIOR
SELECT SYNTAX 2
 SELECT column1,column2,column3,
FROM table _name;
 SELECT name ,age ,class FROM student;
student
NAME AGE CLASS
AMAN 19 B.COM
SHIVAM 18 BBA
MOHIT 19 DCA
TARUN 20 BCA
ROHIT 21 B.SC
SELECT SYNTAX 3
 SELECT * FROM table _name WHERE
column _ name = “ value ”;
 SELECT * FROM student WHERE class =
“ B.com ”;
student
ID NAME AGE CLASS FEES CITY
1 AMAN 19 B.COM 9,000 KATNI
6 PRAVEEN 19 B.COM 9,000 GWALIOR
SQL AND OPERTORS SYNTAX 1
 The AND operator displays a record if all
the condition separated by AND are true.
 SELECT * FROM table _name WHERE
condition1 AND condtion2 ;
 SYNTAX:- SELECT * FROM student
WHERE class=“BBA” AND age = 18;
student
t
ID NAME AGE CLASS FEES CITY
2 SHIVAM 18 BBA 15,000 JABLPUR
SQL AND OPERTORS SYNTAX 1
 SYNTAX:-
SELECT * FROM student WHERE
fees>=9000 AND fees<=12000;
student
ID NAME AGE CLASS FEES CITY
1 AMAN 19 B.COM 9,000 KATNI
4 TARUN 20 BCA 10,000 UJJAN
5 ROHIT 21 B.SC 12,000 INDORE
6 PRAVEEN 19 B.COM 9,000 GWALIOR
SQL OR OPERTORS SYNTAX 1
❖ The OR opertor displays a record if any
conditions separated by.
❖ SELECT column1, column2,column3,
FROM table _name WHERE condition1 OR
condtion2 OR condition3 ;
 SYNTAX:- SELECT * FROM student
WHERE class=“B.sc” OR age=“21”;
student
ID NAME AGE CLASS FEES CITY
5 ROHIT 21 B.SC 12,000 INDORE
“NOT”OPERTORS SYNTAX <>/!=
 The NOT operator displays a record if the
condition is NOT TRUE.
 SYNTAX:- SELECT * FROM table _name
WHERE NOT condition;
 SELECT * FROM student WHERE NOT
city= gwalior ;
ID NAME AGE CLASS FEES CITY
1 AMAN 19 B.COM 9,000 KATNI
2 SHIVAM 18 BBA 15,000 JABLPUR
3 MOHIT 19 DCA 16,000 BHOPAL
4 TARUN 20 BCA 10,000 UJJAN
5 ROHIT 21 B.SC 12,000 INDORE
“IN” OPERATOR SYNTAX
 The “in” operator allows you to specify
multiple values in a WHERE clause.
 SELECT * FROM table _name WHERE
column _name IN (value1,value2..) ;
 SYNTAX:-SELECT * FROM student WHERE
class IN (“b.sc” ,“Dca”);
student
ID NAME AGE CLASS FEES CITY
3 MOHIT 19 DCA 16,000 BHOPAL
5 ROHIT 21 B.SC 12,000 INDORE
“IN” OPERATOR SYNTAX
 SELECT column_name1,column _name 2
FROM table _name WHERE column _name
IN (value1,value2..) ;
 SYNTAX:-SELECT name, fees FROM
student WHERE class IN (“B.com”,“Bca”) ;
student
NAME FEES
AMAN 9,000
TARUN 10,000
PRAVEEN 9,000
“BETWEEN” OPERATOR SYNTAX
 The between operator select value with in
a given range. The value can be numbers
,text or dates.
 SELECT * FROM table _name WHERE
column _name BETWEEN value1 AND
value2;
 SELECT * FROM student WHERE age
BETWEEN 18 AND 21;
ID NAME AGE CLASS FEES CITY
2 SHIVAM 18 BBA 15,000 JABLPUR
5 ROHIT 21 B.SC 12,000 INDORE
“BETWEEN” OPERATOR SYNTAX
 SELECT column _name FROM table _name
WHERE column _name BETWEEN value1
AND value2;
 SELECT name FROM student WHERE fees
BETWEEN 9000 AND 12000;
student
NAME
AMAN
TARUN
ROHIT
PRAVEEN
“LIKE” OPERATOR SYNTAX {P%}
 The LIKE operator is used in a WHERE
clause to search for a specified pattern in
a column.
 SELECT * FROM table _name WHERE
column _name LIKE “pattern%”;
 SYNTAX :-SELECT * FROM student WHERE
name LIKE “ S% ”;
student
ID NAME AGE CLASS FEES CITY
2 SHIVAM 18 BBA 15,000 JABLPUR
“LIKE” OPERATOR SYNTAX {%P}
 SELECT * FROM table _name WHERE
column _name LIKE “%pattern”;
 SYNTAX :-SELECT * FROM student WHERE
name LIKE “ %T ”;
student
ID NAME AGE CLASS FEES CITY
3 MOHIT 19 DCA 16,000 BHOPAL
5 ROHIT 21 B.SC 12,000 INDORE
“LIKE” OPERATOR SYNTAX {%P%}
 SELECT * FROM table _name WHERE
column _name LIKE “%pattern%”;
 SYNTAX :-SELECT * FROM student WHERE
name LIKE “ %V% ”;
student
ID NAME AGE CLASS FEES CITY
2 SHIVAM 18 BBA 15,000 JABLPUR
6 PRAVEEN 19 B.COM 9,000 GWALIOR
“LIKE” OPERATOR SYNTAX {%A}
 SELECT column _name FROM table _name
WHERE column _name BETWEEN value 1
AND value2 column _name LIKE
“%pattern”;
 SYNTAX :-SELECT name FROM student
WHERE age BETWEEN 19 AND 21 name
LIKE “%n ”;
Name
Tarun
SQL FUNCTION
 SQL provides many built _in function to
perform operations on data.
 These functions are useful while performing
mathematical calculations, string
concatentions sub _strings etc.
 SQL functions are divided into two
categories:- 1) Aggregate functions.
2) Scalar functions.
❑Aggregate and Scalar function both return a
single value.
❑Aggregate operator on many records while
scalar operate on each record independently.
MIN AGGREGATE FUNCTIONS
 The MIN () function returns the smallest
value of the selected column.
 SELECT MIN(column _name)FROM table
_name;
 SYNTAX:- SELECT MIN(FEES)FROM
student ;
student
MIN(FEES)
9000
MAX AGGREGATE FUNCTIONS
 The MAX () function returns the smallest
value of the selected column.
 SELECT MAX(column _name)FROM table
_name;
 SYNTAX:- SELECT MAX(FEES)FROM
student ;
student
MAX(FEES)
16,000
SUM AGGREGATE FUNCTIONS
 The SUM() function returns the total sum
of a numeric column.
 SELECT SUM(column _name)FROM table
_name;
 SYNTAX:- SELECT SUM(FEES)FROM
student ;
student
SUM (FEES)
71,000
AVG AGGREGATE FUNCTIONS
 The AVG() function returns the average
value of a numeric column.
 SELECT AVG(column _name)FROM table
_name;
 SYNTAX:- SELECT AVG(FEES)FROM
student ;
student
AVG(FEES)
11834
COUNT (*)AGGREGATE FUNCTIONS
 The COUNT () function returns the
number of rows that matches a specified
criterion.
 SELECT COUNT(*) FROM table _name;
 SYNTAX:- SELECT COUNT(*)FROM
student ;
student
COUNT(*)
6
SQL ORDER BY DESC
 The ORDER BY keyword is used to sort
the result-set in ascending or descending
order.
 SELECT * FROM table _name ORDER BY column _name
DESC;
 SELECT * FROM student ORDER BY name
DESC;
student
ID NAME AGE CLASS FEES CITY
5 ROHIT 21 B.SC 12,000 INDORE
2 SHIVAM 18 BBA 15,000 JABLPUR
4 TARUN 20 BCA 10,000 UJJAN
SQL ORDER BY ASC
 SELECT * FROM table _name ORDER BY
column _name ASC;
 SELECT * FROM student ORDER BY name
ASC;
student
ID NAME AGE CLASS FEES CITY
1 AMAN 19 B.COM 9,000 KATNI
3 MOHIT 19 DCA 16,000 BHOPAL
6 PRAVEEN 19 B.COM 9,000 GWALIOR
5 ROHIT 21 B.SC 12,000 INDORE
2 SHIVAM 18 BBA 15,000 JABLPUR
4 TARUN 20 BCA 10,000 UJJAN
SQL GROUP BY
 The GROUP BY statement groups rows
that have the same values into summary
rows.
 The GROUP BY statement is often used
with aggregate functions (COUNT, MAX,
MIN, SUM, AVG) to group the result-set
by one or more columns.
SQL GROUP BY
 SELECT column _name FROM table _name
GROUP BY column _name;
 SELECT class ,sum(fees)FROM student
GROUP BY class;
student
Class Sum (fees)
B.com 18000
B.SC 12000
BBA 15000
BCA 10000
DCA 16000
The SQL HAVING Clause
 The HAVING clause was added to SQL
because the WHERE keyword could not be
used with aggregate functions.
 SELECT COUNT(fees),class FROM student GROUP
BY class HAVING COUNT (fees)>10000;
SQL JOIN
 A JOIN clause is used to combine rows
from two or more tables, based on a
related column between them.
 SELECT student.ID ,student.name
,account.fees FROM student , account
WHERE student.ID=account.ID;
DIFFERENT TYPES OF SQL JOIN
 (INNER) JOIN: Returns records that have
matching values in both tables.
 LEFT (OUTER) JOIN: Returns all records
from the left table, and the matched
records from the right table.
 RIGHT (OUTER) JOIN: Returns all records
from the right table, and the matched
records from the left table.
 FULL (OUTER) JOIN: Returns all records
when there is a match in either left or
right table
STUDENT TABLE
ACCOUNT AND LIBRARY TABLES
SQL INNER /EQUI JOIN(,)
 The INNER join keyword select records
that have matching values in both tables.
 SYNTAX:- SELECT table1 column _name1
, table1 column _name2, table2 column _name
FROM table1 INNER JOIN table2 WHERE
table1.column _name = table1.column
_name;
SQL INNER /EQUI JOIN(,)
 SELECT stu.id ,student.class
,account.accountfeepaid FROM
stu,account WHERE stu.id=accountid;
SQL INNER /EQUI JOIN(,)
 SELECT stu.id ,stu.name
,account.accountfeepaid,library.noofbook
FROM stu,account,library WHERE
(stu.id=accountid)and(stu.id=library.id);
SQL LEFT JOIN
 The LEFT JOIN keyword returns all records from the left
table (table1), and the matched records from the right
table (table2). The result is NULL from the right side, if
there is no match.
 SELECT table1 column _name1 , table1
column _name2, table2 column _name
FROM table1 LEFT JOIN table2 WHERE
table1.column _name = table1.column
_name;
SQL LEFT JOIN
 SELECT
stu.id,stu.name,stu.class,account.account
feepaid FROM stu LEFT JOIN account on
stu,id = account.accountid;
SQL RIGHT JOIN
 The RIGHT JOIN keyword returns all records from the right
table (table2), and the matched records from the left table
(table1). The result is NULL from the left side, when there
is no match.
 SELECT table1 column _name1 , table1
column _name2, table2 column _name
FROM table1 RIGHT JOIN table2 WHERE
table1.column _name = table1.column
_name;
SQL RIGHT JOIN
 SELECT
stu.id,stu.name,stu.class,account.account
feepaid FROM stu RIGHT JOIN account on
stu,id = account.accountid;
SQL FULL JOIN
 The FULL OUTER JOIN keyword returns all
records when there is a match in left (table1) or
right (table2) table records.
 SELECT table1 column _name1 , table1
column _name2, table2 column _name
FROM table1 FULL JOIN table2 WHERE
table1.column _name = table1.column
_name;
SQL SELF JOIN
 A self JOIN is a regular join, but the table
is joined with itself.
 SELECT table1 column _name1 , table1
column _name2, table2 column _name
FROM table1 SELF JOIN table2 WHERE
table1.column _name = table1.column
_name;
SHOW TABLES COMMAND
Relational model concept.
 Relation :A relation is a table with columns and
row.
 Attribute: An attribute is a named column of
relation.
 Domain: A domain is the set of allowable values
for one or more attributes.
 Tuple:- A tuple is a row of relation.
 Relation schema : relation schema represents the name
of the relation with its attributes.
 Relation instance: Relation instance is a finite set of
tuple. relation instance never have duplicate tuples.
 degree: The total number of rows present in the
table.
 Cardinality: Total number of row present in the
table.
 TABLE/RELATION
Attribute/column
STUDENT
Primary key
Tuple/row cardinality
(no. of row4)
Domain degree (no. of column)=3
(e.g interger 0-9) Tuple variable
Relation schema:- student ( id, name, mobile)
ID(pk) NAME MOBILE
1 AMAN 7024348361
2 SHIVAM 8959959372
3 MOHIT 8839285958
4 TARUN 6264128865
Thank you
Some basic command
 Show databases;
 Show tables;
 Desc table_name;
 Clear
 Mysqld_safe
 Mysqld_safe –u root
 Mysql
Relational database management system

Relational database management system

  • 1.
    Relational Database Management system.(RDBMS) (E.F.codd) IBM1970 RDBMS is the basis for SQL and for all modern database systems such as (MS SQL SERVER,IBM DB2,ORACLE,MYSQL, MICROSOFT ACCESS)
  • 2.
     The datain RDBMS is store in database objects called tables . A tables is a collection of related data entries and it consists of columns and rows.  Alternative terminology for Relational mode. Formal terms Alternative 1 Alternative 2 Relation Table File Tuple Row Record Attribute Column Field
  • 3.
    PROPERTIES OF RELATIONAL Each relation has unique name.  Each tuple/row is unique NO duplicate row.  Entries in any column have the same domain.  Each attribute/column has a unique name  Order of the column or row is irrelevant i.e relation are unordered.  Each cell of relation contain exactly one value i.e attribute value are required to atomic.
  • 4.
    SQL :- StructuredQuery Language. SQL is a standard language for accessing and manipulating databases. 1)SQL stands for structured Query Language. 2)SQL is a declarative programming language designed for creating and querying RDBMS. 3)SQL become a standard of the “American national standards institute(ANSI) in 1986,and of the international organization for standardization(ISO) in 1987.
  • 5.
    What can SQLdo ?  SQL can execute queries against a database.  SQL can retrieve data from a database.  SQL can insert records in a database.  SQL can update records in a database.  SQL can delete records from a database.  SQL can create new database.  SQL can create new tables in a database.  SQL can create stored procedures in a database.  SQL can create views in a database.  SQL can set permission on tables, procedures, and views.
  • 6.
     DATABASE TABLES:- A database most often contains one or more tables .each tables is identified by a name (e.g. “students” or “employees”).tables contain records (ROWS) with data. Roll no Name Age Email City 1 praveen 19 sonipraveen220@gmail.com Bhopal 2 shivam 20 joshShivam8547@gmai.com Indore 3 aman 18 Aman.aman440@gmail.com Jabalpur
  • 7.
     SEMICOLON AFTERSQL STATEMENTS? Some database systems require a semicolon at the end of SQL each statement. Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.
  • 8.
    Some of TheMost Important SQL Command  SELECT - extracts data from a database  UPDATE - updates data in a database  DELETE - deletes data from a database  INSERT INTO - inserts new data into a database  CREATE DATABASE - creates a new database  ALTER DATABASE - modifies a database  CREATE TABLE - creates a new table  ALTER TABLE - modifies a table  DROP TABLE - deletes a table  CREATE INDEX - creates an index (search key)  DROP INDEX - deletes an index
  • 9.
    SQL DATA TYPES CHARACTER (length)or CHAR(length):- The CHAR data type accepts character string ,including unicode , of a fixed length.  VARCHAR (length):- The VARECHAR data type accepts character string , including unicode , of a variable length is up to the maximum length specified in the data type declaration. CHARE(20) AMAN 4 - 16 16 HOLED VARCHAR (20) AMAN 4 – 16 16 FREE
  • 10.
     INTEGER ORINT :- The INTEGER data type accepts numeric values with an implied scale of zero. 12345  DATE :- The DATE data type accepts date value and date value should be specified in the form: YYYY-MM-DD.  TIME :- The TIME data type accepts time value and time value should be specified in the form: HH:MM:SS.
  • 11.
    CREATE TABLE SYNTAX CREATE TABLE table _name (column1 data type,column2 data type,column3 data type, column4 data type,column5 data type);  FOR EXAMPLE:- create table student(SID INT(12),SNAME VARCHAR(30),AGE INT(4),ADDRESS VARCHAR(50),MOBILE INT(16)); SID SNAME AGE ADDRESS MOBILE
  • 12.
    INSERT INTO SYNTAX INSERT INTO table_name (column1,column2,column3,column4,colu mn5)VALUES(value1,value2,value3,value4 ,value5));  INSERT INTO student( sid,sname,age, address,mobile)VALUES(1,PRAVEEN,19,K ATNI,8818858401); SID SNAME AGE ADDRESS MOBILE 1 PRAVEEN 19 KATNI 881885840 1
  • 13.
    INSERT INTO SYNTAX2  INSERT INTO table_name VALUES(value1,value2,value3,value4,valu e5);  INSERT INTO student VALUES (2,“SHIVAM”,18, “INDORE”,8959959372); SID SNAME AGE ADDRESS MOBLIE 2 SHIVAM 18 INDORE 8959959372
  • 14.
    UPDATE SYNTAX  UPDATEtable-name SET column1= value1 , column2 = value2, WHERE condition;  UPDATE student SET name = “ mohit ” , mobile = 8269968401,WHERE sid = 1 ; SID NAME AGE ADDRESS MOBILE 1 MOHIT 19 KATNI 8269968401 SID NAME AGE ADDRESS MOBILE 1 PRAVEEN 19 KATNI 8818858401
  • 15.
    DELETE SYNTAX  DELETEFROM table_ name WHERE condition;  DELETE FROM student WHERE name = mohit ; SID NAME AGE ADDRESS MOBILE 2 SHIVAM 18 INDORE 8959959372 SID NAME AGE ADDRESS MOBILE 1 MOHIT 19 KATNI 8269968401 2 SHIVAM 18 INDORE 8959959372
  • 16.
    DELETE ALL RECORDSSYNTAX  DELETE FROM table_ name;  DELETE FROM student; SID NAME AGE ADDRESS MOBILE - - - - - - - - - - SID NAME AGE ADDRESS MOBILE 1 MOHIT 19 KATNI 8269968401 2 SHIVAM 18 INDORE 8959959372
  • 17.
    DROP TABLE SYNTAX DROP TABLES table _name;  DROP TABLES student; SID NAME AGE ADDRESS MOBILE EMAIL 1 PRAVEEN 19 KATNI 8818858401 SONIPRAVEEN220@ GMAIL.COM 2 SHIVAM 18 INDORE 8959959372 joshShivam8547@g mai.com 3 AMAN 17 JABLPUR 7024348361 Aman.aman440@g mail.com - - - - - - - - - - - - - - - - - - - - - - - -
  • 18.
    CONSTRAINTS ARE COMMONLYUSED IN SQL  NOT NULL - Ensures that a column cannot have a NULL value  UNIQUE - Ensures that all values in a column are different  PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table  FOREIGN KEY - Uniquely identifies a row/record in another table  CHECK - Ensures that all values in a column satisfies a specific condition  DEFAULT - Sets a default value for a column when no value is specified  INDEX - Used to create and retrieve data from the database very quickly
  • 19.
    Key constraint  Akey is an attribute or set of attribute that uniquely identifies any record (or tuple) from the table.  PURPOSE:-1) key is use to identify any record or row of data from table. 2) It is also used to establish and identify relationship between tables.
  • 20.
    TYPES OF KEYIN SQL  SUPER KEY  CANDIDATE KEY  PRIMARY KEY  ALTERNATE KEY  FOREIGN KEY  COMPOSITE KEY
  • 21.
    SUPER KEY  Superkey is a combination of all possible attributes that can uniquely identify the row (or tuple) in the given relation. ➢ super key is superset of a combination key. ➢ a table can have many super keys. ➢ A super key have addition attribute that are not needed for unique identify.
  • 22.
    CANDIDATE KEY  Acandidate key is an attribute or set of an attribute which can uniquely identify a tuple.  Candidate key are not allowed to have null value .  Candidate keys are defined as distinct set of attributes from which primary key can be selected.  A candidate key is a minimal super key or a super key with no redundant attribute .
  • 23.
    PRIMRY KEY  Primarykey is one of the candidate key chosen by the database designer to unique identify the tuple in relation. ➢ The value of primary key can never be NULL. ➢ The value of primary key must always be unique (not duplicate). ➢ The null values of primary key never be changed i.e no updation is possible . ➢ The vale of primary key must be assigned when inserting a record. ➢ A relation is allowed to have only one primary key.
  • 24.
    ALTERNATE KEY  Outof all candidate key ,only one gets selected as primary key ,remaining key are known as alternate key.  In student table:- o “Id” is best suited for the primary key. o Rest of the attribute like “email”, are considered as a alternate key.
  • 25.
    FOREIGN KEYS  Aforeign key is:- ➢ A key used to link two tables together. ➢An attribute (or set attributes) in one table that refers to the primary key in another table . • The purpose of the foreign key is:- ➢ To ensure or maintain REFERENTIAL integrity of the data.
  • 26.
    FOREIGN KEY  Foreignkey reference the primary key of the table .  Foreign key can take only those value which are present in the primary key of the referenced relation.  Foreign key may have a name other than that of primary key .  Foreign key can take the null value.  There is no restriction on a foreign key to be unique.  In fact, foreign key is not unique most of the time.  Referenced relation may also be called as the master tables or primary tables.  Referencing relation my also be calld as the foreign table.
  • 27.
    COMPOSITE KEY  Akey that more than one attributes is known as composite key is also known as compound key.  composite key:-{customer _id , product _code} Customer _ id Order _ id Product _code Product _ count CUST01 1 PRO11 10 CUST01 14 PRO12 9 CUST03 14 PRO11 5
  • 28.
    SQL CONSTRAINT  PRIMARYKEY:- A combination of NOT NULL AND UNIQUE uniquely identified each row in a table.  PRIMARY KYE SYNTAX :-CREATE TABLE student(ID INT(12) PRIMARY KEY, NAME VARCHAR(30),AGE INT(5),EMAIL VARCHAR(50));  MYSQL PRIMARY KYE SYNTAX:- CREATE TABLE student (ID INT(12),NAME VARCHAE(30),AGE INT(5),PRIMARY KEY(ID));
  • 29.
    UNIQUE CONSTRAIN CREATE UNIQUE :- unique constraint Ensures that all values a column are different and only one (1) value must be null.  SYNTAX :- CREATE TABLE student (ID INT(12) UNIQUE,NAME VARCHAR(30),AGE INT(5),EMAIL VARCHAR(50));  DROP a UNIQUE constrain :- ALTER TABLE DROP CONSTRAINT UC_student;
  • 30.
    FOREIGN KEY CONSTRAINT CONSIDER a NEW TABLE ACCOUNT is created and it is used id(primary key)of table student as a foreign key.  RULES :- 1)table values must match values of a parent(p.k)table.  2)  3)  SYNTAX :-CREATE TABLE account(id int(12)primary key, name varchar(30) unique, age int(5),email varchar(50),sid int(12) FOREIGN KEY REFERENCES student(sid));
  • 31.
    NOT NULL CONSTRAINT BY default ,a column can hold null values.  The not null constraint enforces a column to NOT accept NULL values.  SYNTAX:- CREATE TABLE student(ID INT(12)NOT NULL,NAME VARCHAR(30) NOT NULL,AGE INT(5) NOT NULL,EMAIL VARCHAR(50) NOT NULL unique);
  • 32.
    CHECK CONSTRAINT SYNTAX. The check constraint is use to limit the value range that can be place in a column.  CHECK: constraint on a single column it allows only certain values for this column.  CHECK SYNTAX :- CREATE TABLE student( id int (12) NOT NULL primary key, name varchar(30)NOT NULL ,age int (5) CHECK (AGE>18)AND(AGE<45),city varchar(20),pin code int (10));
  • 33.
    CHECK CONSTRAINT  Simplecheck syntax:- CREATE TABLE student( id int (12) NOT NULL primary key, name varchar(30)NOT NULL ,age int (5) CHECK (AGE>18),email varchar(50));  ORACLE SYNTAX:- CHREATE TABLE table_name( id int(12),name varchar(30),age int(5) check(age>18 and(age<30));
  • 34.
    DEFAULT constraint The defaultconstraint is use to provide a default value for a column. SYNTAX:- CREATE TABLE student(ID INT(12)NOT NULL,NAME VARCHAR(30) NOT NULL,AGE INT(5) NOT NULL, CITY VARCHAR(20) DEFAULT “INDORE”); DROP A DEFAULT SYNTAX:- ALTER TABLE student ALTER city DROP DEFAULT ;
  • 35.
    SQL CREATE INDEXSYNTAX  THE CREATE INDEX statement is used to create indexes in tables.  Index are used to retrieve data from the database more quickly than other wish. The user cannot see the indexes ,they are just used to speed up searches/queries.  Syntax:- CREATE INDEX index _name ON TABLE _NAME(COLUMN1,COLUMN2,….);  DROP ORACLE:- DROP INDEX INDEX_NAME;
  • 36.
    AUTO INCREMENT  AUTO_INCREMENTallows a unique number o be generated automatically when a new row(record) is inserted into table. Often this is primary key field that we would like to be create automatically every time a new record is inserted.  Ms syntax:-CREATE TABLE student(id INT (12) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30),age INT(5));
  • 37.
    SELECT SYNTAX  TheSELECT statement is used to select data from a database.  The data returned is stored is stored in a result table, called the result-set.  ( * ) :- ALL ATTRIBUTE  SELECT:- ATTRIBUTE NAME  FROM :- TABLE NAME  WHERE:- CONDITIONS
  • 38.
    SELECT SYNTAX 2 •SYNTAX:- SELECT * FROM table _name; ▪ SELECT * FROM student; STUDENT ID NAME AGE CLASS FEES CITY 1 AMAN 19 B.COM 9,000 KATNI 2 SHIVAM 18 BBA 15,000 JABLPUR 3 MOHIT 19 DCA 16,000 BHOPAL 4 TARUN 20 BCA 10,000 UJJAN 5 ROHIT 21 B.SC 12,000 INDORE 6 PRAVEEN 19 B.COM 9,000 GWALIOR
  • 39.
    SELECT SYNTAX 2 SELECT column1,column2,column3, FROM table _name;  SELECT name ,age ,class FROM student; student NAME AGE CLASS AMAN 19 B.COM SHIVAM 18 BBA MOHIT 19 DCA TARUN 20 BCA ROHIT 21 B.SC
  • 40.
    SELECT SYNTAX 3 SELECT * FROM table _name WHERE column _ name = “ value ”;  SELECT * FROM student WHERE class = “ B.com ”; student ID NAME AGE CLASS FEES CITY 1 AMAN 19 B.COM 9,000 KATNI 6 PRAVEEN 19 B.COM 9,000 GWALIOR
  • 41.
    SQL AND OPERTORSSYNTAX 1  The AND operator displays a record if all the condition separated by AND are true.  SELECT * FROM table _name WHERE condition1 AND condtion2 ;  SYNTAX:- SELECT * FROM student WHERE class=“BBA” AND age = 18; student t ID NAME AGE CLASS FEES CITY 2 SHIVAM 18 BBA 15,000 JABLPUR
  • 42.
    SQL AND OPERTORSSYNTAX 1  SYNTAX:- SELECT * FROM student WHERE fees>=9000 AND fees<=12000; student ID NAME AGE CLASS FEES CITY 1 AMAN 19 B.COM 9,000 KATNI 4 TARUN 20 BCA 10,000 UJJAN 5 ROHIT 21 B.SC 12,000 INDORE 6 PRAVEEN 19 B.COM 9,000 GWALIOR
  • 43.
    SQL OR OPERTORSSYNTAX 1 ❖ The OR opertor displays a record if any conditions separated by. ❖ SELECT column1, column2,column3, FROM table _name WHERE condition1 OR condtion2 OR condition3 ;  SYNTAX:- SELECT * FROM student WHERE class=“B.sc” OR age=“21”; student ID NAME AGE CLASS FEES CITY 5 ROHIT 21 B.SC 12,000 INDORE
  • 44.
    “NOT”OPERTORS SYNTAX <>/!= The NOT operator displays a record if the condition is NOT TRUE.  SYNTAX:- SELECT * FROM table _name WHERE NOT condition;  SELECT * FROM student WHERE NOT city= gwalior ; ID NAME AGE CLASS FEES CITY 1 AMAN 19 B.COM 9,000 KATNI 2 SHIVAM 18 BBA 15,000 JABLPUR 3 MOHIT 19 DCA 16,000 BHOPAL 4 TARUN 20 BCA 10,000 UJJAN 5 ROHIT 21 B.SC 12,000 INDORE
  • 45.
    “IN” OPERATOR SYNTAX The “in” operator allows you to specify multiple values in a WHERE clause.  SELECT * FROM table _name WHERE column _name IN (value1,value2..) ;  SYNTAX:-SELECT * FROM student WHERE class IN (“b.sc” ,“Dca”); student ID NAME AGE CLASS FEES CITY 3 MOHIT 19 DCA 16,000 BHOPAL 5 ROHIT 21 B.SC 12,000 INDORE
  • 46.
    “IN” OPERATOR SYNTAX SELECT column_name1,column _name 2 FROM table _name WHERE column _name IN (value1,value2..) ;  SYNTAX:-SELECT name, fees FROM student WHERE class IN (“B.com”,“Bca”) ; student NAME FEES AMAN 9,000 TARUN 10,000 PRAVEEN 9,000
  • 47.
    “BETWEEN” OPERATOR SYNTAX The between operator select value with in a given range. The value can be numbers ,text or dates.  SELECT * FROM table _name WHERE column _name BETWEEN value1 AND value2;  SELECT * FROM student WHERE age BETWEEN 18 AND 21; ID NAME AGE CLASS FEES CITY 2 SHIVAM 18 BBA 15,000 JABLPUR 5 ROHIT 21 B.SC 12,000 INDORE
  • 48.
    “BETWEEN” OPERATOR SYNTAX SELECT column _name FROM table _name WHERE column _name BETWEEN value1 AND value2;  SELECT name FROM student WHERE fees BETWEEN 9000 AND 12000; student NAME AMAN TARUN ROHIT PRAVEEN
  • 49.
    “LIKE” OPERATOR SYNTAX{P%}  The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.  SELECT * FROM table _name WHERE column _name LIKE “pattern%”;  SYNTAX :-SELECT * FROM student WHERE name LIKE “ S% ”; student ID NAME AGE CLASS FEES CITY 2 SHIVAM 18 BBA 15,000 JABLPUR
  • 50.
    “LIKE” OPERATOR SYNTAX{%P}  SELECT * FROM table _name WHERE column _name LIKE “%pattern”;  SYNTAX :-SELECT * FROM student WHERE name LIKE “ %T ”; student ID NAME AGE CLASS FEES CITY 3 MOHIT 19 DCA 16,000 BHOPAL 5 ROHIT 21 B.SC 12,000 INDORE
  • 51.
    “LIKE” OPERATOR SYNTAX{%P%}  SELECT * FROM table _name WHERE column _name LIKE “%pattern%”;  SYNTAX :-SELECT * FROM student WHERE name LIKE “ %V% ”; student ID NAME AGE CLASS FEES CITY 2 SHIVAM 18 BBA 15,000 JABLPUR 6 PRAVEEN 19 B.COM 9,000 GWALIOR
  • 52.
    “LIKE” OPERATOR SYNTAX{%A}  SELECT column _name FROM table _name WHERE column _name BETWEEN value 1 AND value2 column _name LIKE “%pattern”;  SYNTAX :-SELECT name FROM student WHERE age BETWEEN 19 AND 21 name LIKE “%n ”; Name Tarun
  • 53.
    SQL FUNCTION  SQLprovides many built _in function to perform operations on data.  These functions are useful while performing mathematical calculations, string concatentions sub _strings etc.  SQL functions are divided into two categories:- 1) Aggregate functions. 2) Scalar functions. ❑Aggregate and Scalar function both return a single value. ❑Aggregate operator on many records while scalar operate on each record independently.
  • 54.
    MIN AGGREGATE FUNCTIONS The MIN () function returns the smallest value of the selected column.  SELECT MIN(column _name)FROM table _name;  SYNTAX:- SELECT MIN(FEES)FROM student ; student MIN(FEES) 9000
  • 55.
    MAX AGGREGATE FUNCTIONS The MAX () function returns the smallest value of the selected column.  SELECT MAX(column _name)FROM table _name;  SYNTAX:- SELECT MAX(FEES)FROM student ; student MAX(FEES) 16,000
  • 56.
    SUM AGGREGATE FUNCTIONS The SUM() function returns the total sum of a numeric column.  SELECT SUM(column _name)FROM table _name;  SYNTAX:- SELECT SUM(FEES)FROM student ; student SUM (FEES) 71,000
  • 57.
    AVG AGGREGATE FUNCTIONS The AVG() function returns the average value of a numeric column.  SELECT AVG(column _name)FROM table _name;  SYNTAX:- SELECT AVG(FEES)FROM student ; student AVG(FEES) 11834
  • 58.
    COUNT (*)AGGREGATE FUNCTIONS The COUNT () function returns the number of rows that matches a specified criterion.  SELECT COUNT(*) FROM table _name;  SYNTAX:- SELECT COUNT(*)FROM student ; student COUNT(*) 6
  • 59.
    SQL ORDER BYDESC  The ORDER BY keyword is used to sort the result-set in ascending or descending order.  SELECT * FROM table _name ORDER BY column _name DESC;  SELECT * FROM student ORDER BY name DESC; student ID NAME AGE CLASS FEES CITY 5 ROHIT 21 B.SC 12,000 INDORE 2 SHIVAM 18 BBA 15,000 JABLPUR 4 TARUN 20 BCA 10,000 UJJAN
  • 60.
    SQL ORDER BYASC  SELECT * FROM table _name ORDER BY column _name ASC;  SELECT * FROM student ORDER BY name ASC; student ID NAME AGE CLASS FEES CITY 1 AMAN 19 B.COM 9,000 KATNI 3 MOHIT 19 DCA 16,000 BHOPAL 6 PRAVEEN 19 B.COM 9,000 GWALIOR 5 ROHIT 21 B.SC 12,000 INDORE 2 SHIVAM 18 BBA 15,000 JABLPUR 4 TARUN 20 BCA 10,000 UJJAN
  • 61.
    SQL GROUP BY The GROUP BY statement groups rows that have the same values into summary rows.  The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
  • 62.
    SQL GROUP BY SELECT column _name FROM table _name GROUP BY column _name;  SELECT class ,sum(fees)FROM student GROUP BY class; student Class Sum (fees) B.com 18000 B.SC 12000 BBA 15000 BCA 10000 DCA 16000
  • 63.
    The SQL HAVINGClause  The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.  SELECT COUNT(fees),class FROM student GROUP BY class HAVING COUNT (fees)>10000;
  • 64.
    SQL JOIN  AJOIN clause is used to combine rows from two or more tables, based on a related column between them.  SELECT student.ID ,student.name ,account.fees FROM student , account WHERE student.ID=account.ID;
  • 65.
    DIFFERENT TYPES OFSQL JOIN  (INNER) JOIN: Returns records that have matching values in both tables.  LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from the right table.  RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records from the left table.  FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
  • 66.
  • 67.
  • 68.
    SQL INNER /EQUIJOIN(,)  The INNER join keyword select records that have matching values in both tables.  SYNTAX:- SELECT table1 column _name1 , table1 column _name2, table2 column _name FROM table1 INNER JOIN table2 WHERE table1.column _name = table1.column _name;
  • 69.
    SQL INNER /EQUIJOIN(,)  SELECT stu.id ,student.class ,account.accountfeepaid FROM stu,account WHERE stu.id=accountid;
  • 70.
    SQL INNER /EQUIJOIN(,)  SELECT stu.id ,stu.name ,account.accountfeepaid,library.noofbook FROM stu,account,library WHERE (stu.id=accountid)and(stu.id=library.id);
  • 71.
    SQL LEFT JOIN The LEFT JOIN keyword returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side, if there is no match.  SELECT table1 column _name1 , table1 column _name2, table2 column _name FROM table1 LEFT JOIN table2 WHERE table1.column _name = table1.column _name;
  • 72.
    SQL LEFT JOIN SELECT stu.id,stu.name,stu.class,account.account feepaid FROM stu LEFT JOIN account on stu,id = account.accountid;
  • 73.
    SQL RIGHT JOIN The RIGHT JOIN keyword returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side, when there is no match.  SELECT table1 column _name1 , table1 column _name2, table2 column _name FROM table1 RIGHT JOIN table2 WHERE table1.column _name = table1.column _name;
  • 74.
    SQL RIGHT JOIN SELECT stu.id,stu.name,stu.class,account.account feepaid FROM stu RIGHT JOIN account on stu,id = account.accountid;
  • 75.
    SQL FULL JOIN The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table records.  SELECT table1 column _name1 , table1 column _name2, table2 column _name FROM table1 FULL JOIN table2 WHERE table1.column _name = table1.column _name;
  • 76.
    SQL SELF JOIN A self JOIN is a regular join, but the table is joined with itself.  SELECT table1 column _name1 , table1 column _name2, table2 column _name FROM table1 SELF JOIN table2 WHERE table1.column _name = table1.column _name;
  • 78.
  • 79.
    Relational model concept. Relation :A relation is a table with columns and row.  Attribute: An attribute is a named column of relation.  Domain: A domain is the set of allowable values for one or more attributes.  Tuple:- A tuple is a row of relation.  Relation schema : relation schema represents the name of the relation with its attributes.  Relation instance: Relation instance is a finite set of tuple. relation instance never have duplicate tuples.  degree: The total number of rows present in the table.  Cardinality: Total number of row present in the table.
  • 80.
     TABLE/RELATION Attribute/column STUDENT Primary key Tuple/rowcardinality (no. of row4) Domain degree (no. of column)=3 (e.g interger 0-9) Tuple variable Relation schema:- student ( id, name, mobile) ID(pk) NAME MOBILE 1 AMAN 7024348361 2 SHIVAM 8959959372 3 MOHIT 8839285958 4 TARUN 6264128865
  • 81.
  • 89.
    Some basic command Show databases;  Show tables;  Desc table_name;  Clear  Mysqld_safe  Mysqld_safe –u root  Mysql