DML QUERIES
DML QUERIES
1 Hansen Ola
2 Svendson Tove
3 Pettersen Kari
Orders Table
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()).
2 Chang 1 1 24 - 12 oz 19
bottles
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.