DBMS LAB File-1
DBMS LAB File-1
Table Name
// Command
Create table table_name (
Column 1 datatype,
Column2 datatype,
Column3 datatype,
…
);
DEFAULT ‘BLY’
UNIQUE NOT NULL
SELECT SYNTAX
Select column1, column2, column3,…..
From table_name;
Select*
From table_name;
Ruchi 20 F
Raman 21 M
Ruchi 20 F
OPERATOR DESCRIPTION
= EQUAL
Syntax
Select column1, column2, column3,….
From table_name
Where condition1 AND conditon2 And condition3….;
Select*
From table_name
Where column_name BETWEEN value1 AND value2….;
Select column1, column2, column3,….
From table_name
Where column_name NOTBETWEEN value1 AND value2….;
SELECT Data with LIKE Operator
UPDATE SYNTAX
Update table_name
Set column1_name = value1, column_name2= value2,…
Where condition;
Update employee
Set salary=6000
Where id=2;
Syntax
Create table table_name(
Id int not null auto_increment,
Name varchar(50) not null,
Age int not null,
City varchar(10) not null,
PRIMARY KEY (id)
);
SYNTAX
Create table student(
Id int not null auto_increment,
Name varchar(50) not null,
Age int not null,
City varchar(10) not null,
PRIMARY KEY (id),
FOREIGN KEY (city) references city (cid)
);
Alter table with Primary Key
Alter table Table_name
ADD FOREIGN KEY(city) REFERENCES City(cid);
JOIN
INNER JOIN
LEFT JOIN
RIGHT JOIN
CROSS JOIN
Inner Join
INNER JOIN
Id NAME AGE City
PRIMARY KEY
MATCH
FOREIGN KEY
SYNTAX
Select column_name
From table1
Inner join table 2
On table1.column_name = table2.column_name;
LEFT JOIN
What is left join ?
TABLE 1 TABLE 2
LEFT JOIN
The left join return all records from the left table (table1), and the matched records from the right
table (table2)
Student table LEFT JOIN city
table
1 Rushel 19 1 1 Ludhiyana
2 Abrahim 18 2 2 Banaras
3 John 19 6 3 Keral
4 Tom 21 3 4 Mumbai
5 Thomas 20 5 5 Delhi
6 Riyana 21 4 6 Gandhinagar
MATCH
SYNTAX
Select column_name
From table1
left join table 2
On table1.column_name = table2.column_name;
Select *
From table1
LEFT join table 2
On table1.column_name = table2.column_name;
RIGHT JOIN
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching
records (if any) from the left table (table1).
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
And a selection from the "Employees" table:
Example
SELECT Orders.OrderID, Employees.LastName, Employees.FirstName
FROM Orders
RIGHT JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
ORDER BY Orders.OrderID;
Exercise:
Choose the correct JOIN clause to select all the records from the Customers table plus all the
matches in the Orders table.
SELECT *
FROM Orders
-----
ON Orders.CustomerID=Customers.CustomerID;
CROSS JOIN
The CROSS JOIN keyword returns all records from both tables (table1 and table2).
CROSS JOIN Syntax
SELECT column_name(s)
FROM table1
CROSS JOIN table2;
10308 2 7 1996-09-18 3
10309 37 3 1996-09-19 1
10310 77 8 1996-09-20 2
The following SQL statement selects all customers, and all orders:
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
CROSS JOIN Orders;
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
CROSS JOIN Orders
WHERE Customers.CustomerID=Orders.CustomerID;
Self Join
A self join is a regular join, but the table is joined with itself.
Example
SELECT A.CustomerName AS CustomerName1,
B.CustomerName AS CustomerName2, A.City
FROM Customers A, Customers B
WHERE A.CustomerID <> B.CustomerID
AND A.City = B.City
ORDER BY A.City;
CROSS JOIN
Syntax
Select columns
From table1
Cross join table2;
Select columns
From table1
inner join table2
on table1.column_name = table2.column_name primary key
inner join table3
on table1.column_name = table3.column_name;
Group By clause:
Is used in conjunction with the SELECT statement and aggregate function to group rows
together by common column values.
Syntax
Select columns
From table_name
Where condition
Group by column_name(s);
OR
Select columns
From table1 INNER JOIN table2
On table1.column_name = table2.column_name
Where condition
Group by column_name(s);
Select columns
From table1 INNER JOIN table2
On table1.column_name = table2.column_name
Where condition
Group by column_name(s)
Having condition;
Example-
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;
Below is a selection from the "Products" table in the Northwind sample database:
2 Chang 1 1 24 - 12 oz bottles 19
The following SQL statement finds the price of the cheapest product:
Example
SELECT MIN(Price) AS SmallestPrice
FROM Products;
MAX() Example
The following SQL statement finds the price of the most expensive product:
Example
SELECT MAX(Price) AS LargestPrice
FROM Products;
MySQL COUNT(), AVG() and SUM() Functions
COUNT() Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE condition;
Below is a selection from the "Products" table in the Northwind sample database:
ProductID ProductName SupplierID CategoryID Unit Price
2 Chang 1 1 24 - 12 oz bottles 19
COUNT() Example
Example
SELECT COUNT(ProductID)
FROM Products;
AVG() Example
The following SQL statement finds the average price of all products:
Example
SELECT AVG(Price)
FROM Products;
Below is a selection from the "OrderDetails" table in the Northwind sample database:
OrderDetailID OrderID ProductID Quantity
1 10248 11 12
2 10248 42 10
3 10248 72 5
4 10249 14 9
5 10249 51 40
SUM() Example
The following SQL statement finds the sum of the "Quantity" fields in the "OrderDetails" table:
Example
SELECT SUM(Quantity)
FROM OrderDetails;
EXISTS Syntax
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
2 Chang 1 1 24 - 12 oz bottles 19
2 New Orleans Cajun Shelley Burke P.O. Box 78934 New 70117 USA
Delights Orleans
3 Grandma Kelly's Regina Murphy 707 Oxford Rd. Ann Arbor 48104 USA
Homestead
Examples
The following SQL statement returns TRUE and lists the suppliers with a product
price less than 20:
Example
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID
= Suppliers.supplierID AND Price < 20);
The following SQL statement returns TRUE and lists the suppliers with a product
price equal to 22:
Example
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.SupplierID
= Suppliers.supplierID AND Price = 22);