Oracle - Notes on Common Database Objects (Snippet)

1: Users and permissions

1.1 User Creation

a) Syntax ---    create user username  identified by password;

b) Create user abcd and set the password to abcd; --- Note: DBA (database administrator) authority is required to operate database objects;

create user abcd identified by bjsxt;

1.2 Authorize users

There are many permissions in Oracle. In order to facilitate user management of permissions, the concept of roles is provided. A role represents an identity that has fixed permissions. Common roles: DBA (database administrator), CONNECT (temporary user, with a small amount of authority ), RESOURCE (more reliable authority, with more authority, cannot manage the database);

a) Give the abcd user the privileges of a normal user

grant connect,resource to abcd;

b) Revoke resource permission from user abcd

revoke resource from abcd;

1.3 Delete User

drop user abcd;

1.4 Modify users

1.4.1 Modify user password

alter user abcd identified by 123;

1.4.2 Lock User and Unlock User

a) Lock the user

alter user abcd account lock;

b) Unlock the user

alter user abcd account unlock;

2. Database tables

2.1 Data Types in Oracle

2.1.1 Character Type

a) varchar2---variable length string

b) cahr --- an immutable length string, which is more efficient;

2.1.2 Numeric Types

number, which can represent both integers and floating-point numbers;

2.1.3 Date Time Type

a) date, the date and time of storage;

b) timestamp, a more precise date and time type than date;

2.1.4 lob type

a) blob, used to store binary data, which can be used to store binary data such as files, pictures, audio, and video;

b) clob, used to store text information;

2.2 Create the form

Create a student table with fields including student ID, name, gender, age, enrollment date, class, email and other information;

create table student(

        sno number(4),

        sname varchar(12),

        gender char(3),

        age number(3),

        sdate date,

        clazz varchar2(20),

        email varchar2(50)

)

2.3 Modify the form

2.3.1 Add fields

alter table student add(score number(3));

2.3.2 Modify the type of the field

alter table student modify(score number(5,2));

2.3.3 Renaming fields

alter table student rename column score to fenshu ;

2.3.4 Delete fields

alter table student drop column fenshu;

2.3.5 Delete table

drop table stu;

3. Form constraints

Constraints, constraints, are used to limit the amount of data in the table to ensure the integrity and consistency of the table data.

Syntax: constraints constraint name + constraint type (constraint field)

3.1 Primary key constraints (primary key)

The primary key constraint is used to uniquely identify the fields of a record, which must be guaranteed to be non-null and unique.

In a table, there can only be one primary key;

a) Define primary key constraints at the table level, and manage constraints uniformly

create table student (

       sno number(4),

       sname varchar2(12),

       gender char(3),

       age number(3),

       sdate date,

       clazz varchar2(20),

       email varchar2(50),

       constraints pk_student primary key (sno)

);

b) add primary key constraint at column level

create table student (

       sno number(4) constraints pk_student primary key,

       sname varchar2(12),

       gender char(3),

       age number(3),

       sdate date,

       clazz varchar2(20),

       email varchar2(50)

);

c) Add a simplified version constraint at the column level

create table student (

       sno number(4) primary key,

       sname varchar2(12),

       gender char(3),

       age number(3),

       sdate date,

       clazz varchar2(20),

       email varchar2(50)

);

d) Joint primary keys, which can only be defined at the table level, because a table can only have one primary key

create table student (

       sno number(4),

       sname varchar2(12),

       gender char(3),

       age number(3),

       sdate date,

       clazz varchar2(20),

       email varchar2(50),

       constraints pk_student primary key (sno, email)

);

3.2 Not null constraint not null

Field values ​​are not allowed to be null, and not-null constraints can only be defined at the column level

create table student (

       sno number(4),

       sname varchar2(12) not null,

       gender char(3),

       age number(3),

       sdate date,

       clazz varchar2(20),

       email varchar2(50),

       constraints pk_student primary key (sno)

);

3.3 Unique constraints (unique)

It is required that the field value cannot be repeated;

