Column1 Datatype, Column2 Datatype, Column3 Datatype,: Alter
Column1 Datatype, Column2 Datatype, Column3 Datatype,: Alter
The ALTER TABLE statement is also used to add and drop various constraints on
an existing table.
To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):
To change the data type of a column in a table, use the following syntax:
Constraints can be specified when the table is created with the CREATE TABLE statement, or after the
table is created with the ALTER TABLE statement.
SQL Constraints
SQL constraints are used to specify rules for the data in a table.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and
reliability of the data in the table. If there is any violation between the constraint and the data action,
the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and table
level constraints apply to the whole table.
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table
INDEX - Used to create and retrieve data from the database very quickly
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.
The following SQL ensures that the "ID", "LastName", and "FirstName" columns
will NOT accept NULL values:
EXAMPLE :
Tip: If the table has already been created, you can add a NOT NULL constraint
to a column with the ALTER TABLEstatement.
SQL UNIQUE Constraint on CREATE TABLE
The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons" table is created:
SQL
The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is created:
SELECT Syntax
Here, column1, column2, ... are the field names of the table you want to select
data from. If you want to select all the fields available in the table, use the
following syntax:
The following SQL statement selects all the columns from the "Customers"
table:
Example
SELECT * FROM Customers;
Inside a table, a column often contains many duplicate values; and sometimes
you only want to list the different (distinct) values.
SELECT Example
The following SQL statement selects all (and duplicate) values from the
Example
SELECT Country FROM Customers;
Example
SELECT DISTINCT Country FROM Customers;
The following SQL statement lists the number of different (distinct) customer
countries:
Example
SELECT COUNT(DISTINCT Country) FROM Customers;
The SQL WHERE Clause
The WHERE clause is used to filter records.
The WHERE clause is used to extract only those records that fulfill a specified
condition.
WHERE Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example
Example
SELECT * FROM Customers
WHERE CustomerID=1;
The AND and OR operators are used to filter records based on more than one
condition:
The AND operator displays a record if all the conditions separated by AND
is TRUE.
The OR operator displays a record if any of the conditions separated by
OR is TRUE.
AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
AND Example
The following SQL statement selects all fields from "Customers" where country
is "Germany" AND city is "Berlin":
Example
Example
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
NOT Example
The following SQL statement selects all fields from "Customers" where country
is NOT "Germany":
Example
The following SQL statement selects all fields from "Customers" where country
is "Germany" AND city must be "Berlin" OR "München" (use parenthesis to form
complex expressions):
Example
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');
The following SQL statement selects all fields from "Customers" where country
is NOT "Germany" and NOT "USA":
Example
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';
The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
ORDER BY Example
The following SQL statement selects all customers from the "Customers" table,
Example
SELECT * FROM Customers
ORDER BY Country;
Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;
Example
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;
Note: Be careful when updating records in a table! Notice the WHERE clause in
the UPDATE statement. The WHERE clause specifies which record(s) that should
be updated. If you omit the WHERE clause, all records in the table will be
updated!
UPDATE Table
The following SQL statement updates the first customer (CustomerID = 1) with
a new contact person and a new city.
Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;
The following SQL statement will update the contactname to "Juan" for all
records where country is "Mexico":
Example
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';
****Be careful when updating records. If you omit the WHERE clause, ALL
records will be updated!
Example
UPDATE Customers
SET ContactName='Juan';
Example
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';
or: