9 chapter managing schema objects

27
Managing Schema Objects DBA

Transcript of 9 chapter managing schema objects

Managing Schema Objects

DBA

Users, User Accounts, Schemas, and Schema Objects

• User a person who logs on to the database server. Users after successful authentication can create and use objects

• User Accounts contains authentication information user id and password to log on to the database

• Schema A schema consists of the objects owned by the account.– Initially it will be empty– Container of tables, views, codes and other database

objects

Object Namespaces

A namespace defines a group of object types, within which all names must be uniquely identified, by schema and name.

– Tables– Views– Sequences– Private synonyms– Stand-alone procedures– Stand-alone stored functions– Packages– Materialized views– User-defined types

Datatypes

When creating tables, each column must be assigned a datatype.VARCHAR2 Variable-length character data, from 1 byte to 4 KB. Thedata is stored in the database character set. NVARCHAR2 As VARCHAR2, but the data is stored in the alternative national language character set: one of the permitted Unicode character sets.CHAR Fixed-length character data, from 1 byte to 2 KB, in the database character set. If the data is not the length of the column, then it will be padded with spaces.

Numeric Datatypes

NUMBER Numeric data, for which you can specify precision and scale. The precision can range from 1 to 38; the scale can range from –84 to 127.FLOAT This is an ANSI datatype, floating-point number with precision of 126 binary (or 38 decimal). Oracle also provides BINARY_FLOAT and BINARY_DOUBLE as alternatives.INTEGER Equivalent to NUMBER, with scale zero.

Date Data Types

• DATE– The DATE datatype stores date and time information– DATE 'YYYY-MM-DD'

• TIMESTAMP- – extension of the DATE datatype. – It stores year, month, day, hour, minute, and second values. – It also stores fractional seconds, which are not stored by the DATE datatype.

• TIMESTAMP WITH TIMEZONE– TIMESTAMP WITH TIME ZONE is a variant of TIMESTAMP that includes a time zone offset

or time zone region name in its value.• TIMESTAMP WITH LOCAL TIMEZONE

– TIMESTAMP WITH LOCAL TIME ZONE is another variant of TIMESTAMP.– It differs from TIMESTAMP WITH TIME ZONE as follows: data stored in the database is

normalized to the database time zone, and the time zone offset is not stored as part of the column data.

Date Data Types

• INTERVAL YEAR TO MONTH– INTERVAL YEAR TO MONTH stores a period of time using

the YEAR and MONTH datetime fields. Specify INTERVAL YEAR TO MONTH– INTERVAL YEAR [(year_precision)] TO MONTH– INTERVAL '123-2' YEAR(3) TO MONTH

• INTERVAL DAY TO SECOND– INTERVAL DAY TO SECOND stores a period of time in terms of days, hours,

minutes, and seconds. Specify this datatype as follows:

Large Object Data Types

• CLOB– Character data stored in the database character set, size effectively unlimited: 4 GB multiplied by the

database block size• NCLOB

– As CLOB, but the data is stored in the alternative national language character set: one of the permitted Unicode character sets.

• BLOB– As CLOB, but binary data that will not undergo character set conversion by Oracle Net.

• BFILE– A locator pointing to a file stored on the operating system of the database server. The size of the files

is limited to 4 GB.• LONG

– Character data in the database character set, up to 2 GB. All the functionality of LONG (and more) is provided by CLOB; LONGs should not be used in a modern database, and if your database has any columns of this type, they should be converted to CLOB.

• LONG RAW– As LONG, but binary data that will not be converted by Oracle Net. Any LONG RAW columns should

be converted to BLOBs.

Create Table

Create table t ( col1 number, col2 varchar(20)) tablespace ts;Create table t as select * from employeesAlter table- Removing column

- Alter table t drop column email;- Alter table t drop column phone_number

- Adding new columns to existing table- Alter table t add (dob date);- Alter table t add (remarks varchar(20));

Manage Constraints

• UNIQUE – Allows unique values and also allow null in the column. Unique constraint

column also called key column • NOT NULL

– Specifies that column cannot have null value• PRIMARY KEY• FOREIGN KEY• CHECK

FOREIGN KEY

• To establish one to many relationship between tables• ON DELETE CASCADE• This means that if a row in the parent table is deleted, Oracle will search the child

table for all the matching rows and delete them too. This will happen automatically.

• ON DELETE SET NULL• In this case, if a row in the parent table is deleted, Oracle will search the child table

for all the matching rows and set the foreign key columns to null.

CHECK Constraints

• Check Constraints• A check constraint can be used to enforce simple rules, such

as that the value entered in a column must be within a range of values. The rule must be an expression that will evaluate to TRUE or FALSE.

Constraints with Example

