Playing with oracle study notes (4) - database operation

1. Database administrator

1. Each oracle database should have at least one database administrator (dba). For a small database, one dba is enough, but for a large database, multiple dba may be required to undertake different management responsibilities. So what is the main job of a database administrator?

Responsibilities:

1) Install and upgrade oracle database

2) Build database, table space, table, view, index...

3) Develop and implement a backup and recovery plan

4) Database rights management, tuning and troubleshooting

5) For advanced dba, it is required to be able to participate in project development, and be able to write sql statements, stored procedures, triggers, rules, constraints, packages

 

2. The users who manage the database are mainly sys and system

sys: Chairman

system: general manager

The difference between the two:

a. The most important difference, the importance of the stored data is different

        sys: All the base tables and views of Oracle's data dictionary are stored in the sys user. These base tables and views are crucial to the operation of Oracle and are maintained by the database itself, and no user can manually change them. The sys user has the dba, sysdba, and sysoper roles or permissions, and is the user with the highest oracle authority.

        system: used to store first-level internal data, such as some features of oracle or management information of tools. The system user has the dba, sysdba role, or system privileges.

b. Second difference, different permissions

        The sys user must log in as sysdba or as sysoper, and cannot log in to the database in normal mode.

        If the system logs in normally, it is actually an ordinary dba user, but if you log in as sysdba, the result is actually logged in as a sys user, which can be seen from the login information.

 

3. The difference between sysdba and sysoper permissions


conn system/manager as sysdba;

shutdown

startup

 

4. Manage initialization parameters

        The initialization parameters are used to set the characteristics of the instance or database. Oracle9i provides more than 200 initialization parameters, and each initialization parameter has a default value.

        Display initialization parameters: show parameter

        How to modify parameters

        It should be noted that if you want to modify these initialization parameters, you can go to the file D:\oracle\admin\myoral\pfile\init.ora to modify it, for example, to modify the name of the instance.

 

2. Logical backup and recovery of database (table)

        Logical backup refers to the process of using the tool export to export the structure and data of the data object to a file. Logical recovery refers to the process of using the tool import to import the data object into the database using the backup file when the database object is damaged by misoperation. Physical backup can be performed in the open state of the database or after the database is closed, but logical backup and recovery can only be performed in the open state.

1. Export

        Export is divided into three ways: export table, export scheme, and export database

        The export is done using the exp command. The commonly used options for this command are:

userid: used to specify the user name, password, and connection string to perform the export operation
tables: used to specify the table to perform the export operation
owner: used to specify the scheme to perform the export operation
full=y: used to specify the database to perform the export operation
inctype: used to specify the incremental type to perform the export operation
rows: used to specify whether to export the data in the table when the export operation is performed
file: used to specify the export file name

1) Export table

a. Export your own table

exp userid=scott/tiger@myoral tables=(emp) file=d:\e1.dmp

 

b. Export tables for other schemes

If users want to export tables of other schemes, they need dba permissions or exp_full_database permissions. For example, system can export scott's tables: exp userid=system/manager@myoral tables=(scott.emp) file=d:\e2 .dmp

Special note: When importing and exporting, go to the bin directory of the oracle directory

example:

open cmd window

Switch to the bin directory of the oracle directory

Export a table:

exp userid=scott/tiger@myoral tables=(emp) file=d:\emp.dmp

Export multiple tables:

exp userid=scott/tiger@myoral tables=(emp,dept) file=d:\emp2.dmp

Export tables for other schemes:

exp userid=system/manager@myoral tables=(scott.emp) file=d:\kk.dmp

c. The structure of the export table

exp userid=scott/tiger@myoral tables=(emp) file=d:\e3.dmp rows=n

example:

exp userid=scott/tiger@myoral tables=(emp,dept) file=d:\emp3.dmp rows=n

 

d. Use the direct export method

exp userid=scott/tiger@myoral tables=(emp) file=d:\e3.dmp direct=y

This method is faster than the default conventional method. When the amount of data is large, you can consider using this method. At this time, the character set of the database needs to be exactly the same as the character set of the client, otherwise an error will be reported.

example:

exp userid=scott/tiger@myoral tables=(emp,dept) file=d:\emp4.dmp direct=y

 

2) Export scheme

Exporting a scheme refers to using the export tool to export all the diagonals (tables, indexes, constraints...) and data in a scheme or multiple schemes, and store them in a file

a. Export your own scheme

exp scott/tiger@myoral owner=scott file=d:\scott.dmp

example:

exp userid=scott/tiger@myoral owner=scott file=d:\emp5.dmp

 

b. Export other schemes

If the user wants to export other schemes, he needs the authority of dba or the authority of exp_full_database, for example, the system user can export any scheme

exp system/manager@myoral owner=(system,scott) file=d:\system.dmp

example:

exp userid=system/manager@myoral owner=(system,scott) file=d:\kk.dmp

 

