MySQL Handbook 2023-24
MySQL Handbook 2023-24
(using MySQL)
Unique Key - It is a column or set of column which can uniquely identify each row
in a table but it accept NULL values.
Foreign Key – It is a column in a table which is the primary key in another table.
CUSTOMER
ORDER
Customer_id
Candidate Key – There may be two or more attributes which can become the
primary key of table. These all are candidate keys.
Alternate Key – Out of all Candidate keys one will be selected as Primary key and
remaining will be known as Alternate keys.
CHECK - CHECK constraint ensures that all values in a column satisfy certain
conditions.
e.g. – CREATE TABLE Customer
(SID integer CHECK(SID>0),
Last_Name varchar(30),
First_Name varchar(30));
DATA TYPES
For Numbers – INT , INT(Value)
For Decimal Numbers – FLOAT(M,N)
For String/Text – CHAR(N), VARCHAR(N)
For Date - DATE
Degree = 3 Cardinality=5
DDL COMMANDS
1. CREATE TABLE
SYNTAX :
CREATE TABLE Table_Name
(
Col_Name_1 DataType Constraints,
Col_Name_2 DataType Constraints,
Col_Name_3 DataType Constraints,
.
.
Col_Name_n DataType Constraints
);
NOTE
SYNTAX:-
ALTER TABLE Table_name
ADD/MODIFY/DROP(column1_name datatype(size),
column2_name datatype(size),
…………………………………
columni_name datatype(size)
);
3. DROP TABLE
SYNTAX:-
DROP TABLE Table_Name ;
e.g. :- drop table ‘companies’.
Disadvantages:-
1. With indexes, the performances on Insert, Update, delete
decreases.
2. Indexes consumes storage space and this increases with number of
attributes and their length.
SYNTAX :-
CREATE TABLE Table_Name
( Col1 DataType [NOT NULL/PRIMARY KEY],
Col2 DataType [NOT NULL/PRIMARY KEY],
.
.
INDEX Index_Name (Index_Col(Size) [ASC/DESC],
.
.
);
CREATE INDEX Index_Name ON TableName (Col1 [ASC/DESC],
Col2 [ASC/DESC],…….);
UPDATE
Two clause UPDATE & SET are used for changing the values stored in any cell.
SYNTAX:::-
UPDATE Table_name
SET column_name = value_expression
[WHERE condition];
SYNTAX:::-
DELETE FROM Table_Name
WHERE predicate;
SELECT
Select command is used to retrieve/ fetch/ extract or display data from the Table.
SYNTAX :
SELECT [DISTINCT|ALL] column_name1,column_name2,….
FROM table_name1
[WHERE Condition]
[ORDER BY Ordering_column [ASC|DESC]
[GROUP BY Grouping_coumn_name]
[HAVING Predicate];
WHERE
WHERE :- The WHERE keyword is used to select group of records based on some
condition(s).
cname
BPL
IBM
ORDER BY
ORDER BY :-
1. ORDER BY clause is used to produce output in a logical order.
2. The order may be of Ascending or Descending for numbers or alphabet.
3. The keyword ASC and DESC are used for Ascending or Descending order.
Note : By default order by arranges all records in Ascending order.
e.g. :- Display all details from table companies in descending order of their company name.
cnum cname city stdcode
Ans.
SELECT * 1003 Voltas Chennai 044
FROM Companies 1004 IBM Delhi 011
ORDER BY cname DESC; 1001 BPL Delhi 011
Foreign Key – It is a column in a table which is the primary key in another table.
CUSTOMER
ORDER
Customer_id
FootNote : - For 2 table based queries, always write the condition on common column that
must be equal for both tables.
ROLLBACK TO Mark2;
BankID Branch Balance
1001 Jaipur 10110000
SELECT * FROM Bank;
1002 Kota 21023300
1003 Ajmer 10000230
INSERT INTO TABLE VALUES (1006,”Agra”,10076230); BankID Branch Balance
SELECT * FROM Bank; 1001 Jaipur 10110000
1002 Kota 21023300
1003 Ajmer 10000230
1006 Agra 10076230
CARTESIAN PRODUCT
Method1
SELECT *
FROM Student, Issue;
Method2
SELECT *
FROM Student JOIN Issue;
Method3
SELECT *
FROM Student CROSS JOIN Issue;
NATURAL JOIN
Method1
SELECT *
FROM Student NATURAL JOIN Issue;
Method2
SELECT *
FROM Student JOIN Issue
P a g e 22 | 23 Amit Kumar Jain,PGT(CS), K.V. Sunderbani
USING (RollNo);
EQUI JOIN
Method1
SELECT *
FROM Student JOIN Issue
WHERE Student.RollNo=Issue.RollNo;
Method2
SELECT *
FROM Student JOIN Issue
ON Student.RollNo=Issue.RollNo;
OUTER JOIN
(LEFT JOIN/ RIGHT JOIN)
SELECT *
FROM Student LEFT JOIN Issue
ON Student.RollNo=Issue.RollNo;
SELECT *
FROM Student RIGHT JOIN Issue
ON Student.RollNo=Issue.RollNo;