Database
Database
FirstName varchar(255),
Age int
);
MySQL:
FirstName varchar(255),
Age int,
UNIQUE (ID)
);
To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
FirstName varchar(255),
Age int,
);
To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
);
CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int,
CONSTRAINT oit UNIQUE (ID,LastName) )
insert into persons values(101,'abc','xyz',23)
insert into persons values(102,'abc','xyz',23)
ALTER TABLE Persons DROP CONSTRAINT oit
CREATE TABLE Persons1 ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255),
Age int CHECK (Age>=18) )
insert into persons1 values(123,'abc','xyz',26)
ALTER TABLE Persons1 add CONSTRAINT CHECK (Age>=1 and Age<=125)
CREATE TABLE Persons2 ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age in
t, City varchar(255), CONSTRAINT CHK_Person CHECK (Age>=18 AND City='Sandnes' or City=
'Pune' ) )
FirstName varchar(255),
Age int,
);
CREATE TABLE Persons3 ( Personid int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NU
LL, FirstName varchar(255), Age int, PRIMARY KEY (Personid) )
insert into persons3(LastName,FirstName,Age) values('sam','nigade',8)
SELECT * FROM `persons3`
insert into persons3(LastName,FirstName,Age) values('Ram','Shah',34)
SELECT * FROM persons3
Update Dateofjoining of employee to ‘15/06/2019’ whose name is “Mr. Roy’ and belongs to mumbai
city.
The SELECT DISTINCT statement is used to return only distinct (different) values.
Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct)
values.
FROM table_name;
SELECT productname,price
FROM Products
WHERE Price BETWEEN 5 AND 30;
WHERE CustomerName LIKE Finds any values that start with "a"
'a%'
WHERE CustomerName LIKE Finds any values that end with "a"
'%a'
WHERE CustomerName LIKE Finds any values that have "or" in any position
'%or%'
WHERE CustomerName LIKE Finds any values that have "r" in the second position
'_r%'
WHERE CustomerName LIKE Finds any values that start with "a" and are at least 2
'a_%' characters in length
WHERE CustomerName LIKE Finds any values that start with "a" and are at least 3
'a__%' characters in length
WHERE ContactName LIKE 'a% Finds any values that start with "a" and ends with "o"
o'
NOT Example
The following SQL statement selects all fields from "Customers" where
country is NOT "Germany":
Example
SELECT *
FROM Customers
where customerid>=60 ORDER BY customerid desc;
SELECT *
FROM Customers
where customerid>=60 and city like 's%' order by customerid ;
SELECT *
SELECT *
FROM Customers
LIMIT 3;
SELECT MIN(Price)
FROM Products;
MAX() Example
The following SQL statement finds the price of the most expensive product:
Example
FROM Products;
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;
SQL Aliases
SQL aliases are used to give a table, or a column in a table, a temporary name.
Aliases are often used to make column names more readable.
UNION Syntax
SELECT column_name(s) FROM table1
UNION
UNION ALL
GROUP BY Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Ex
SELECT count(CustomerID), Country
FROM Customers
GROUP BY Country
having CustomerID>=50 ;
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Add column phone_No into Emp table with data type int.
Display the name of department whose location is “Pune” and “Mr. Advait” is working in it
Display the names of employees whose salary is greater than 50000 and department is
“Quality”.
Update Dateofjoining of employee to ‘15/06/2019’ whose department is ‘computer science’ and name is
“Mr. Roy’.
1. Add column amount into sales_amt table with data type float.
2. Delete the details of the clients whose names start with ‘A’ character.
3. Delete sales order details of client whose name is “Patil” and order date is “09/08/2019”.
Consider the following entities and their relationships. Create a RDB in 3 NF with appropriate data types and Constraints. [15
Marks]
Hospital (hno ,hname , city, Est_year, addr)
Doctor (dno , dname , addr, Speciality,salary)
The relationship between Hospital and Doctor is one - to – Many Constraints: - Primary Key, Est_year should be greater than
1990.
2. Display doctor name, Hospital name and specialty of doctors from “Pune City” .
3. Display the names of the hospitals which are located at “Pimpri” city.
4. Display the names of doctors who are working in “Birla” Hospital and city name is “Chinchwad”
5. Display the specialty of the doctors who are working in “Ruby” hospital.
6. Give the count of doctor’s hospital wise which are located at “Pimple Gurav”.
8. display doctor details whose speciality is Heart Surgen and whole belong to hospital from pune
Q3. Consider the following entities and their relationships. Create a RDB in 3 NF with appropriate data types and Constraints.
Project (pno, pname, start_date, budget, status) 2
Department (dno, dname, HOD, loc) 1
The relationship between Project and Department is Many to One. Constraint: Primary key. Project Status Constraints:
C – Completed,
P - Progressive,
I – Incomplete
2. Display the details of project whose start_date is before one month and status is “Progressive”
3. Display the names of project and department who are worked on projects whose status is ‘Completed’
8.Display project details where project name start with A and whose budget is >10000 in descending order
Create a RDB in 3 NF with appropriate data types and Constraints. [15 Marks]
Customer (cust_no, cust_name, address, city)
Loan (loan_no, loan_amt)
The relationship between Customer and Loan is Many to Many Constraint:
Primary key, loan_amt should be > 0.