3) Export database

        Exporting the database means using export to export objects and data in all databases, requiring the user to have dba authority or exp_full_database authority

exp userid=system/manager@myoral full=y inctype=complete file=d:\aa.dmp

Note: Because of the large amount of data, it will take a long time

 

2. Import

        Import is to use the tool import to import the objects and data in the file into the database, but the file to be used for import must be the file specified by export. Similar to export, import is also divided into three types: import table, import scheme, and import database. Way.

        The commonly used options for imp are:

userid: used to specify the user name, password, and connection string to perform the import operation
tables: used to specify the table to perform the import operation
fromuser: used to specify the source user
touser: used to specify the target user
file: used to specify the import file name
full=y: used to specify that the entire file is imported
inctype: used to specify the type of increment to perform the import operation
rows: Specifies whether to import table rows (data)
ignore: only import data if the table exists

1) Import table

a. Import your own table

imp userid=scott/tiger@myoral tables=(emp) file=d:\xx.dmp

b. Import the table to other users

The user is required to have dba privileges, or imp_full_database privileges

imp userid=system/manager@myoral tables=(emp) file=d:\xx.dmp touser=scott

c. The structure of the import table

Import only the structure of the table without importing the data

imp userid=scott/tiger@myoral tables=(emp) file=d:\xxx.dmp rows=n

d. Import data

If the object (such as a table) already exists, you can only import the data of the table

imp userid=scott/tiger@myoral tables=(emp) file=d:\xxx.dmp ignore=y

example:

Executed drop table emp in sqlplus;

The location must be imported in cmd, as follows:

imp userid=scott/tiger@myoral tables=(emp) file=d:\emp.dmp

 

2) Import scheme

Importing a schema refers to using the import tool to import objects and data from a file into one or more schemas. If you want to import other programs, the user is required to have the dba authority, or the imp_full_database authority.

a. Import your own scheme

imp userid=scott/tiger file=d:\xxx.dmp

b. Import other programs

imp userid=system/manager file=d:\xxx.dmp fromuser=system touser=scott

3) Import the database

By default, when importing a database, all object structures and data will be imported, as follows:

imp userid=system/manager full=y file=d:\xxx.dmp

 

3. Data dictionary and dynamic performance view

        The data dictionary is the most important part of the oracle database, it provides some system information of the database.

        The dynamic performance view records the relevant information after the routine is started.

1. Data dictionary

        The data dictionary records the system information of the database. It is a collection of read-only tables and views. The owner of the data dictionary is the sys user.

Users can only perform query operations (select statements) on the data dictionary, and its maintenance and modification are automatically completed by the system.

The composition of the data dictionary: The data dictionary includes the data dictionary base table and the data dictionary view. The base table stores the basic information of the database, and ordinary users cannot directly access the base table of the data dictionary. The data dictionary view is a view established based on the data dictionary base table. Ordinary users can obtain system information by querying the data dictionary view. Data dictionary views mainly include three types: user_xxx, all_xxx, and dba_xxx (xxx may represent tables and views).

        user_tables: used to display all the tables owned by the current user, it only returns all the tables of the scheme corresponding to the user. For example: select table_name from user_tables;

        all_tables: used to display all the tables that the current user can access, it will not only return all the tables of the current user's schema, but also return the tables of other schemas that the current user can access, such as: select table_name from all_tables;

        dba_tables: It will show all the database tables owned by the schema. However, to query this database dictionary view, the user must be the dba role or have the select any table system privilege. For example, when the data dictionary view dba_tables is queried by the system user, the database tables corresponding to the system, sys, scott... schemes will be returned.

        When creating a user, oracle will store the user's information in the data dictionary. When granting permissions or roles to users, oracle will store the information of permissions and roles in the data dictionary.

        Details of all database users can be displayed by querying dba_users;

desc dba_users;

select username,password from dba_sys_users;

        By querying the data dictionary view dba_sys_privs, all system privileges of the user can be displayed;

        By querying the data dictionary view dba_tab_privs, the object privileges the user has can be displayed;

        The column privileges a user has can be displayed by querying the data dictionary dba_col_privs;

        The roles a user has can be displayed by querying the database dictionary view dba_role_privs.

desc dba_role_privs;
select * from dba_role_privs where grantee = ‘SCOTT’;

        Among them: ADMIN indicates whether there is permission to authorize other users.

1) Query all system permissions in oracle, usually dba

select * from system_privilege_map order by name;

2) Query all roles in oracle, usually dba

select * from dba_roles;

3) Query all object permissions in oracle, usually dba

select distinct privilege from dba_tab_privs;

4) Query the tablespace of the database

select tablespace_name from dba_tablespaces;

5) How to query the permissions included in a role?

        Permissions are divided into object permissions and system permissions. System permissions are for databases, and object permissions are for data objects.

Guess you like

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