26.oracleocp題庫dba經(jīng)典輔助學(xué)習(xí)les_第1頁
26.oracleocp題庫dba經(jīng)典輔助學(xué)習(xí)les_第2頁
26.oracleocp題庫dba經(jīng)典輔助學(xué)習(xí)les_第3頁
26.oracleocp題庫dba經(jīng)典輔助學(xué)習(xí)les_第4頁
26.oracleocp題庫dba經(jīng)典輔助學(xué)習(xí)les_第5頁
已閱讀5頁,還剩35頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、Using DDL Statementsto Create and Manage Tables ObjectivesAfter completing this lesson, you should be able to do the following:Categorize the main database objectsReview the table structureList the data types that are available for columnsCreate a simple tableUnderstand how constraints are created a

2、t the time of table creationDescribe how schema objects workDatabase ObjectsObjectDescriptionTableBasic unit of storage; composed of rows View Logically represents subsets of data from one or more tables Sequence Generates numeric valuesIndexImproves the performance of some queriesSynonym Gives alte

3、rnative names to objectsNaming RulesTable names and column names:Must begin with a letterMust be 130 characters longMust contain only AZ, az, 09, _, $, and #Must not duplicate the name of another object owned by the same userMust not be an Oracle server reserved wordYou must have:CREATE TABLE privil

4、egeA storage areaYou specify:Table nameColumn name, column data type, and column sizeCREATE TABLE StatementCREATE TABLE schema.table (column datatype DEFAULT expr, .);Referencing Another Users TablesTables belonging to other users are not in the users schema.You should use the owners name as a prefi

5、x to those tables.USERBUSERASELECT * FROM userB.employees;SELECT * FROM userA.employees;Specify a default value for a column during an insert.Literal values, expressions, or SQL functions are legal values.Another columns name or a pseudocolumn are illegal values.The default data type must match the

6、column data type.DEFAULT Option. hire_date DATE DEFAULT SYSDATE, . CREATE TABLE hire_dates (id NUMBER(8), hire_date DATE DEFAULT SYSDATE);Table created.Creating TablesCreate the table.Confirm table creation.DESCRIBE deptCREATE TABLE dept (deptno NUMBER(2), dname VARCHAR2(14), loc VARCHAR2(13), creat

7、e_date DATE DEFAULT SYSDATE);Table created.Data TypesData TypeDescriptionVARCHAR2(size)Variable-length character dataCHAR(size) Fixed-length character dataNUMBER(p,s) Variable-length numeric dataDATE Date and time valuesLONG Variable-length character data (up to 2 GB)CLOBCharacter data (up to 4 GB)R

8、AW and LONG RAW Raw binary dataBLOBBinary data (up to 4 GB)BFILEBinary data stored in an external file (up to 4 GB)ROWIDA base-64 number system representing the unique address of a row in its tableDatetime Data TypesYou can use several datetime data types:Data Type DescriptionTIMESTAMPDate with frac

9、tional secondsINTERVAL YEAR TO MONTHStored as an interval of yearsand monthsINTERVAL DAY TO SECONDStored as an interval of days, hours, minutes, and secondsDatetime Data TypesThe TIMESTAMP data type is an extension of the DATE data type.It stores the year, month, and day of the DATE data type plus h

10、our, minute, and second values as well as the fractional second value.You can optionally specify the time zone.TIMESTAMP(fractional_seconds_precision)TIMESTAMP(fractional_seconds_precision)WITH TIME ZONETIMESTAMP(fractional_seconds_precision)WITH LOCAL TIME ZONEDatetime Data TypesThe INTERVAL YEAR T

11、O MONTH data type stores a period of time using the YEAR and MONTH datetime fields: The INTERVAL DAY TO SECOND data type stores a period of time in terms of days, hours, minutes, and seconds:INTERVAL YEAR (year_precision) TO MONTHINTERVAL DAY (day_precision) TO SECOND (fractional_seconds_precision)I

12、NTERVAL DAY TO SECOND Data TypeINTERVAL DAY TO SECOND stores a period of time in terms of days, hours, minutes, and seconds.INTERVAL 4 5:12:10.222 DAY TO SECOND(3)Indicates 4 days, 5 hours, 12 minutes, 10 seconds, and 222 thousandths of a second.INTERVAL 4 5:12 DAY TO MINUTEIndicates 4 days, 5 hours

13、 and 12 minutes.INTERVAL 400 5 DAY(3) TO HOURIndicates 400 days 5 hours.INTERVAL 11:12:10.2222222 HOUR TO SECOND(7)indicates 11 hours, 12 minutes, and 10.2222222 seconds.Including ConstraintsConstraints enforce rules at the table level.Constraints prevent the deletion of a table if there are depende

14、ncies.The following constraint types are valid:NOT NULLUNIQUE PRIMARY KEYFOREIGN KEYCHECKConstraint GuidelinesYou can name a constraint, or the Oracle server generates a name by using the SYS_Cn format.Create a constraint at either of the following times:At the same time as the table is createdAfter

