© 2007 by Prentice Hall3-1 Introduction to Oracle 10g Chapter 3 Creating, Modifying, Renaming, and Deleting Database Tables James Perry and Gerald Post
© 2007 by Prentice Hall3-2 Chapter Outline Designing a Database Understanding Oracle User Accounts Further Instructions for Personal Oracle Users Creating Tables Defining and Using Constraints Altering a Table and Its Constraints Displaying Tables’ Names and Constraints Dropping, Reinstating, and Renaming Tables Creating Tables Based on Other Tables
© 2007 by Prentice Hall3-3 Table 3.1 Oracle character data types TypeOracle DesignationDescription FixedCHAR(size)Fixed-length character data of size characters padded with spaces. Maximum size is 2000 bytes. Fixed NationalNCHAR(size)Same as CHAR except stores National characters of maximum length 2000 bytes. VariableVARCHAR2(size)Variable-length character data of size characters. Maximum size is 4000 bytes. Variable NationalNVARCHAR2(size)Variable-length character data of size National characters. Maximum size is 4000 bytes. MemoLONGCharacter data of variable length up to 2 gigabytes. (Not recommended. Use CLOB data type instead.)
© 2007 by Prentice Hall3-4 Table 3.2 Oracle numeric data types TypeOracle DesignationDescription INT, INTEGER, SMALLINTNUMBER(38)An integer with up to 38 digits of precision. Fixed precisionNUMBER(p,s)A variable length number. Precision is the maximum number of digits, scale is the maximum number of digits to the right of the decimal point. FLOAT, DOUBLE PRECISION NUMBERA floating-point number with up to 38 digits of precision.
© 2007 by Prentice Hall3-5 Table 3.3 Oracle date and time data types TypeOracle DesignationDescription INT, INTEGER, SMALLINTNUMBER(38)An integer with up to 38 digits of precision. Fixed precisionNUMBER(p,s)A variable length number. Precision is the maximum number of digits, scale is the maximum number of digits to the right of the decimal point. FLOAT, DOUBLE PRECISION NUMBERA floating-point number with up to 38 digits of precision.
© 2007 by Prentice Hall3-6 Table 3.4 Oracle image data types Oracle DesignationDescription BLOBBinary LOB stores binary, unstructured data up to 128 terabytes. CLOBCharacter LOB stores character data for very large objects—up to 128 terabytes. NCLOBVariable length Unicode national character data up to 128 terabytes.
© 2007 by Prentice Hall3-7 Table 3.5 Customer column names and data types Column name Data typeMaximum length Special conditions CustomerIDIntegerprimary key FirstNameNational, variable length string30not null LastNameNational, variable length string30not null AddressNational, variable length string40 CityNational, variable length string30 StateNational, variable length string20 ZipcodeNational, variable length string20 HomePhoneNational, variable length string20 CellPhoneNational, variable length string20 WorkPhoneNational, variable length string20
© 2007 by Prentice Hall3-8 Table 3.6 Constraint prefixes and example constraint names Constraint TypeConstraint Type PrefixExample Constraint Name CHECKckck_customers_zipcode FOREIGN KEYfkfk_customers_properties NOT NULLnnnn_agents_lastname PRIMARY KEYpkpk_agentid UNIQUEunun_contactreason_description
© 2007 by Prentice Hall3-9 Table 3.7 ContactReason, data types, and constraint Column nameData typeLengthConstraint ContactReasonNVARCHAR215PRIMARY KEY DescriptionNVARCHAR250
© 2007 by Prentice Hall3-10 Table 3.8 Structure of the Agents table Column nameData typeLengthConstraint AgentIDINTEGERPRIMARY KEY FirstNameNVARCHAR230NOT NULL LastNameNVARCHAR230NOT NULL HireDateDATE BirthDateDATE GenderNVARCHAR210Only allowed values are ‘M’ and ‘F’ WorkPhoneNVARCHAR220 CellPhoneNVARCHAR220UNIQUE HomePhoneNVARCHAR220 TitleNVARCHAR220 TaxIDNVARCHAR220 LicenseIDNVARCHAR220 LicenseDateDATE LicenseExpireDATE LicenseStatusIDINTEGER
© 2007 by Prentice Hall3-11 Table 3.9 Selected objects available through data dictionary views ObjectDescription CONS_COLUMNSTable columns having constraints CONSTRAINTSTable constraints INDEXESTable indexes OBJECTSAll database objects SEQUENCESSequences generating unique keys TAB_COLUMNSTable columns. (USER_TAB_COLUMNS are the current user’s columns) TABLESDatabase tables USERSNames of all database users (ALL_USERS) VIEWSDatabase views
© 2007 by Prentice Hall3-12 Table 3.10 Some of the user constraints columns ColumnDescription ownerOwner of the constraint (username). constraint_nameName of the constraint. constraint_typeType: P, R, C, U, V or O. table_nameTable containing the constraint. statusEither ENABLED or DISABLED.
© 2007 by Prentice Hall3-13 Table 3.11 Customer columns and their constraints Column NameData TypeLengthConstraint CustomerIDIntegerPrimary key; default 0 CompanyNameStringVariable up to 50NOT NULL CityStringVariable up to 50NOT NULL NationStringVariable up to 50(none) ContactIDInteger(none) BaseCurrencyStringVariable up to 50(none)
© 2007 by Prentice Hall Representing tables by listing their names and their column names Customers(CustomerID, FirstName, LastName, Address, City, State, Zipcode, HomePhone, CellPhone, WorkPhone) Listings(ListingID, PropertyID, ListingAgentID, SaleStatusID, BeginListDate, EndListDate, AskingPrice) SaleStatus(SaleStatusID, StatusText)
© 2007 by Prentice Hall Simplified initial design of a Customers real estate table Customers(CustomerID, FirstName, LastName, …, PropertyAddress, SqFt, Bedrooms, …) seller informationproperty information
© 2007 by Prentice Hall Attempting to log in with a new user account having no privileges
© 2007 by Prentice Hall Changing another user’s password changing a password to Columbus Logging in and changing the password to a new one
© 2007 by Prentice Hall Removing a user
© 2007 by Prentice Hall Examining selected Oracle data types
© 2007 by Prentice Hall Creating a table using the SQL*Plus interface SQL statement to create Customers table. Oracle built the table this way.
© 2007 by Prentice Hall Creating and displaying a table comment
© 2007 by Prentice Hall Column comment example comment inserted into the State column
© 2007 by Prentice Hall Displaying NOT NULL constraints using the DESCRIBE statement NOT NULL in the Null? column
© 2007 by Prentice Hall Defining a primary key type this SQL command
© 2007 by Prentice Hall Creating the Agents table with constraints
© 2007 by Prentice Hall Designating default values for selected table columns
© 2007 by Prentice Hall Tables with foreign key references to each other
© 2007 by Prentice Hall Adding constraints with the ALTER TABLE statement
© 2007 by Prentice Hall Adding a column to a table newly added ContactDate column
© 2007 by Prentice Hall Dropping a column from a table
© 2007 by Prentice Hall Marking multiple columns unused columns about to be marked “unused”
© 2007 by Prentice Hall Describing the Listings table
© 2007 by Prentice Hall Displaying table names using a data dictionary view names of tables you created
© 2007 by Prentice Hall Some of the columns in the user_tables view
© 2007 by Prentice Hall Displaying details about a table’s columns
© 2007 by Prentice Hall Listing your table constraints
© 2007 by Prentice Hall Reviewing column constraints for the Listings table
© 2007 by Prentice Hall Displaying more complete constraint information
© 2007 by Prentice Hall Seven rows showing schema SYS’ table comments
© 2007 by Prentice Hall DROP TABLE attempt with resulting error message
© 2007 by Prentice Hall Reinstating a previously dropped table
© 2007 by Prentice Hall Creating a table from another table