Tài liệu creating tables pptx

40 220 0
Tài liệu creating tables pptx

Đ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 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. 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 [...]... 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, EMP is treated as the same name as eMP or eMp For more information, see Oracle7 Server SQL Reference, Release 7.3, “Object Names and Qualifiers.” Creating Tables. .. table-constraint level The constraint syntax can apply to any column in the table, not only on the column on which it is defined 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.. .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... table constraint if the constraint constitutes more than one column 5 Map the UNIQUE, CHECK, and FOREIGN KEY constraints 6 Save and execute the script file 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... REGION_ID column as a composite unique key to ensure that the same combination of department name and region number does not appear in the table more than once Creating Tables 9Ć27 9Ć28 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Creating a Table from a Table Instance Chart continued Example Create the S_EMP database table based on the table instance chart Also, enforce the business... 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... 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 9Ć14 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Constraints Constraints have been available with the Oracle7 Server since Version 6 In Oracle7, constraints are enforced... relationship between the column and a column of the referenced table CHECK Specifies a condition that must be true For more information, see Oracle7 Server SQL Reference, Release 7.3, “CONSTRAINT clause.” Creating Tables 9Ć15 9Ć16 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Constraints continued Constraint Naming Conventions All constraints are stored in the data dictionary Constraints are... constraints except NOT NULL SyntaxĊColumnĆConstraint Level column [CONSTRAINT constraint_name] constraint_type, SyntaxĊTableĆConstraint Level column, [CONSTRAINT constraint_name] constraint_type (column, ), Creating Tables 9Ć17 9Ć18 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder Constraints continued The NOT NULL Constraint The NOT NULL constraint ensures that null values are not allowed in the . Creating Tables 9 Introduction to Oracle: SQL and PL/SQL Using Procedure Builder9Ć2 Creating Tables 9Ć3 Objectives In this lesson, you will create tables. . 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

Ngày đăng: 21/12/2013, 06:17

Từ khóa liên quan

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

Tài liệu liên quan