Oracle Database - a two-dimensional table Account Management & Administration

 

Oracle Account Management

Authority: the ability to operate with certain things, and this ability is called permission.
Role: a collection of privileges
Oracle's own account
  system account management
    features: have operating authority most of the oracle, mainly used to manage general accounts and oralce data
    using human: oracle data maintenance staff
  sys super administrator account
    features: with All the permissions system, but also have other rights
    to use people: oracle siege lion
1, create an account
  using the system account and use the identity of dba, oracle management system to log
             the create the User the Test IDENTIFIED by the Test;        
2, to maintain the account
  given permission to grant permissions user name or role name to
  grant connect to test; - given to user logon rights
  grant resource to test; - the resources to the user operating authority
  grant dba to test; - dba privileges given to the user
  select * from scott.emp; - - View other users table (table name user name.)         
  delete permission revoke privileges or role name from the user name
    revoke dba from the Test;
3, delete the account drop user user name
  drop user test;

Oracle's two-dimensional table management

Oracle create two-dimensional table

Use: create table table name (type name field, type field names, ....);
  Data Type:
    Number Type
    Value Type
      Integer Number Type (a) the total length of a
      floating point type number (a, b) Total length of a, decimal bit length is b, the decimal is not necessary.
    varchar2 type
      character type varchar2 (ln) ln represents the maximum length of characters, the actual storage memory lengths be allocated according to the character size, but can not be over ln
      features: dynamic allocation of memory space saving
    char type
      character type char (ln) regardless of the character how is the data length, stores the data directly open space ln size
      characteristics: storage efficiency than VARCHAR2
    DATE type
Create Table Student (
      SnO Number (10),
      sname VARCHAR2 (100),
      SAGE Number (. 3),
      SSEx char (100),
      VARCHAR2 sfav (500),
      sbirth DATE  
)

oracle create two-dimensional table and add a constraint

1, the primary key constraint
2, non-null constraint
3, check constraints
4, the only constraint
5, foreign key constraint
simple table and field types to create
      Create Table Student (
           SnO Number (10), Key --primary
           sname VARCHAR2 (100), - null -not
           SAGE Number (. 3), --check (SAGE <150 and SAGE> 0)
           SSEx char (. 4), --check (SSEx = 'M' or ssex = 'F')
           sfav VARCHAR2 (500),
           sbirth DATE,
           sqq. VARCHAR2 (30) --unique
           --constraints pk_student_sno primary key (SnO) - Add primary key constraint
           --constraints ck_student_sname check (sname is not null ) - non-empty constraint
           --constraints ck_student_sage check (sage <150 and sage> 0) - check constraints
           --constraints ck_student_ssex check (ssex = 'M' or ssex = 'F') - check constraint
           --constraints un_student_sqq unique (sqq) - the only constraint
      )  
1, add the primary key constraint
  alter table student add constraints pk_student_sno primary key (sno );
  ALTER Student drop Constraints Table pk_student_sno;
    Primary key features: non-null unique
      1) used as primary After creating the field of the table Key
      2) using constraints pk_ table rearmost creation statement tables _ field name primary key (field)
      3) after creating the table alter table table add constraints pk_ table _ field name primary key (field name);
         constraint name drop primary alter table student drop constraints primary key;
2, adding a non-null constraints
  alter table student add constraints ck_student_sname check ( sname is not null );
  ALTER Student drop Constraints Table ck_student_sname;
    1) not null keywords directly after creating fields of the table
    2) Use constraints ck_ table name in the final surface of the table creation statement _ field names check (not null field name IS)
    3) Use alter table after table creation table _ add constraints ck_ table field name check (field name is not null);
          delete non-empty alter table student drop constraints bound a non-null constraint name;
3, check constraint is added
  alter table student add constraints ck_student_sage check ( sage <150 SAGE and> 0)
  ALTER Student drop constraints Table ck_student_sage;
      add parity check constraints sex
  alter table student add constraints ck_student_ssex check ( ssex = ' M' or ssex = 'F')
  ALTER Student drop constraints Table ck_student_ssex;
    1) directly check (after creating condition field of the table) Number e.g. SAGE (. 3) check (SAGE <150 and SAGE> 0)
    2) rearmost in the table using constraints ck_ statements create a table of field names check _ (condition)
    3) use alter table table name add constraints ck_ table _ column names check (condition) after creating the table;
          delete check constraints alter table student drop constraints check constraint name;
4, add unique constraints
       alter table student add constraints UNIQUE un_student_sqq (sqq.)
  ALTER Student drop Constraints table un_student_sqq;
    . 1) directly after creation of the field of the table UNIQUE
    2) using the table constraints un_ statement to create the table in the back _ field name unique (field name);
    3) Create alter table using the table name of the table _ add constraints un_ table field name unique (field name);
          drop constraints: alter table table name drop constraints only constraint name;
5, foreign key constraints
创建学生表
   create table student(
         sno number(10) primary key,
         sname varchar2(100) not null,
         sage number(3) check(sage>0 and sage<150),
         ssex char(4) check(ssex='男' or ssex='女'),
         sfav varchar2(500),
         sqq varchar2(30) unique,
         cid number(10) --references clazz(cno)
         --constraints fk_student_cid foreign key(cid) references clazz(cno)--外键
      )
  添加外键
  alter  table student add constraints fk_student_cid foreign key(cid) references clazz(cno) on delete set null
  alter  table student drop constraints fk_student_cid 
创建班级表
      create table clazz(
       Number CNO (10) Primary Key,
       CNAME VARCHAR2 (100) Not null,
       cdesc VARCHAR2 (300)
      )

using the foreign key:
  role: when inserted in the sub-table data is not present in the parent table, it is automatically given.
  Concept: When the value of a field of a table need to rely on additional value of a field of a table, then the foreign key constraints.
    Wherein the active dependent tables as sub-tables, the dependent tables as the parent table. Adding a foreign key in the child table.
  Use:
    1) after the sub-field in the table directly references the parent table name (field) e.g.: CID Number (10) references clazz (CNO)
    2) sub-table name using constraints fk_ rearmost field created _ statement table name foreign key (field name) references the parent table name (field names)
    3) after you create a table using: alter table table name add constraints fk_ child table _ column names foreign key (field name) references the parent table name (field name )
       remove the foreign key: alter table table name drop constraints foreign key constraint
  foreign key to select:
    generally selected primary key of the parent table as a foreign key in the child table.
  Foreign key drawback:
    you can not directly delete the parent table data, unless cascade delete
    Cascade delete: When you add a foreign key constraint, use the keyword on delete cascade
      use: When you delete the parent table data, automatically deletes all the data related to the child table.
      Disadvantages: can not reserve sub-table of historical data.
      Use the keyword on delete set null
        when deleting the parent table data, will depend on the value of the field in the child table to null.
        Note: The dependent child table fields can not add a non-empty constraint.

Maintenance of two-dimensional table

Add a new field:
  alter table table name add Field Name Type
  alter table student add sphone number (11 ) - add a new field in the students table
to amend the original field
  to modify the field type
    alter table table name modify field names a new type of
    alter table student modify sphone varchar2 (11)
  modify the field name
    alter table table name rename column field name to the new field name
    alter table student rename column sphone to phone
  to delete the field
    alter table table name drop column column name
    alter table student drop column phone
modify the table name
  rename the original table to the new table name
  rename student to student2
drop table
  drop table table name
  drop table student
 

Guess you like

Origin www.cnblogs.com/sunny-sml/p/12052433.html