SQL Data Types
November 12th, 2010 by aBisona
Data types and ranges for Microsoft Access, MySQL and SQL Server.
Microsoft Access Data Types
Data type
Description
Storage
Text
Use for text or combinations of text and numbers. 255 characters maximum
Memo
Memo is used for larger amounts of text. Stores up to 65,536 characters. Note: You cannot sort a memo field. However, they are searchable
Byte
Allows whole numbers from 0 to 255
1...SQL NULL Functions
November 12th, 2010 by aBisona
SQL ISNULL(), NVL(), IFNULL() and COALESCE() Functions
Look at the following “Products” table:
P_Id
ProductName
UnitPrice
UnitsInStock
UnitsOnOrder
1
Jarlsberg
10.45
16
15
2
Mascarpone
32.56
23
3
Gorgonzola
15.67
9
20
Suppose that the “UnitsOnOrder” column is optional, and may contain NULL values.
We have the following SELECT statement:
SELECT ProductName,UnitPrice*(UnitsInStock+UnitsOnOrder)
FROM...SQL NULL Values
November 12th, 2010 by aBisona
NULL values represent missing unknown data.
By default, a table column can hold NULL values.
This chapter will explain the IS NULL and IS NOT NULL operators.
SQL NULL Values
If a column in a table is optional, we can insert a new record or update an existing record without adding a value to this column. This means that the field will be saved with a NULL value.
NULL values are treated differently...SQL Date Functions
November 12th, 2010 by aBisona
SQL Dates
The most difficult part when working with dates is to be sure that the format of the date you are trying to insert, matches the format of the date column in the database.
As long as your data contains only the date portion, your queries will work as expected. However, if a time portion is involved, it gets complicated.
Before talking about the complications of querying for dates, we will...SQL Views
November 12th, 2010 by aBisona
A view is a virtual table.
This chapter shows how to create, update, and delete a view.
SQL CREATE VIEW Statement
In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and ...SQL AUTO INCREMENT Field
November 12th, 2010 by aBisona
Auto-increment allows a unique number to be generated when a new record is inserted into a table.
AUTO INCREMENT a Field
Very often we would like the value of the primary key field to be created automatically every time a new record is inserted.
We would like to create an auto-increment field in a table.
Syntax for MySQL
The following SQL statement defines the “P_Id” column to be an auto-increment...SQL ALTER TABLE Statement
November 12th, 2010 by aBisona
The ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
SQL ALTER TABLE Syntax
To add a column in a table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To delete a column in a table, use the following syntax (notice that some database systems don’t allow deleting a column):
ALTER TABLE table_name
DROP COLUMN...SQL DROP INDEX, DROP TABLE, and DROP DATABASE
November 12th, 2010 by aBisona
Indexes, tables, and databases can easily be deleted/removed with the DROP statement.
The DROP INDEX Statement
The DROP INDEX statement is used to delete an index in a table.
DROP INDEX Syntax for MS Access:
DROP INDEX index_name ON table_name
DROP INDEX Syntax for MS SQL Server:
DROP INDEX table_name.index_name
DROP INDEX Syntax for DB2/Oracle:
DROP INDEX index_name
DROP INDEX Syntax for MySQL:
ALTER...SQL CREATE INDEX Statement
November 12th, 2010 by aBisona
The CREATE INDEX statement is used to create indexes in tables.
Indexes allow the database application to find data fast; without reading the whole table.
Indexes
An index can be created in a table to find data more quickly and efficiently.
The users cannot see the indexes, they are just used to speed up searches/queries.
Note: Updating a table with indexes takes more time than updating a table without...SQL DEFAULT Constraint
November 12th, 2010 by aBisona
SQL DEFAULT Constraint
The DEFAULT constraint is used to insert a default value into a column.
The default value will be added to all new records, if no other value is specified.
SQL DEFAULT Constraint on CREATE TABLE
The following SQL creates a DEFAULT constraint on the “City” column when the “Persons” table is created:
My SQL / SQL Server / Oracle / MS Access:
CREATE TABLE...SQL CHECK Constraint
November 12th, 2010 by aBisona
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 single column it allows 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.
SQL CHECK Constraint on CREATE TABLE
The following SQL creates...SQL FOREIGN KEY Constraint
November 12th, 2010 by aBisona
SQL FOREIGN KEY Constraint
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
Let’s illustrate the foreign key with an example. Look at the following two tables:
The “Persons” table:
P_Id
LastName
FirstName
Address
City
1
Hansen
Ola
Timoteivn 10
Sandnes
2
Svendson
Tove
Borgvn 23
Sandnes
3
Pettersen
Kari
Storgt 20
Stavanger
The “Orders” table:
O_Id
OrderNo
P_Id
1
77895
3
2
44678
3
3
22456
2
4
24562
1
Note...
