SEE
SEE
SEE
Database
A database consists of one or more tables. A table is identified by its name. A
table is made up of columns and rows. Columns contain the column name and data
type. Rows contain the records or data for the columns.
Basic SQL
Each record has a unique identifier or primary key. SQL, which stands for
Structured Query Language, is used to communicate with a database. Through SQL one
can create and delete tables. Here are some commands:
= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal to
LIKE pattern matching operator
SELECT * FROM tablename
UPDATE tablename
SET colX = valX [, colY = valY, ...]
WHERE condition
DELETE
The DELETE command can be used to remove a record(s) from a table.
To delete all the records from a table without deleting the table do
DROP
To remove an entire table from the database use the DROP command.
AND / OR
AND and OR can join two or more conditions in a WHERE clause. AND will return data
when all the conditions are true. OR will return data when any one of the
conditions is true.
IN
IN operator is used when you know the exact value you want to return for at least
one of the columns
BETWEEN / AND
The BETWEEN ... AND operator selects a range of data between two values. These
values can be numbers, text, or dates.
JOIN
There are times when we need to collate data from two or more tables. That is
called a join. Tables in a database are related to each other through their keys.
We can associate data in various tables without repeating them. For example we
could have a table called Customers which could have information about customers
like their name, address, phone numbers. We could have another table called
Products that has information regarding the products like part number, product
name, manufacturer, number in stock, unit price. A third table called Orders could
have information regarding what product was ordered, by whom, the date the order
was placed, and quantity. Here are the tables:
Customers
Cust_ID FirstName LastName Address Phone
01 Mickey Mouse 123 Gouda St. 456-7890
02 Donald Duck 325 Eider Ln. 786-2365
Products
Part_No Name Manufacturer In_Stock Price
20-45 Hammer Stanley 57 3.50
21-68 ScrewDriver DeVries 84 2.75
Orders
Order_No Part_No Cust_ID Date Quantity
2005-27 21-68 02 31 Oct 2005 2
2005-34 20-45 01 02 Nov 2005 3
We can obtain information on who has ordered what:
This will select all the customers from USA and Asia but if there is a name that
occurs in both the tables it will return only one such name. To get all the names
use UNION ALL instead.
SQL Functions
There are several built-in functins in SQL. The basic function types are:
A view could be used from inside a query, a stored procedure, or from inside
another view. You can add functions and joins to a view and present the data you
want to the user.