Constraints, views, sequences, pseudo columns, and indexes

Source: Internet
Author: User
Preface constraints are used to ensure data integrity. There are mainly primary key constraints (primarykey) non-null constraints (notnull), unique constraints (unique) check constraints (check) and foreign key constraints (foreignkey ). body constraint primary key constraint (primarykey) primary key constraint contains non-null and unique createtable table name (field Name field

Preface constraints are used to ensure data integrity. There are primary key constraints (primary key)/non-null constraints (not null), unique constraints (unique) check constraints (check) and foreign key constraints (foreign key ). body constraints primary key constraints include non-null and unique create table names (field Name field

Preface

Constraints are used to ensure data integrity. There are primary key constraints (primary key)/non-null constraints (not null), unique constraints (unique) check constraints (check) and foreign key constraints (foreign key ).

Body constraint primary key constraint (primary key)

Primary Key constraints include non-null and unique

Create table Name (field Name field type (length) constraints );

1. create custom name Constraints

Create table Name (field Name field type (length), constraint name constraint type (field name ));

Note: You can use the user_constraints data dictionary to query constraints.

Non-null constraint (not null) check constraint (check)

Create table t_test (id number (10); sex char (2); constraint test_sex check (sex in ('male', 'female '))); -- you can set the default value by adding the default value to the field; use the default keyword when using the default value.

Note: Limits on tuples are defined.

You can set constraints for values of different attributes at the tuples level.

Check (ssex = 'female 'or sname NOT like 'Ms. %'); -- when the student's gender is male, its name cannot start with MS.

Foreign key constraint (foreign key field name references table name (field name )).

The foreign key field can be blank or reference the self-dependent parent item.

Set Cascade

1. assign a null value

Foreign key (foreign key field) references table name (field) on delete set null;

2. cascading Deletion

Foreign key (foreign key field) references table name (field) on delete cascade;

Joint primary key

Constraint pk_t_emp primary key (Field 1, Field 2 );

DDL operation constraints

Alter table Name drop contraints constraint name;

Alter table name add contraints constraint name constraint type (field );

Alter table name modify (constraints to be added for field types );

View

Create or replace view name as select * from emp;

Note:

The definition of a complex view is that data in a view cannot be obtained directly from a table, but must be obtained through computation. The query field of a complex view must be an alias.

Sequence

Sequence is a unique set of numbers in the database that can automatically increase.

Create sequence name increment by start with start value of each increment step.

1. select sequence name. nextval from dual. -- Obtain the next value of the sequence. The current value of the obtained sequence uses the currval attribute.

Pseudo Column

Oracle-specific is provided to each table by default to implement certain functions.

Rownum

Indicates the sequence number of each record (the sequence number assigned only after the result set is found), which is often used for paging.

Rowid

Indicates the unique identifier of each record (a 32-character string). It can be used to obtain the physical address of the record.

Apply to Paging

1. calculate the total number of pages. totalpage = sum/pagesize + sum % pagesize = 0? ; // Calculate the total number of pages. sum indicates the total number of records.

2. the start position and end position of the record on the page are:

Pagesize * (page-1) + 1; start position

Pagesize * page; j. End position

Note:

1. You can use minus to implement paging in database queries, but the efficiency is low'

2. Common subqueries use rownum as the field of another result set to implement paging.

Select ee. * from (select e. *, rownum rr from (select * from emp where sal is not null order by sal) e) ee where ee. rr berween & start and & end;

3. Sample Code of rownum paging java

Index

Quickly locate a record (rowid is used at the underlying layer); full table scan is used when no index is found)

Note:

1. You can use the user_indexes data dictionary to search for indexes.

2 database. index is created for the primary key and unique key in the table by default.

Custom Index

Create index name on table name (field name );

The index should not be applied.

1. Fields frequently updated

2. The table is small and the query result set is large.

3. infrequently used fields

Application Index

1. Fields frequently used for query

2. When the table data volume is large and the query result set is small

3. Many blank fields with the current field value

4. fields often used for joint queries

5. Foreign key field

Data Dictionary

A table or view that stores information about a description object (table, sequence, view, trigger, process, function, etc.) is called a data dictionary.

User_XXX indicates the data dictionary of the current user's related information

User_tables/user_constraints/user_indexes/user_sequences/user_views/user_objects

Example: select distinct object -- type from user_objects; -- view all objects

User_all_XXX Data Dictionary of the objects that the current user can access dba_xxx data dictionary summary of all objects in the current database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.