sql1 PDF
sql1 PDF
Spring 2005
Introduction to SQL
CUS_INITIAL CHAR(1),
CUS_AREACODE CHAR(3) DEFAULT ‘615’ NOT NULL
CHECK (CUS_AREACODE IN (‘615’, ‘713’, ‘931’)),
CUS_PHONE CHAR(8) NOT NULL,
CUS_BALANCE NUMBER(9,2) DEFAULT 0.00,
CONSTRAINT CUS_UI1 UNIQUE (CUS_LNAME, CUS_FNAME));
UPDATE tablename
SET columnname = expression [, columnname = expression ]
[ WHERE conditionlist ];
COMMIT [ tablename ];
-or-
COMMIT; //saves all changes made in any modified tables
• The inner query is always executed first by the RDBMS and the values
extracted by the inner query will be used as input to the outer query (in
this case the INSERT command). The values returned by the inner
query must match the attributes and data types of the table in the
INSERT statement.
FROM PRODUCT
WHERE V_CODE <> 21344;
• The PRODUCT table is shown on the next page and the output from
this query is shown on the following page. Notice that rows 10 and 13
in the PRODUCT table do not appear in the results of this query.
These two rows do not appear in the result on the following page.
• Note that in Access the delimiters for dates is #, so in Access this query would
look like:
SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE, P_INDATE
FROM PRODUCT
WHERE P_INDATE >= #20-Jan-2004#;
The computed
column with its
alias.
Oracle Version
SELECT P_CODE, P_INDATE, SYSDATE – 90 AS CUTDATE
FROM PRODUCT
WHERE P_INDATE <= SYSDATE – 90;
SELECT P_DESCRIPT,
P_INDATE,
P_PRICE,
V_CODE
FROM PRODUCT
WHERE
V_CODE = 21344
OR
V_CODE = 24288;
Access wildcard
• We’ll look more closely at the IN operator later when we deal more in
depth with subqueries.
UPDATE PRODUCT_2
SET P_SALECODE = ‘2’
WHERE P_CODE = ‘1546-QQ2’;
UPDATE PRODUCT_2
SET P_SALECODE = ‘1’
WHERE P_CODE IN (‘2232/QWE’, ‘2232/QTY’);
UPDATE PRODUCT
SET P_SALECODE = ‘1’
WHERE P_INDATE >= ’16-Jan-2004’
AND P_INDATE <= ’10-Feb-2004’;
UPDATE PRODUCT
SET P_ONHAND = P_ONHAND + 20
WHERE P__CODE = ‘2232/QWE/;
UPDATE PRODUCT
SET P_PRICE = P_PRICE * 1.10
WHERE P__PRICE < 50.00;
• Note that the PART column names need not be identical to those in
the original table, and that the new table need not have the same
number of columns as the original table.
– In this case, the first column in the PART table is PART_CODE, rather
than the original P_CODE found in the PRODUCT table. And the
PART table contains only three columns, rather than the seven columns
found in the PRODUCT table.
– However, column characteristics must match: you cannot copy a
character-based attribute into a numeric structure, and vice versa.
SELECT *
FROM PART;
Results of copying
parts of a table with
renaming of the
columns.
• Quite aside from the fact that the integrity rules are not
automatically transferred to a new table that derives its data
from one or more other tables, there are several other
scenarios that would leave you without entity and referential
integrity enforcement.
• For example, you might have simply forgotten to define the
primary and foreign keys when you created the tables.
• The integrity rules can be reestablished via the ALTER
command as shown below:
ALTER TABLE PRODUCT
ADD PRIMARY KEY(P_CODE)
ADD FOREIGN KEY(V_CODE) REFERENCES VENDOR;