@Sarah
2015-12-01T04:50:20.000000Z
字数 9101
阅读 1018
SQL
The basic commands and functions of SQL
SQL for Data Declaration(DDL)
SQL for Data Management and Retrieval (DML)
Anatomy of an SQL Statement
physical design+ maintenance
Commands that define a database, including creating, altering, and dropping tables and establishing constraints
Define schema objects, primarily table structures and indexes.
◦ CREATE
◦ ALTER
◦ DROP
//Syntax
CREATE TABLE tablename (
column name1 datatype constraint,
column name 2 datatype constraint
);
SQL Constraints
- NOT NULL constraint
- Ensures that column does not accept nulls
- UNIQUE constraint
- Ensures that all values in column are unique
- DEFAULT constraint
- Assigns value to attribute when a new row is added to table
- CHECK constraint
- Validates data when attribute value is entered
//ALTER TABLE Syntax:
ALTER TABLE tablename ADD PRIMARY KEY(fieldname);
//For foreign key, use FOREIGN KEY in place of PRIMARY KEY
//Syntax:
ALTER TABLE tablename
ADD|MODIFY|RENAME COLUMN| DROP columnnames;
//Three options:
ADD //adds a column
MODIFY //changes column characteristics
DROP //deletes a column
//Can also be used to:
//Add table constraints
ALTER TABLE tablename ADD PRIMARY KEY(fieldname);
//Remove table constraints
ALTER TABLE emp DROP CONSTRAINT ck_bonus2;
//Syntax:
DROP TABLE tablename;
implementation
Commands that maintain and query a database
Manipulate schema objects, primarily table rows.
◦ INSERT
◦ SELECT
◦ COMMIT
◦ UPDATE
◦ ROLLBACK
◦ DELETE
Displaying table information
most powerful and complex statement
Nearly any question regarding who, what, when, what if and how may can be answered with SELECT.
Once you understand the SELECT statement you can apply it to UPDATE, INSERT, and DELETE statements.
SELECT statement is composed of several distinct keywords known as clauses. Each clause has one or more keywords.
There is no limit on the number of columns you can specify in the SELECT clause.
//Viewing a User’s Table Names
SELECT table_name FROM user_tables;
SELECT * FROM user_tables;
//View constraint information
SELECT * FROM user_constraints
SELECT constraint_name, constraint_type, table_name FROM USER_CONSTRAINTS
//Viewing Tablespace information
DESCRIBE user_tablespaces
SELECT * FROM USER_TABLESPACES
//Used to list contents of table
SELECT columnlist
FROM tablename;
//Columnlist represents one or more attributes, separated by commas
//Asterisk (*) can be used as wildcard character to list all columns
Fine-tune SELECT command by adding restrictions to search criteria using:
◦ Conditional restrictions
◦ Arithmetic operators
◦ Logical operators
◦ Special operators
Selecting Rows with Conditional Restrictions
Select partial table contents by placing restrictions on rows to be included in output
//Add conditional restrictions to SELECT statement, using WHERE clause
SELECT columnlist
FROM tablelist
[ WHERE conditionlist ] ;
Perform operations within parentheses
Perform power operations
Perform multiplications and divisions
Perform additions and subtractions
Logical operators work well in the query environment
SQL provides useful functions that:
Count
Find minimum and maximum values
Calculate averages, etc.
SQL allows user to limit queries to:
◦ Entries having no duplicates
◦ Entries whose duplicates may be grouped
Changes made to table contents are not physically saved on disk until:
◦ Database is closed
◦ Program is closed
◦ COMMIT command is used
//Syntax
COMMIT [WORK];
//Will permanently save any changes made to any table in the database
◦ Used to enter data into table
◦
//Syntax:
INSERT INTO columnname
VALUES (value1, value2, … , valueN);
When entering values, notice that:
◦ Row contents are entered between parentheses
◦ Character and date values are entered between apostrophes
◦ Numerical entries are not enclosed in apostrophes
◦ Attribute entries are separated by commas
◦ A value is required for each column
Use NULL for unknown values
Modify data in a table
//Syntax:
UPDATE tablename
SET columnname = expression [, columnname = expression]
[WHERE conditionlist];
Undoes changes since last COMMIT
Brings data back to prechange values
COMMIT and ROLLBACK only work with commands to add, modify, or delete table rows
//Syntax:
ROLLBACK;
Deletes a table row
//Syntax:
DELETE FROM tablename
[WHERE conditionlist ];
//WHERE condition is optional
BETWEEN: checks whether attribute value is within a range
IS NULL: checks whether attribute value is null
LIKE: checks whether attribute value matches given string pattern
IN: checks whether attribute value matches any value within a value list
EXISTS: checks if subquery returns any rows
ORDER BY clause is useful when listing order is important
//Syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[ORDER BY columnlist [ASC | DESC]];
Ascending order by default
DISTINCT clause produces list of only values that are different from one another
//Example:
SELECT DISTINCT V_CODE
FROM PRODUCT;
- Access places nulls at the top of the list
- Oracle places it at the bottom
- Placement of nulls does not affect list contents
Aggregate Functions
Frequency distributions created by GROUP BY clause within SELECT statement [在where后面 在order by 前面]
//Syntax:
SELECT columnlist
FROM tablelist
[WHERE conditionlist]
[GROUP BY columnlist]
[HAVING conditionlist]
[ORDER BY columnlist [ASC | DESC] ] ;
Alias is especially useful when a table must be joined to itself
Use aliases to differentiate the table from itself
1.SELECT(required)
◦ List the columns (and expressions) that should be returned from the query
2.FROM(required)
◦ Indicate the table(s) or view(s) from which data will be obtained
** 3.WHERE**
◦ Indicate the conditions under which a row will be included in the result
4.GROUP BY
◦ Used with aggregate functions to summarize data
5.HAVING
◦ Filters the result of aggregate functions in grouped information, similar to WHERE
6. ORDER BY
◦ Sorts the result according to specified criteria
implementation+ maintenance
Commands that control a database, including administering privileges and committing data
Terminology
Relation = table
Tuple or record = row
Attribute or field = column
Data type selection is usually dictated by nature of data and by intended use
Supported data types:
◦ Number(L,D), Integer, Smallint, Decimal(L,D)
◦ Char(L), Varchar(L), Varchar2(L)
◦ Date, Time, Timestamp
◦ Real, Double, Float
◦ Interval day to hour
◦ Many other types
◦ RDBMS specific
有用的ppt