SQL Tutorial
|
|
|
|
| Articles Reviews Structured Query Language | |||||||||||||||||||||||||||
| Written by Bogdan V | |||||||||||||||||||||||||||
| Thursday, 14 September 2006 | |||||||||||||||||||||||||||
|
Page 14 of 17 {mos_sb_discuss:29} A primary key is used to uniquely identify each row in a table. It can either be part of the actual record itself , or it can be an artificial field (one that has nothing to do with the actual record). A primary key can consist of one or more fields on a table. When multiple fields are used as a primary key, they are called a composite key.
Primary keys can be specified either when the table is created (using
CREATE TABLE) or by changing the existing table structure (using ALTER
TABLE).
Below are examples for specifying a primary key when creating a table:
MySQL:
Oracle:
SQL Server:
Below are examples for specifying a primary key by altering a table:
MySQL:
Oracle:
SQL Server: Note: Before using the ALTER TABLE command to add a primary key, you'll need to make sure that the field is defined as 'NOT NULL' -- in other words, NULL cannot be an accepted value for that field. SQL FOREIGN KEY
For example, say we have two tables, a CUSTOMER table that includes all
customer data, and an ORDERS table that includes all customer orders.
The constraint here is that all orders must be associated with a
customer that is already in the CUSTOMER table. In this case, we will
place a foreign key on the ORDERS table and have it relate to the
primary key of the CUSTOMER table. This way, we can ensure that all
orders in the ORDERS table are related to a customer in the CUSTOMER
table. In other words, the ORDERS table cannot contain information on a
customer that is not in the CUSTOMER table.
The structure of these two tables will be as follows: Table CUSTOMER
Table ORDERS
In the above example, the Customer_SID column in the ORDERS table is a
foreign key pointing to the SID column in the CUSTOMER table.
Below we show examples of how to specify the foreign key when creating the ORDERS table:
MySQL:
Oracle:
SQL Server:
Below are examples for specifying a foreign key by altering a table.
This assumes that the ORDERS table has been created, and the foreign
key has not yet been put in:
MySQL:
Oracle:
SQL Server: SQL DROP TABLE Statement DROP TABLE "table_name" So, if we wanted to drop the table called customer that we created in the CREATE TABLE section, we simply type DROP TABLE customer. SQL TRUNCATE TABLE Statement TRUNCATE TABLE "table_name" So, if we wanted to truncate the table called customer that we created in SQL CREATE, we simply type, TRUNCATE TABLE customer SQL INSERT INTO Statement In the previous sections, we have seen how to retrieve information from tables. But how do these rows of data get into these tables in the first place? This is what this section, covering the INSERT statement, and next section, covering tbe UPDATE statement, are about. In SQL, there are essentially basically two ways to INSERT data into a table: One is to insert it one row at a time, the other is to insert multiple rows at a time. Let's first look at how we may INSERT data one row at a time: The syntax for inserting data into a table one row at a time is as follows:
INSERT INTO "table_name" ("column1", "column2", ...) Assuming that we have a table that has the following structure, Table Store_Information
and now we wish to insert one additional row into the table representing the sales data for Los Angeles on January 10, 1999. On that day, this store had $900 in sales. We will hence use the following SQL script:
INSERT INTO Store_Information (store_name, Sales, Date) The second type of INSERT INTO allows us to insert multiple rows into a table. Unlike the previous example, where we insert a single row by specifying its values for all columns, we now use a SELECT statement to specify the data that we want to insert into the table. If you are thinking whether this means that you are using information from another table, you are correct. The syntax is as follows:
INSERT INTO "table1" ("column1", "column2", ...) Note that this is the simplest form. The entire statement can easily contain WHERE, GROUP BY, and HAVING clauses, as well as table joins and aliases. So for example, if we wish to have a table, Store_Information, that collects the sales information for year 1998, and you already know that the source data resides in the Sales_Information table, we'll type in:
INSERT INTO Store_Information (store_name, Sales, Date)
Here I have used the SQL Server syntax to extract the year information
out of a date. Other relational databases will have different syntax.
For example, in Oracle, you will use to_char(date,'yyyy')=1998. |
|||||||||||||||||||||||||||
| Last Updated ( Saturday, 30 June 2007 ) | |||||||||||||||||||||||||||
| < Prev |
|---|







