Tài liệu creating tables docx

40 430 0
Tài liệu creating tables docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Creating Tables 9 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć2 Schedule: Timing Topic 45 minutes Lecture 25 minutes Practice 70 minutes Total Class Management Note: Files required for this lesson are: Demonstration: None Practice: p9q2.sql, p9q3.sql Creating Tables 9Ć3 Objectives In this lesson, you will create tables. You will also build integrity constraints, which are rules governing what can and cannot be done with the data. At the end of this lesson, you should be able to D Create a table containing integrity constraints. D Identify table naming conventions. D Describe the datatypes that can be used when specifying column definitions. D Recognize the indexes that are created automatically by constraints. D Create a table by populating it with rows from another table. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć4 Creating Tables 9Ć5 Overview An Oracle7 database can contain multiple data structures. Each structure should be outlined in the database design so that it can be created during the build stage of database development. Data Structures Structure Description Table Stores data. View Logically represents subsets of data from one or more tables. Sequence Generates primary key values. Index Improves the performance of some queries. Summary of Oracle7 Table Structures D Tables can be created at any time, even while users are using the database. D You do not need to specify the size of any table. The size is ultimately defined by the amount of space allocated to the database as a whole. It is important, however, to estimate how much space a table will use over time. D Table structure can be modified online. Class Management Note: You should be familiar with the full CREATE TABLE syntax. There are many parameters that are useful to know, such as PCTINCREASE and INITIAL. Technical Note: Tables can have up to 254 columns and must conform to standard database object naming conventions. Column definitions can be omitted when using the AS subquery clause. Tables are created without data unless a query is specified. Rows are usually added by using INSERT statements, SQL*Loader, or through a form. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć6 Creating Tables 9Ć7 Creating Tables Create tables to store data by executing the SQL CREATE TABLE command. This command is one of the data definition language (DDL) commands, which you will cover in the next several lessons. DDL commands are a subset of SQL commands used to create, modify, or remove Oracle7 database structures. These commands have an immediate effect on the database, and they also record information in the data dictionary. In order to create a table, a user must have the CREATE TABLE privilege and a storage area in which to create objects. The database administrator uses data control language (DCL) commands, which are covered in a later lesson, to grant privileges to users. Abridged Syntax CREATE TABLE [schema.]table (column datatype [DEFAULT expr][column_constraint], . [table_constraint]); where: schema is the same as the owner’s name. table is the name of the table. DEFAULT expr specifies a default value if a value is omitted in the INSERT statement. column is the name of the column. datatype is the column’s datatype and length. column_constraint is an integrity constraint as part of the column definition. table_constraint is an integrity constraint as part of the table definition. For more information, see Oracle7 Server SQL Reference, Release 7.3, “CREATE TABLE.” Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć8 Creating Tables 9Ć9 Creating Tables continued Referencing Another User's Tables A schema is a collection of objects. Schema objects are the logical structures that directly refer to the data in a database. Schema objects include tables, views, synonyms, sequences, stored procedures, indexes, clusters, and database links. The tables referenced in a constraint must exist in the same database. If the table does not belong to the user creating the constraint, the owner’s name must be prefixed to the table referenced in the constraint. The DEFAULT Option A column can be given a default value by using the DEFAULT option. This option prevents null values from entering the columns if a row is inserted without a value for the column. The default value can be a literal, an expression, or SQL function, such as SYSDATE and USER, but the value cannot be the name of another column or a pseudocolumn, such as NEXTVAL or CURRVAL. The default expression must match the datatype of the column. Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć10 [...]... that the Oracle7 Server does not enforce the constraint and simply documents it Enable the constraint by using the ENABLE clause Creating Tables 9Ć23 9Ć24 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Creating a Table from a Table Instance Chart Create your tables, based on the table instance chart from the database design, by using the CREATE TABLE syntax You might find it useful to... department name and region number does not appear in the table more than once Creating Tables 9Ć27 Class Management Note: Tell the students that the manager number constraint will be added by way of the ALTER TABLE command in the “Altering Tables and Constraints” lesson 9Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Creating a Table from a Table Instance Chart continued Example Create.. .Creating Tables continued Naming Rules Name database tables and columns according to the standard rules for naming any Oracle7 database object D Table names and column names must begin with a letter and can be 1–30 characters long... S_DEPT.REGION_ID can reference the primary key S_REGION.ID And, the S_EMP table must be created after the S_DEPT table so that S_EMP.DEPT_ID can reference S_DEPT.ID Creating Tables 9Ć25 9Ć26 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Creating a Table from a Table Instance Chart continued Example Create the S_DEPT database table based on the table instance chart SQL> CREATE TABLE s_dept 2... using double quotation marks, for example, CREATE TABLE “emp” To reference that table, use double quotation marks, for example SELECT * FROM “emp” Double quotation marks when creating table names are not recommended Creating Tables 9Ć11 9Ć12 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Oracle7 Datatypes There are many different column types The Oracle7 Server can treat values of one... Ensures that the S_EMP table does not contain department number not already stored in the S_DEPT table S_EMP_COMMISSION_ PCT_CK Restricts commission percentages Creating Tables 9Ć29 9Ć30 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Creating a Table from Rows in Another Table A second method to create a table is to apply the AS subquery clause to both create the table and insert rows returned... NUMBER(4,2) Note: Only the NOT NULL constraint is identified in the DESCRIBE command All constraints can be viewed in the data dictionary Creating Tables 9Ć33 9Ć34 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Summary The Oracle7 Server stores data in tables Create a table by using the SQL CREATE TABLE command You can create a table based on the definition of another table by using the... are based upon data values and are purely logical, not physical, pointers A foreign key that is part of a primary key cannot be a null value because no part of a primary key can be NULL Continued Creating Tables 9Ć21 9Ć22 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Constraints continued The FOREIGN KEY ConstraintĊcontinued The foreign key is defined in the child table, and the table... name of another object owned by the same Oracle7 Server user D Names must not be an Oracle7 Server reserved words Naming Guidelines D Use descriptive names for tables and other database objects D Name the same entity consistently in different tables For example, the department number column is called DEPT_ID in both the S_EMP table and the S_REGION table Note: Names are case-insensitive For example,... can specify the size for NUMBER and CHAR columns, but default values are available (38 for NUMBER and 1 for CHAR) For more information, see Oracle7 Server SQL Reference, Release 7.3, “Datatypes.” Creating Tables 9Ć13 Class Management Note: A guideline of a constraint naming convention is: __ For example, S_EMP_LAST_NAME_NN and S_DEPT_ID_PK 9Ć14 Introduction . to Oracle: SQL and PL/SQL Using Procedure Builder9Ć6 Creating Tables 9Ć7 Creating Tables Create tables to store data by executing the SQL CREATE TABLE. and PL/SQL Using Procedure Builder9Ć8 Creating Tables 9Ć9 Creating Tables continued Referencing Another User's Tables A schema is a collection of objects.

Ngày đăng: 10/12/2013, 17:15

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan