Академический Документы
Профессиональный Документы
Культура Документы
Table Store_Information
Table Geography
Region_Name Store_Name
East Boston
We want to find out sales by store, and we only want to see stores with sales listed in the report. To do
this, we can use the following SQL statement using INNER JOIN:
FROM Geography A1
ON A1.Store_Name = A2.Store_Name
GROUP BY A1.Store_Name;
Result:
STORE SALES
Los Angeles 1800
San Diego 250
Boston 700
Syntax
Examples
Let's look at the example. Assuming our starting point is the Customer table
created in the CREATE TABLE section:
Table Customer
MySQL:
Oracle:
SQL Server:
Table Customer
Column Name Data Type
First_Name char(50)
Last_Name char(50)
Address char(50)
City char(50)
Country char(25)
Birth_Date datetime
Gender char(1)
Note that the new column Gender becomes the last column in
the Customer table.
MySQL:
Oracle:
SQL Server:
Table Customer
The INSERT INTO statement is used to add new records into a database
table.
In SQL, there are 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. In this
section, we'll take a look at the first case.
Syntax
The syntax for inserting data into a table one row at a time is as follows:
Examples
Table Store_Information
We want 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, and the Manager_ID for this store is 10. We will use the following SQL
script:
INSERT INTO Store_Information (Store_Name, Manager_ID, Sales, Txn_Date)
VALUES ('Los Angeles', 10, 900, 'Jan-10-1999');
Table Store_Information
Please note that we can specify the column names in any order -- the order
does not have to be the same as that of the table. For example, the following
SQL statement is equivalent to the SQL statement above:
If we leave off the column names in the INSERT INTO statement, we will need
to make sure that data is inserted in the same column order as that in the
table. For example,
will result in Store_Name being set to 900, Manager_ID being set to 'Los
Angeles', and Sales being set to 10. Clearly this is not what we intend to
accomplish.
In the first two examples, we insert a value for every column in the table.
Sometimes, we may decide to insert value into some of the columns and
leave the rest of the columns blank. For those cases, we simply specify the
column names that we want to insert values into in our SQL statement. Below
is an example:
INSERT INTO Store_Information (Store_Name, Sales, Txn_Date)
VALUES ('New York', 500, 'Jan-10-1999');
Table Store_Information
In this case, the value for the Manager_ID column in the second row
is NULL. NULL means that data does not exist, and we discuss the concept
of NULL later in this tutorial.
EXAMPLE Exercise :
1. Let's assume we start with the Store_Information table shown above. What
does the table look like after the following SQL statement is executed?
INSERT INTO Store_Information VALUES ('San Jose',25,700,'Jan-10-1999');
2. Continuing with Question 1. What does the table look like after the following
SQL statement is executed?
INSERT INTO Store_Information (Manager_ID,Txn_Date,Sales) VALUES
(10,600,'Jan-11-1999');
3. Using the same table as above. What does the table look like after the
following SQL statement is executed?
INSERT INTO Store_Information ('Portland',30,650,'Jan-11-1999');
There is no change to the table as the SQL statement is not valid (it is missing
'VALUES').
Syntax
UPDATE "table_name"
SET "column_1" = [new value]
WHERE "condition";
UPDATE "table_name"
SET column_1 = [value1], column_2 = [value2], ...
WHERE "condition";
Examples
Table Store_Information
We notice that the sales for Los Angeles on Jan-08-1999 is actually $500
instead of $300, and that particular entry needs to be updated. To do so, we
use the following SQL query:
UPDATE Store_Information
SET Sales = 500
WHERE Store_Name = 'Los Angeles'
AND Txn_Date = 'Jan-08-1999';
Table Store_Information
Store_Name Sales Txn_Date
Los Angeles 1500 Jan-05-1999
San Diego 250 Jan-07-1999
Los Angeles 500 Jan-08-1999
Boston 700 Jan-08-1999
In this case, there is only one row that satisfies the condition in
the WHERE clause. If there are multiple rows that satisfy the condition, all of
them will be modified. If no WHERE clause is specified, all rows will be
modified.
We notice that the 'San Diego' entry has the wrong Sales and TXN_Date
information. To fix it, we run the following SQL statement:
UPDATE Store_Information
SET Sales = 600, Txn_Date = 'Jan-15-1999'
WHERE Store_Name = 'San Diego';
Table Store_Information
1. Using the same Store_Information table right above, what data is in the
table after the following SQL statement is executed?
UPDATE Store_Information
SET Sales = 800
WHERE Store_Name = 'Boston';
2. Continuing to use the same table. What is the content of the table after the
following SQL statement is executed?
UPDATE Store_Information
SET Sales = 2000
WHERE Store_Name = 'Los Angeles' AND Txn_Date = 'Jan-10-1999';
3. Again using the same table. What is the content of the table after the
following SQL statement is executed?
UPDATE Store_Information
SET Sales = 1000;
Ans :
1. Table Store_Information
2. Table Store_Information
3. Table Store_Information
Please note that the DELETE FROM command cannot delete any rows of
data that would violate FOREIGN KEY or other constraints.
Syntax
"Condition" can be simple (such as "Sales > 500") or complex (such as from
the result of a subquery).
Examples
Two examples of how to use the DELETE FROM statement are shown below.
Table Store_Information
Table Store_Information
Table Geography
Region_Name Store_Name
East Boston
East New York
West Los Angeles
West San Diego
Exercises
For the questions below, we use the following table as the starting point:
Table Clients
1. Which of the following SQL statements is valid? (There may be more than
one answer)
a) DELETE * FROM Clients WHERE State = 'CO';
b) DELETE FROM Clients WHERE State = 'CO';
c) DELETE FROM Clients HAVING State = 'CO';
d) DELETE FROM Clients WHERE Customer_ID < 10;
2. How many rows are deleted after the following SQL statement is
executed?
DELETE FROM Clients WHERE State = 'CO';
1. b), d)
2. 1 row.
3. All rows are deleted from the table. This is because the condition in
the WHERE clause is true for all rows.
o add a column to a table using SQL, we specify that we want to change the
table structure via the ALTER TABLEcommand, followed by
the ADD command to tell the RDBMS that we want to add a column.
Syntax
Examples
Let's look at the example. Assuming our starting point is the Customer table
created in the CREATE TABLE section:
Table Customer
MySQL:
Oracle:
SQL Server:
Table Customer
Note that the new column Gender becomes the last column in
the Customer table.
MySQL:
Oracle:
SQL Server:
Table Customer
Let's look at the example. Assuming our starting point is the Customer table
created in the CREATE TABLE section:
Table Customer
Our goal is to alter the data type of the "Address" column to char(100). To do
this, we key in:
MySQL:
Oracle:
SQL Server:
In MySQL, the SQL syntax for ALTER TABLE Rename Column is,
Let's look at the example. Assuming our starting point is the Customer table
created in the CREATE TABLE section:
Table Customer
MySQL:
Oracle:
SQL Server:
It is not possible to rename a column using the ALTER TABLE statement in
SQL Server. Use sp_rename instead.
The resulting table structure is:
Table Customer
In Oracle and SQL Server, the syntax for ALTER TABLE Drop Column is,
Let's look at the example. Assuming our starting point is the Customer table
created in the CREATE TABLE section:
Table Customer
MySQL:
SQL Server:
Oracle:
ALTER TABLE Customer DROP COLUMN Birth_Date;
Table Customer
By default, a column can hold NULL. If you do not want to allow NULL value
in a column, you will want to place the NOT NULL constraint on this column.
The NOT NULL constraint specifies that NULL is not an allowable value.
will result in an error because this will lead to column "SID" being NULL,
which violates the NOT NULL constraint on that column.
DEFAULT Constraint
Even though we didn't specify a value for the "Score" column in the INSERT
INTO statement, it does get assigned the default value of 80 since we had
already set 80 as the default value for this column.
UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are distinct.
column "SID" has a UNIQUE constraint, and hence cannot include duplicate
values. Such constraint does not hold for columns "Last_Name" and
"First_Name". So, if the table already contains the following rows:
will result in an error because '3' already exists in the SID column, thus trying
to insert another row with that value violates the UNIQUE constraint.
Please note that a column that is specified as a primary key must also be
unique. At the same time, a column that's unique may or may not be a primary
key. In addition, multiple UNIQUE constraints can be defined on a table.