0% found this document useful (0 votes)
8 views

DBMS

Dbms
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

DBMS

Dbms
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Different Constraints

• Unique Constraint
• Not Null Constraint
• Primary Key Constraint
• Default Constraint
• Check Constraint
• Foreign key Constraint
Create table employee
(
ecode int Primary Key,
ename varchar(20) Not Null,
gender char(1) Not Null,
grade char(2) Default ‘E1’,
gross decimal(8,2) check(gross>2000)
);
Create table employee
(
ecode int,
ename varchar(20),
gender char(1),
grade char(2) Default ‘E1’,
gross decimal(8,2)
Primary key (ecode, ename), Not Null (ename, gender),
check (gross>2000)
);
The ALTER TABLE statement is used
to add, delete, or modify columns in
an existing table.

The ALTER TABLE statement is also


used to add and drop various
constraints on an existing table.
Student
Admno Name Class Marks
23459 Vinay XH 456
20390 Soumaya XI A 440
26784 Ridhi XG 302
27891 Gurpreet IXA 467
ALTER TABLE - ADD Column
 To add a column
 To add an integrity Constraint

ALTER TABLE table_name


ADD column_name datatype;

ALTER TABLE table_name


ADD new_column_name datatype
[ FIRST | AFTER column_name ];
Alter table student
Add Percentage int;

Alter table student


Add Remarks varchar(30);

Alter table student


Add FatherName varchar(30)
After Name;
Alter table student
Add Percentage int check
(percentage<=100);
When adding new column + constraint

Alter table Person


Add primary key (ID);
Unique, primary key, referential
integrity & check
Using ADD clause
ALTER TABLE - MODIFY Column
 To change datatype
 To change size
 To change default value
 To change order of column
 To change column Name

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;
Alter table student
modify marks decimal(3,1);

Alter table accounts


modify bal default 0;

Alter table assignment


modify ProjectID int first;
ALTER TABLE Teacher
alter Salary DROP DEFAULT

ALTER TABLE Teacher


alter Salary SET DEFAULT 30000

Alter table accounts


Change bal Balance decimal(10,2);
ALTER TABLE - DROP Column
 To drop primary key, Foreign key constraint
 To remove column

ALTER TABLE table_name


DROP COLUMN column_name;
Alter table student
drop class;

ALTER TABLE Teacher


drop primary key;
Aggregate Functions
An aggregate function is a function that performs a
calculation on a set of values, and returns a single
value.
SQL aggregation function is used to perform the
calculations on multiple rows of a single column of a
table. It returns a single value.
Sometimes it is required to apply certain
mathematical functions on group of values in a
database. Such functions are called Aggregate
Functions.
MIN() - returns the smallest value within the
selected column

MAX() - returns the largest value within the


selected column

COUNT() - returns the number of rows in a set

SUM() - returns the total sum of a numerical


column

AVG() - returns the average value of a numerical


column
SELECT MIN(Price)
FROM Products;

SELECT MAX(Price)
FROM Products;

SELECT MIN(Price) AS SmallestPrice


FROM Products;

SELECT MIN(Price) AS SmallestPrice, CategoryID


FROM Products
GROUP BY CategoryID;
SELECT AVG(Price)
FROM Products;

SELECT AVG(Price) AS [average price]


FROM Products;

SELECT AVG(Price)
FROM Products
WHERE CategoryID = 1;

SELECT AVG(Price) AS AveragePrice, CategoryID


FROM Products
GROUP BY CategoryID;
SELECT COUNT(*)
FROM Products;

SELECT COUNT(ProductName)
FROM Products;

SELECT COUNT(ProductID)
FROM Products
WHERE Price > 20;
SELECT COUNT(DISTINCT Price)
FROM Products;

SELECT COUNT(*) AS [Number of records], CategoryID


FROM Products
GROUP BY CategoryID;
SELECT SUM(Quantity)
FROM OrderDetails;

SELECT SUM(Quantity) AS total


FROM OrderDetails;

SELECT SUM(Quantity * 10)


FROM OrderDetails;
SELECT SUM(Quantity)
FROM OrderDetails
WHERE ProductId = 11;

SELECT OrderID, SUM(Quantity) AS [Total


Quantity]
FROM OrderDetails
GROUP BY OrderID;
The GROUP BY statement groups rows that have the
same values into summary rows, like "find the
number of customers in each country"

The Group By statement combines all those records


that have identical values in a particular field or a
group of fields.

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.
 The SELECT statement is used with the GROUP
BY clause in the SQL query.

 WHERE clause is placed before the GROUP BY


clause in SQL.

 ORDER BY clause is placed after the GROUP BY


clause in SQL.
• The GROUP BY Clause is used to group the rows,
which have the same values.

• The SELECT statement in SQL is used with the


GROUP BY clause.

• In the Group BY clause, the SELECT statement can


use constants, aggregate functions, expressions,
and column names.

• The GROUP BY Clause is called when the HAVING


clause is used to reduce the results.
SQL HAVING Clause

The HAVING clause was added to SQL because the


WHERE keyword cannot be used with aggregate
functions.

The HAVING clause places the condition in the groups


defined by the GROUP BY clause in the SELECT
statement.
Employee
Ecode EName Gender Grade Gross
234 Vinay M A1 50000.45
203 Soumaya F E4 34000.32
267 Ridhi F B2 45000.23
278 Gurpreet M E4 29000.22

SELECT avg(Gross), sum(Gross)


FROM Employee Group by Gender
Having Gender = ’F’;
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
CREATE TABLE Department
(
Dept_ID INTEGER PRIMARY KEY,
Dept_Name VARCHAR(30) NOT NULL
);

CREATE TABLE Teacher


(
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER,
PRIMARY KEY (Teacher_ID),
FOREIGN KEY (Dept_No) REFERENCES Department (Dept_ID)
);
CREATE TABLE Department
(
Dept_ID INTEGER PRIMARY KEY,
Dept_Name VARCHAR(30) NOT NULL
);

CREATE TABLE Teacher


(
Teacher_ID INTEGER Primary key,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER FOREIGN KEY REFERENCES Department
(Dept_ID)
);
CREATE TABLE Department
(
Dept_ID INTEGER PRIMARY KEY,
Dept_Name VARCHAR(30) NOT NULL
);

CREATE TABLE Teacher


(
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER,
CONSTRAINT TEACHER_PK PRIMARY KEY (Teacher_ID),
CONSTRAINT TEACHER_FK FOREIGN KEY (Dept_No) REFERENCES
Department (Dept_ID)
);
SELECT First_Name, Last_Name, Dept_ID, Dept_Name
FROM Teacher, Department
WHERE Dept_ID=Dept_No;
SELECT First_Name, Last_Name, Dept_ID, Dept_Name
FROM Teacher t, Department d
WHERE t.Dept_ID=d.Dept_ID;
x, y = 2, 6
x, y = y, x+2
print(x, y)
Write a program to calculate in how many days a
work will be completed by three persons A, B and C
together. A, B, C take x days, y days and z days
respectively to do the job alone. The formula to
calculate the number of days if they work together is
xyz/(xy+yz+xz) days where x, y, and z are given as
input to the program.
Select 2+9 from dual;

Select 2+9 as sum from dual;

Select curdate() as Today from dual;

You might also like