SQL FOR QA
Sunday, January 23, 2011
Foreign key
The foreign key identifies a column or a set of columns in one (referencing) table that refers to a set of columns in another (referenced) table. The columns in the referencing table must be the primary key or other candidate key in the referenced table.
Tuesday, January 11, 2011
Difference between primary key and unique key
Primary key allows each row in a table to be uniquely identified and ensures that no duplicate rows exist and no null values are entered.
Unique key constraint is used to prevent the duplication of key values within the rows of a table and allow null values. (In oracle, one null is not equal to another null).
Unique key constraint is used to prevent the duplication of key values within the rows of a table and allow null values. (In oracle, one null is not equal to another null).
Saturday, July 3, 2010
SQL WHERE Clause
The WHERE clause is used to filter records.
Now we want to select only the persons living in the city "Sandnes" from the table above.
We use the following SELECT statement:
The result-set will look like this:
Although, numeric values should not be enclosed in quotes.
For text values:
For numeric values:
SQL WHERE Syntax
| SELECT column_name(s) FROM table_name WHERE column_name operator value |
WHERE Clause Example
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 |
We use the following SELECT statement:
| SELECT * FROM Persons WHERE City='Sandnes' |
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Hansen | Ola | Timoteivn 10 | Sandnes |
| 2 | Svendson | Tove | Borgvn 23 | Sandnes |
Quotes Around Text Fields
SQL uses single quotes around text values (most database systems will also accept double quotes).Although, numeric values should not be enclosed in quotes.
For text values:
| This is correct: SELECT * FROM Persons WHERE FirstName='Tove' This is wrong: SELECT * FROM Persons WHERE FirstName=Tove |
| This is correct: SELECT * FROM Persons WHERE Year=1965 This is wrong: SELECT * FROM Persons WHERE Year='1965' |
Operators Allowed in the WHERE Clause
With the WHERE clause, the following operators can be used:| Operator | Description |
|---|---|
| = | Equal |
| <> | Not equal |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal |
| <= | Less than or equal |
| BETWEEN | Between an inclusive range |
| LIKE | Search for a pattern |
| IN | If you know the exact value you want to return for at least one of the columns |
The SQL SELECT Statement
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
FROM table_name
and
SELECT * FROM table_name
Now we want to select the content of the columns named "LastName" and "FirstName" from the table above.
We use the following SELECT statement:
The result-set will look like this:
We use the following SELECT statement:
Tip: The asterisk (*) is a quick way of selecting all columns!
The result-set will look like this:
The result is stored in a result table, called the result-set.
SQL SELECT Syntax
SELECT column_name(s)FROM table_name
and
SELECT * FROM table_name
An SQL SELECT Example
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 |
We use the following SELECT statement:
| SELECT LastName,FirstName FROM Persons |
| LastName | FirstName |
|---|---|
| Hansen | Ola |
| Svendson | Tove |
| Pettersen | Kari |
SELECT * Example
Now we want to select all the columns from the "Persons" table.We use the following SELECT statement:
| SELECT * FROM Persons |
The result-set will look like this:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Hansen | Ola | Timoteivn 10 | Sandnes |
| 2 | Svendson | Tove | Borgvn 23 | Sandnes |
| 3 | Pettersen | Kari | Storgt 20 | Stavanger |
DML and DDL
SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL).
The query and update commands form the DML part of SQL:
The query and update commands form the DML part of SQL:
- SELECT - extracts data from a database
- UPDATE - updates data in a database
- DELETE - deletes data from a database
- INSERT INTO - inserts new data into a database
- CREATE DATABASE - creates a new database
- ALTER DATABASE - modifies a database
- CREATE TABLE - creates a new table
- ALTER TABLE - modifies a table
- DROP TABLE - deletes a table
- CREATE INDEX - creates an index (search key)
- DROP INDEX - deletes an index
RDBMS
RDBMS stands for Relational Database Management System.
The data in RDBMS is stored in database objects called tables.
The data in RDBMS is stored in database objects called tables.
Friday, July 2, 2010
Subscribe to:
Comments (Atom)