|
MBA 669 E-Business Technology --
Syllabus Classes References Student Info Guest Info
|
Oracle Reference>> under construction Oracle 8i Server 8.1.5 Documentation is available at http://hojohn.photo.net/ora81/DOC/ Data ModelingSee the Data Modeling presentation, example, Greenspun page 313, and http://photo.net/sql/data-modeling.html ?? SQL NamesNames of tables and columns in SQL are subject to the following rules:
We will use lower case table and column names. SQL Data TypesNote, we are only using a subset of Oracle datatypes. This section explains what we are using and gives a summary of other types. Character Strings:
There are other character string types including:
String functions:
Dates:
Mask format elements for to_char and to_date include: 'D' day of week from 1-7, 'DAY' name of day, 'DD' day of month, 'DDD' day of year, 'DY' abbreviated day name, 'HH' hours, 'HH24' hours in military time, 'MI' minutes, 'MM' month number, 'MON' abbreviated month, 'MONTH' full month, 'YYYY' year, etc. Round(date, format) will round dates according to the specified format; Trunc(date, format) will truncate date values. Format values include: cc century, q quarter, yyyy or year year, month , ww the nearest Monday, etc. For more information:
Numeric Types:
Oracle stores all numeric datatypes (Decimal, Integer, Float, Double Precision,
and Real) are as Number. Monetary amounts in dollars and cents would be In many cases Oracle will convert between numbers and strings automatically. When you want to control conversion and format use:
Mask format elements include '$' currency symbol, '.' decimal symbol, ',' comma, '9' numeric value or space, '0' numeric value or zero, 'S' sign character. SQL Round can be used to control the length of calculated values. Use round(value, precision) in your Select statement. Precision may be 0 to round to an integer, positive to specify the number of places to the right of the decimal, or negative to round to a number of places to the left of the decimal. For example, to get a value in dollars and cents specify
State ValuesState values can be represented as character strings that are constrained to have one of a defined set of values. >> fix up example
SequenceA Sequence is a way of creating unique identifier values that are increasing but not necessarily sequential values. See Greenspun pg 467, http://photo.net/wtr/thebook/case-studies.html, and http://www.photo.net/sql/transactions.html (at end of section). Sequences are different from tables but are declared and named in a similar way. Define a sequence in SQL Plus with a create statement. For example:
Sequence operators include:
Sequences are used within SQL statements. For example:
The following TCL code sets seq_id to the next value of the sequence usf_seq (using the dummy table named dual):
The following code sets curr_id to the current value of the sequence usf_seq (using the dummy table named dual):
The sequence usf_seq has been predefined for class use and does not require a prefix when referenced from within TCL code. You may create your own sequence as follows:
DualOracle provides a dummy table named dual that can be used in queries. For example:
PredicateSince Oracle does not have Boolean data type, we will use our own Predicate type to represent true/false values. Note that we specify a default value so that we do not have to be concerned with Null values for predicates. foo_p char(1) default 'f' (check foo_p in ('t', 'f')) NullNull is not really a data type, it is the value returned for attributes that have not had any value specified. In other words, Null is the value of an undefined attribute. Numeric calculations including a Null value will return Null, that is no data. For numeric columns, use the Oracle NVL function to substitute a default value, like 0, for Null. For example:
When testing for Null you must use 'is null' like this:
The use of =null will not work:
Database DesignSQL ViewsSQL Views are like tables but the data comes from other tables. View may be used in queries but not updated. If you find that you have a complex query you use frequently, or have trouble mixing aggregate functions with other values, consider creating a View. Views are created, dropped, and shared (Grant) like tables. For example:
RownumRownum is a psuedo column provided by Oracle that gives you the row number of a record returned by a query. You can:
Note: values of Rownum are assigned BEFORE rows are arranged by any ORDER BY or GROUP BY statement. In order to get row numbers in sequence, create a View and then use Rownum when selecting from the view. For example to get the first 10 names:
Constraints Primary Key IndexA SQL Index speeds up retrieval of rows in certain conditions and forces values to be unique. In some conditions it may not be possible to change the value stored in a column that is used as an index. In general, we don't need to be concerned with making index columns. An example of index creation is:
Interactive SQLIn order to use Interactive SQL you have to log onto the server using Telnet. There are two ways to login to SQLPlus, normally you will log in automatically (sqlplus /). In some cases you must login by name. It is important that you type Login AutomaticallyTo use SQL Plus with the same user-name as you logged into Linux with, type sqlplus / You will not have to enter user-name or password. Login by nameFollow these instructions to login with a specified user name that may be different from your Linux account name. Type sqlplus Enter user-name: enter your user name as supplied. Names are case sensitive. Enter password: enter your password exactly as given to you. Note: your password will not be displayed and passwords are case sensitive.
Type exit to log off. To see your Oracle data, start SQL Plus and use these commands:
Interactive SQL CommandsCreate Table Drop Table Deletes a table and all associated data. Alter Table Use Alter Table to add or drop columns
Dropping a column. SQL> alter table s drop (q); Insert Select Use a where clause to specify values for a select and join (Greenspun 339, http://photo.net/sql/queries.html ) Search for key words contained in a column using
Like and % to match zero or more characters. For example this
select with find Elvis Presley, Elvis Costello, and Dancing Elvises:
Delete Update Column Formatting You can display only 10 characters of a varchar column by typing the following command for each column:
This only effects the display and will not change your data. Longer values may be wrapped and displayed on multiple lines. If you don't want wrapping type:
CommitThe Commit command makes database changes permanent and closes the current transaction. Up until the point at which you commit, you can use Rollback to undo your changes. SQL> commit; RollbackYou can undo all the changes you have made since the last Commit command or the start of your SQL Plus session with the Rollback command. SQL> rollback; Count select count(*) from users; Check ?? Query Hints (Greenspun p 336) Import Data Export Data Upload (Greenspun p 512 does not work) Show what tables, views, synonyms and sequences you may access: select * from cat; Show 'your' tables and columns: select table_name, column_name from cols; select table_name from user_tables order by table_name; describe some_table; Multiple Users and GrantEach Oracle developer has their own set of data definitions, called a schema. This avoids problems with two users creating tables with the same name. Tables defined in one schema are not visible to other developers until they are explicitly shared using the Grant command. Shared tables are referenced as schema.table, or user.table. For example, TABLE chuck.person. You will want to share tables with your team mates and with user AD, the AOL server. A separate Grant statement is required for each table. You may Grant ALL Privileges or only specific privileges such as SELECT, DELETE, INSERT, UPDATE, or REFERENCES. Sharing privileges may be Granted to specific users (by name) or to all users (PUBLIC). Privileges may be Granted with Grant Option which allows the user receiving the privileges to Grant them to others. The Revoke command can be used to reverse the effect of a Grant. Examples: GRANT ALL PRIVILEGES ON TABLE person TO AD; GRANT SELECT, INSERT, UPDATE ON person TO PUBLIC; GRANT DELETE ON person TO chuck, geoff WITH GRANT OPTION; REVOKE DELETE ON TABLE person FROM AD; Database ProgrammingBegin/End Transaction Additional Oracle ResourcesFor more information see:
|
|
© 2000 by Chuck Ehrlich, all rights reserved. Comments to webmaster. Updated on April 27, 2000. |