15、 the table has been createdDefine a constraint at the column or table level.View a constraint in the data dictionary.Defining ConstraintsSyntax:Column-level constraint:Table-level constraint:CREATE TABLE schema.table (column datatype DEFAULT expr column_constraint, . table_constraint,.);column,. CON

16、STRAINT constraint_name constraint_type (column, .),column CONSTRAINT constraint_name constraint_type,Defining ConstraintsColumn-level constraint: Table-level constraint:CREATE TABLE employees( employee_id NUMBER(6) CONSTRAINT emp_emp_id_pk PRIMARY KEY, first_name VARCHAR2(20), .);CREATE TABLE emplo

17、yees( employee_id NUMBER(6), first_name VARCHAR2(20), . job_id VARCHAR2(10) NOT NULL, CONSTRAINT emp_emp_id_pk PRIMARY KEY (EMPLOYEE_ID);12NOT NULL ConstraintEnsures that null values are not permitted for the column:NOT NULL constraint(No row can containa null value forthis column.)Absence of NOT NU

18、LL constraint (Any row can contain a null value for this column.)NOT NULL constraintUNIQUE ConstraintEMPLOYEES UNIQUE constraintINSERT INTONot allowed: already existsAllowedUNIQUE ConstraintDefined at either the table level or the column level: CREATE TABLE employees( employee_id NUMBER(6), last_nam

19、e VARCHAR2(25) NOT NULL, email VARCHAR2(25), salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE NOT NULL,. CONSTRAINT emp_email_uk UNIQUE(email);PRIMARY KEY ConstraintDEPARTMENTS PRIMARY KEYINSERT INTONot allowed(null value)Not allowed (50 already exists)FOREIGN KEY ConstraintDEPARTMENTS

20、 EMPLOYEESFOREIGNKEYINSERT INTONot allowed(9 does not exist)AllowedPRIMARYKEYFOREIGN KEY ConstraintDefined at either the table level or the column level:CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), salary NUMBER(8,2), commission_pct NUMBER(2,2),

21、 hire_date DATE NOT NULL,. department_id NUMBER(4), CONSTRAINT emp_dept_fk FOREIGN KEY (department_id) REFERENCES departments(department_id), CONSTRAINT emp_email_uk UNIQUE(email);FOREIGN KEY Constraint:KeywordsFOREIGN KEY: Defines the column in the child table at the table-constraint levelREFERENCE

22、S: Identifies the table and column in the parent tableON DELETE CASCADE: Deletes the dependent rows in the child table when a row in the parent table is deletedON DELETE SET NULL: Converts dependent foreign key values to nullCHECK ConstraintDefines a condition that each row must satisfyThe following

23、 expressions are not allowed:References to CURRVAL, NEXTVAL, LEVEL, and ROWNUM pseudocolumns Calls to SYSDATE, UID, USER, and USERENV functionsQueries that refer to other values in other rows., salaryNUMBER(2) CONSTRAINT emp_salary_min CHECK (salary 0),.CREATE TABLE: ExampleCREATE TABLE employees (

24、employee_id NUMBER(6) CONSTRAINT emp_employee_id PRIMARY KEY , first_name VARCHAR2(20) , last_name VARCHAR2(25) CONSTRAINT emp_last_name_nn NOT NULL , email VARCHAR2(25) CONSTRAINT emp_email_nn NOT NULL CONSTRAINT emp_email_uk UNIQUE , phone_number VARCHAR2(20) , hire_date DATE CONSTRAINT emp_hire_d

25、ate_nn NOT NULL , job_id VARCHAR2(10) CONSTRAINT emp_job_nn NOT NULL , salary NUMBER(8,2) CONSTRAINT emp_salary_ck CHECK (salary0) , commission_pct NUMBER(2,2) , manager_id NUMBER(6) , department_id NUMBER(4) CONSTRAINT emp_dept_fk REFERENCES departments (department_id);UPDATE employees *ERROR at li

26、ne 1:ORA-02291: integrity constraint (HR.EMP_DEPT_FK) violated - parent key not foundUPDATE employeesSET department_id = 55WHERE department_id = 110;Violating Constraints Department 55 does not exist.Violating ConstraintsYou cannot delete a row that contains a primary key that is used as a foreign k

27、ey in another table.DELETE FROM departmentsWHERE department_id = 60;DELETE FROM departments *ERROR at line 1:ORA-02292: integrity constraint (HR.EMP_DEPT_FK) violated - child record foundCreating a Tableby Using a SubqueryCreate a table and insert rows by combining the CREATE TABLE statement and the

28、 AS subquery option.Match the number of specified columns to the number of subquery columns.Define columns with column names anddefault values.CREATE TABLE table (column, column.)AS subquery;CREATE TABLE dept80 AS SELECT employee_id, last_name, salary*12 ANNSAL, hire_date FROM employees WHERE department_id = 80;Table created.Creating a Tableby Using a Subqu

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論