1 create table dept(2 deptno number(2,0) constraint dept_deptno_pk primary key3 constraint dept_deptno_ck check (deptno between 10 and

90),4 dname varchar2(20) constraint dept_dname_nn not null);5 create table emp(6 empno number(4,0) constraint emp_empno_pk primary key,7 ename varchar2(20) constraint emp_ename_nn not null,8 mgr number (4,0) constraint emp_mgr_fk references emp

(empno),9 dob date,

Constraint Example

10 hiredate date,11 deptno number(2,0) constraint emp_deptno_fk references dept(deptno)12 on delete set null,13 email varchar2(30) constraint emp_email_uk unique,14 constraint emp_hiredate_ck check (hiredate >= dob + 365*16),15 constraint emp_email_ck16 check ((instr(email,'@') > 0) and (instr(email,'.') > 0)));

Explanation

1. The first table created is DEPT, intended to have one row for each department.2. DEPTNO is numeric, two digits, no decimals. This is the table’s primary key. The

constraint is named DEPT_DEPTNO_PK.3. A second constraint applied to DEPTNO is a check limiting it to numbers in the

range 10 to 90. The constraint is named DEPT_DEPTNO_CK.4. The DNAME column is variable-length characters, with a constraint

DEPT_DNAME_NN making it not nullable.5. The second table created is EMP, intended to have one row for every employee.

Explanation

6. EMPNO is numeric, up to four digits with no decimals. Constraint EMP_EMPNO_PK marks this as the table’s primary key.7. ENAME is variable-length characters, with a constraint EMP_ENAME_NN making it not nullable.8. MGR is the employee’s manager, who must himself be an employee. The column is defined in the same way as the table’s primary key column of EMPNO. The constraint

EMP_MGR_FK defines this column as a self-referencing foreign key, so any value entered must refer to an already-extant row in EMP (though it is not constrained to be not null, so it can be left blank).

Explanation

9. DOB, the employee’s birthday, is a date and not constrained.HIREDATE is the date the employee was hired and is not constrained. At least, not yet.

10. DEPTNO is the department with which the employee is associated. The column is defined in the same way as the DEPT table’s primary key column of DEPTNO, and the constraint EMP_DEPTNO_FK enforces a foreign key relationship; it is not possible to assign an employee to a department that does not exist. This is nullable, however.12. The EMP_DEPTO_FK constraint is further defined as ON DELETE SET NULL, so if the parent row in DEPT is deleted, all matching child rows in EMPNO will have DEPTNO set to NULL.13. EMAIL is variable-length character data and must be unique if entered (though it can be left empty).

Explanation

14. This defines an additional table level constraint EMP_HIREDATE_CK. The constraint checks for use of child labor, by rejecting any rows where the date of hiring is not at least sixteen years later than the birthday. This constraint could not be defined in line with HIREDATE, because the syntax does not allow references to other

columns at that point.

Explanation

15., 16. An additional constraint EMP_EMAIL_CK added to the EMAIL column, which makes two checks on the e-mail address. The INSTR functions search for “@” and “.” characters (which will always be present in a valid e-mail address) and if it can’t find both of them, the check condition will return FALSE and the row will be rejected.

Constraint States

• Two basic states of constraints– Enable

• data is checked as it is entered or updated in the database, and data that does not conform to the constraint is prevented from being entered

– Disable• data that does not conform can also be allowed to enter the database

Constraint States

• Validate– To check existing data of the table must conform the constraint

• Novalidate– Existing may not conform to the constraint

Constraint States

• ENABLE VALIDATE It is not possible to enter rows that would violate the constraint, and all rows in the table conform to the constraint.

• ENABLE NOVALIDATE There may already be non-conforming data in the table, but all data entered now must conform.

• DISABLE VALIDATE An impossible situation: all data in the table conforms to the constraint, but new rows need not. The end result is that the table is locked against DML commands.

• DISABLE NOVALIDATE Any data (conforming or not) can be entered, and there may already be non-conforming data in the table.

Constraint Checking

• Immediate– By default– Set constraint c_eNo immediate;

• Deferred– Constraint can be deferred if declared so– Add constraint c_eNo unique defferrable initially immediate;– Set constraint c_eNo deferred– Scoped of this statement will be on specific session

Renaming Constraint

• Alter table t RENAME constraint old_name to new_name;

Index

• Index has two functions– Enforce primary and unique constraints– Improve performance

• B-Tree Index• Bit Map Index

Index Options

• Unique or non-unique (default)• Reverse key• Compressed• Composite• Function based

Create Index

• CREATE [UNIQUE | BITMAP] INDEX [ schema.]indexname ON [schema.]tablename (column [, column...] ) ;

• create unique index emp_i1 on emp(empno);• create bitmap index ex_dept_i on ex_emps(department_id);