Download SQL Summary

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Database wikipedia , lookup

Information privacy law wikipedia , lookup

Predictive analytics wikipedia , lookup

Rainbow table wikipedia , lookup

Data vault modeling wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Clusterpoint wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Transcript
SQL Summary
SQL contains commands to create tables, to update tables, and to retrieve data from tables.
The commands that are used to retrieve data from tables are often called queries.
Note: All of the examples in this summary are based on the Premiere Database.
Commands:
The SQL command CREATE TABLE is used to describe the layout of a table. For each field,
you must enter the type of data, and the size of field that you are allocating. The SalesRep table
already exists in the PREMIERE database, but if you were to create it, you would write the
following statement:
CREATE TABLE SalesRep
(Sales_Rep_Number CHAR(2),
Last_Name CHAR(15),
First_Name CHAR(15),
Street CHAR(15),
City CHAR(15),
State CHAR(2),
Zip_Code CHAR(5),
Commission DECIMAL(7,2),
Rate DECIMAL(3,2) )
Simple Retrieval in SQL involves the use of a basic expression SELECT – FROM – and
follows the following format:
After SELECT, list the fields that you want to display in the order that you want them listed.
After FROM, list the table or tables involved in the query.
After an optional WHERE, list the conditions that apply to the data you want to retrieve.
EG1. List the number, first name, last name, and balance for all customers (you don’t need the
WHERE part in this query because there are no restrictions)
SELECT customer_Number, First_Name, Last_Name, Balance
FROM CUSTOMER
EG2. List the complete PARTS table (again, no WHERE because there are no restrictions)
SELECT *
FROM PARTS
EG3. Write the first and last name of all customers who have a credit limit of $1000.
SELECT First_Name, Last_Name
FROM CUSTOMER
WHERE Credit_Limit = 1000
EG4. Find the customer name for customer number 124
SELECT First_Name, Last_Name
FROM CUSTOMER
WHERE Customer_Number =’124’
(note that customer number 124 is enclosed in quotes to show that it is a character field)
We can also create queries with compound conditions.
EG5. List the descriptions of all parts in warehouse 3 that have more than 100 units on hand.
SELECT Part_Description, On_Hand
FROM PARTS
WHERE warehouse = ‘3’ AND On_Hand > 100
There are also queries that use computed fields.
EG6. Find the available credit of all customers who began with a credit limit of at least $1500.
(need to display the value of available credit that is not stored in the customer table, but it can be
calculated)
SELECT Customer_Number, Last_Name, First_Name, (Credit_Limit – Balance) AS
Available_Credit
FROM CUSTOMER
WHERE Credit_Limit >= 1500
EG7. List the number, first name, last name, credit limit and balance of all customers. Order the
customers by balance within credit limit.
SELECT Customer_Number, First_Name, Last_Name, Credit_Limit, Balance
FROM CUSTOMER
ORDER BY Credit_Limit, Balance
JOINING TABLES
When queries require data to be retrieved from more than one table, it is necessary to join tables;
that is, to find rows in two or more tables that have identical values in matching fields. In SQL,
this is done through appropriate conditions in the WHERE clause.
EG8. List the number, last name and first name of the customer together with the name (last and
first) of the salesrep who represents the customer.
SELECT Customer.Customer_Number, Customer.Last_Name, Customer.FirstName,
SalesRep.Sales_Rep_Number, SalesRep.Last_Name, SalesRep.First_Name
FROM CUSTOMER, SALESREP
WHERE CUSTOMER.Sales_Rep_Number = SALESREP.Sales_Rep_Number
UPDATING TABLES
SQL has more uses than simply retrieving data from a database. It has several more capabilities
including the ability to update a database as demonstrated by the example below:
EG9. Add a salesrep to the SalesRep table. Her number is 14. Her name is Ann Crane, and her
address is 123 River, Ada, MI, 42411. So far, she has not earned any commission. Her
commission rate is 5 percent (0.05).
Addition of new data is accomplished through the INSERT command. After the words INSERT
INTO, you list the name of the TABLE, followed by the word VALUES. You then list the
values for each of the columns as shown below…
INSERT INTO SALESREP
VALUES (‘14’, ‘Crane’, ‘Ann’, ‘123 River’, ‘Ada’, ‘MI’, ‘42411’, 0.00, 0.05)
VIEWS
Most relational databases support the concept of a view. A view is basically a snapshot of
certain data in the database at a given point in time, and can be used in reports, charts etc. A
view is usually much less involved than a full database, and represents a simplification of the
data in addition to providing some degree of security by omitting sensitive tables or columns.
EG10. Produce a view of the housewares class of parts from the Parts table by listing the part
number, part description, units on hand and unit price.
CREATE VIEW Housewares AS
SELECT Part_Number, Part_Description, On_Hand, Price
FROM PART
WHERE Class = ‘HW’
Integrity Rules
There are 2 integrity rules –
1) entity integrity is the rule that no column that is part of the Primary Key may accept null
values. Setting a column value to null is the same as not filling in that column at all.
This is not the same as entering a 0 that is considered to be a value.
It is therefore possible in a table like Customer to set a null value for the Credit Limit
column to infer that the value is not known.
2) referential integrity is the rule that states that if Table A contains a foreign key that
matches the primary key of Table B, then values of this foreign key must match the value
of the primary key for some row or be null.
Changing the Structure of a Database Table
ALTER TABLE is the command that will allow you to change a table’s structure. Suppose that
you want to maintain a customer type for each customer in the PREMIERE database. This
would require the addition of a column in which each customer would be defined as R for
regular, D for distributor or S for special.
This would mean that from this point on, each new customer added to the Premiere database
could also be described using Customer_Type. However, for the previous customers, there are 2
approaches:
1) Customer_Type value assigned to existing rows can be NULL:
ALTER TABLE CUSTOMER
ADD COLUMN Customer_Type CHAR(1)
2) Assign a default value of ‘R’ to existing customers and later change the values where
appropriate:
ALTER TABLE CUSTOMER
ADD COLUMN Customer_Type CHAR (1) DEFAULT = ‘R’
To delete a column in a table (e.g., to delete the Warehouse column from the Parts table)
ALTER TABLE Parts
DROP COLUMN Warehouse
To delete an entire table, use the DROP command. Thus, if the SALESREP table were no longer
needed, the command would be:
DROP TABLE Salesrep
Information about the database tables are kept in the system catalogue. This catalogue stores
information about a database’s structure. For example, SYSCOLUMNS contains information
about the columns in the tables in the catalogue.
Integrity in SQL
The current standard for SQL provides support for data integrity. This means that data of certain
types must meet certain conditions.
There are 3 types of integrity support:
1) Legal Values - a Check clause ensures that only data which satisfy a particular condition
are allowed in a particular column
2) Primary Keys – the PK for a table is specified by following the data type for the PK
column with the words Primary Key in the CREATE TABLE command.
3) Foreign Keys – an FK is specified through a foreign key clause in which you specify
both the column that is the foreign key in one table and the name of the table whose
primary key it is to match.
Thus, if you were to write the CREATE TABLE statement for the CUSTOMER table
invoking integrity rules, you would write the following:
CREATE TABLE CUSTOMER
(Customer_Number CHAR (4),
Last_Name CHAR (15),
First_Name CHAR(15),
Street CHAR (15),
City CHAR (15),
State CHAR (2),
Zip_Code CHAR (5),
Credit_Limit DECIMAL (7,2),
Sales_Rep_Number CHAR (2),
CONSTRAINT Credit_Limit_CK CHECK Credit_Limit IN (750,1000,1500,2000),
CONSTRAINT Customer_PK PRIMARY KEY(Customer_Number),
CONSTRAINT Sales_Rep_Number_FK FOREIGN KEY(Sales_Rep_Number)
REFERENCES SalesRep(Sales_Rep_Number)