0% found this document useful (0 votes)
5 views5 pages

DML QUERIES

Uploaded by

hamsadineshkumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

DML QUERIES

Uploaded by

hamsadineshkumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

SQL Constraint

The FOREIGN KEY constraint is used to prevent actions that would


destroy links between tables.
A FOREIGN KEY is a field (or collection of fields) in one table, that
refers to the PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table
with the primary key is called the referenced or parent table.
Look at the following two tables:
Persons Table

PersonID LastName FirstName

1 Hansen Ola

2 Svendson Tove

3 Pettersen Kari

Orders Table

OrderID OrderNumber PersonID

1 77895 3

2 44678 3

3 22456 2

4 24562 1

Notice that the "PersonID" column in the "Orders" table points to the
"PersonID" column in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY
KEY in the "Persons" table.
The "PersonID" column in the "Orders" table is a FOREIGN KEY in
the "Orders" table.
The FOREIGN KEY constraint prevents invalid data from being
inserted into the foreign key column, because it has to be one of the
values contained in the parent table.
The UNIQUE constraint ensures that all values in a column are
different.
The NOT NULL constraint enforces a column to NOT accept NULL
values. This enforces a field to always contain a value, which means
that you cannot insert a new record, or update a record without adding
a value to this field.
CREATE TABLE Orders (
OrderID int NOT NULL PRIMARY KEY,
OrderNumber int ,
PersonID int FOREIGN KEY REFERENCES Persons(PersonID)
);
ALTER TABLE orders MODIFY ordernumber int NOT NULL
UNIQUE;
SQL CHECK Constraint
The CHECK constraint is used to limit the value range that can be
placed in a column.
If you define a CHECK constraint on a column it will allow only
certain values for this column.
If you define a CHECK constraint on a table it can limit the values in
certain columns based on values in other columns in the row.
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int CHECK (Age>=18)
);
The SQL IN Operator
The IN operator allows you to specify multiple values in
a WHERE clause.
The IN operator is a shorthand for multiple OR conditions.
SELECT * FROM Customers
WHERE Country IN ('Germany', 'France', 'UK');
SELECT * FROM Customers
WHERE Country NOT IN ('Germany', 'France', 'UK');
SQL Aggregate Functions
An aggregate function is a function that performs a calculation on a
set of values, and returns a single value.
Aggregate functions are often used with the GROUP BY clause of
the SELECT statement. The GROUP BY clause splits the result-set
into groups of values and the aggregate function can be used to return
a single value for each group.
The most commonly used SQL aggregate functions are:
 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
Aggregate functions ignore null values (except for COUNT()).

ProductI ProductName SupplierID CategoryID Unit Price


D
1 Chais 1 1 10 boxes x 20 18
bags

2 Chang 1 1 24 - 12 oz 19
bottles

3 Aniseed Syrup 1 2 12 - 550 ml 10


bottles

4 Chef Anton's Cajun 2 2 48 - 6 oz jars 22


Seasoning

5 Chef Anton's 2 2 36 boxes 21.35


Gumbo Mix

1) SELECT MIN(Price)
FROM Products;
2) SELECT MAX(Price)
FROM Products;
3) SELECT COUNT(*)
FROM Products; /*COUNTS ALL INCLUDING NULL*/
4) SELECT COUNT(ProductName)
FROM Products; /*COUNTS ALL NOT NULL PRODUCT
NAMES*/
5) SELECT SUM(Quantity)
FROM OrderDetails;
6) SELECT AVG(Price)
FROM Products;
7) SELECT AVG(Price)
FROM Products
WHERE CategoryID = 1;

SQL JOIN
A JOIN clause is used to combine rows from two or more tables,
based on a related column between them.

You might also like