create table student (

       sno number(4),

       sname varchar2(12) not null,

       gender char(3),

       age number(3),

       sdate date,

       clazz varchar2(20),

       email varchar2(50), -- unique,

       constraints pk_student primary key (sno),

       constraints uk_student_email unique (email)

);

3.4 Check Constraints (check)

Used to limit the value range of field values;

create table student (

       sno number(4),

       sname varchar2(12) not null,

       gender char (3) check (gender in (' male ',' female ')),

       age number(3),

       sdate date,

       clazz varchar2(20),

       email varchar2(50),

       constraints pk_student primary key (sno),

       constraints uk_student_email unique (email),

       constraints ck_student_age check (age between 18 and 30)

);

3.5 Foreign key constraints

It is used to constrain the relationship between the table and the table. The two target dependencies take the emp table and the dept table as an example. The emp table depends on the dept table. Therefore, the dept table can be called the main table, and the emp table is called Table.

Notice:

》In the main table, only the primary key or the unique structure can be referenced by the slave table;

"The column used as a foreign key in the slave table must be of the same type as the referenced column in the main table;

a) Create the main table clazz

create table clazz (

       cno number (3) primary key,

       cname varchar2(20) not null,

       croom number(3)

);

b) Create the slave table and define the foreign key Student

create table student (

       sno number(4),

       sname varchar2(12) not null,

       gender char (3) default'man ' check (gender in (' man ',' woman ')),

       age number(3),

       sdate date,

       email varchar2(50),

       cno number (3),

       constraints pk_student primary key (sno),

       constraints uk_student_email unique (email),

       constraints ck_student_age check (age between 18 and 30),

       constraints fk_student_cno foreign key (cno) references clazz (cno)

);

c) Foreign keys can also be defined at the column level as follows

create table student (

       sno number(4),

       sname varchar2(12) not null,

       gender char (3) default'man ' check (gender in (' man ',' woman ')),

       age number(3),

       sdate date,

       email varchar2(50),

       cno number (3) references clazz (cno),

       constraints pk_student primary key (sno),

       constraints uk_student_email unique (email),

       constraints ck_student_age check (age between 18 and 30)

);

d) delete issues in foreign keys;

》When the data in the main table is referenced, it cannot be deleted directly. By default, the data in the main table needs to be deleted first.

》You can set the deletion strategy to cascade (cascading) when defining the external construction, which means that when deleting the information of the main table, all the related information in the slave table will be deleted at the same time

create table student (

       sno number(4),

       sname varchar2(12) not null,

       gender char (3) default'man ' check (gender in (' man ',' woman ')),

       age number(3),

       sdate date,

       email varchar2(50),

       cno number (3),

       constraints pk_student primary key (sno),

       constraints uk_student_email unique (email),

       constraints ck_student_age check (age between 18 and 30),

       constraints fk_student_cno foreign key (cno) references clazz (cno) on delete cascade

);

"You can set the deletion policy to set null when defining an external build, which means that when deleting the information of the main table, the relevant data in the slave table will be set to null.

create table student (

       sno number(4),

       sname varchar2(12) not null,

       gender char (3) default'man ' check (gender in (' man ',' woman ')),

       age number(3),

       sdate date,

       email varchar2(50),

       cno number (3),

       constraints pk_student primary key (sno),

       constraints uk_student_email unique (email),

       constraints ck_student_age check (age between 18 and 30),

       constraints fk_student_cno foreign key (cno) references clazz (cno) on delete set null

);

3.6 Adding constraints when modifying tables

a) Add constraints to the student table

--primary key constraint

alter table student add constraints pk_student primary key (sno);

--unique constraint

alter table student add constraints uk_student_email unique (email);

-- check constraints

alter table student add constraints ck_student_age check (age between 18 and 30);

alter table student add constraints ck_student_gender check (gender in ('',''));

-- foreign key constraints

alter table student add constraints fk_student_cno foreign key (cno) references clazz (cno);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324897912&siteId=291194637
Recommended