Basic use of oracle (creating tables, operating tables, etc.)

1. Table space, user

1. Create a table space

  • log in with system
    insert image description here
  • create tablespace
    • waterboss is the tablespace name
    • datafile is used to set the physical file name
    • size is used to set the initial size of the tablespace
    • autoextend on is used to set automatic growth, if the storage capacity exceeds the initial size, automatic expansion will start
    • next is used to set the space size for each expansion
-- 创建表空间
create tablespace waterboss
datafile 'C:\oracle\product\10.2.0\oradata\waterboss.dbf'
size 100m
autoextend on
next 10m

insert image description here

2. Create a user

  • Create pzz user and associate waterboss tablespace
    • wateruser is the created username
    • identified by is used to set the user's password
    • default tablesapce is used to specify the default tablespace name
-- 创建用户
create user pzz
identified by pzz
default tablespace waterboss;

insert image description here

  • Log in with the user you just created without login permissions
    insert image description here

3. User authorization

  • log in with system
  • Authorize the pzz user
grant dba to pzz;

insert image description here

  • login again
    insert image description hereinsert image description here

Second, the operation of the table

Note: Compared with oracle, I personally feel that MySQL is easier to use, and the grammar is similar. It is better to learn MySQL first for the basics. It is not recorded in such detail here.

1. Create a table

1.1. Grammar:

CREATE TABLE 表名称(
 字段名 类型(长度) primary key,
 字段名 类型(长度),
 .......
);

1.2. Data type:

  • character type
    • CHAR : A fixed-length character type that can store up to 2000 bytes.
    • VARCHAR2: A variable-length character type that can store up to 4000 bytes.
    • LONG : Large text type. It can store up to 2 G.
  • numeric
    • NUMBER : Numeric type.
      For example:
      NUMBER(5) The maximum number that can be saved is 99999.
      NUMBER(5,2) The maximum number that can be saved is 999.99.
  • date type
    • DATE: Date and time type, accurate to seconds.
    • TIMESTAMP: Accurate to 9 decimal places of the second.
  • Binary (big data type)
    • CLOB: Store characters, up to 4 G can be stored.
    • BLOB: store image, sound, video and other binary data, up to 4 can be stored.

1.3. Example: create owner table

create table t_owners
(
	id number primary key,
	name varchar2(30),
	addressid number,
	housenumber varchar2(30),
	watermeter varchar2(30),
	adddate date,
	ownertypeid number
);

2. Modify the table

2.1. Add field syntax

  • grammar
ALTER TABLE 表名称 ADD(列名 1 类型 [DEFAULT 默认值],列名 1 类型 [DEFAULT 默认值]...)
  • Example: Add two fields to the owner table
--追加字段
ALTER TABLE T_OWNERS ADD
(
 REMARK VARCHAR2(20),
 OUTDATE DATE
)

2.2. Modify field data type

  • grammar
ALTER TABLE 表名称 MODIFY(列名 1 类型 [DEFAULT 默认值],列名 1 类型[DEFAULT 默认值]...)
  • Example: modify the type of two fields
--修改字段类型
ALTER TABLE T_OWNERS MODIFY
(
REMARK CHAR(20),
OUTDATE TIMESTAMP
)

2.3, modify the field name

  • grammar
ALTER TABLE 表名称 RENAME COLUMN 原列名 TO 新列名
  • example
-- 修改字段名
ALTER TABLE T_OWNERS RENAME COLUMN OUTDATE TO EXITDATE;

2.4, delete the field name

  • grammar
--删除一个字段
ALTER TABLE 表名称 DROP COLUMN 列名
--删除多个字段
ALTER TABLE 表名称 DROP (列名 1,列名 2...)
  • example
--删除字段
ALTER TABLE T_OWNERS DROP COLUMN REMARK

3. Delete table

3.1. Delete table (data and structure)

  • Deleting the structure and data of the table is relatively slow.
  • Deleted can be rolled back.
DROP TABLE 表名称;

3.2. Delete table (only data)

  • It is faster to directly delete the data of the table.
  • Deleted cannot be rolled back to restore.
truncate TABLE 表名称;

4. Use tools to operate

Use the graphical interface to operate.
insert image description here

3. Data operation

1. Insert data

  • grammar
INSERT INTO 表名[(列名 1,列名 2...)]VALUES(1,值 2...)
  • example
insert into T_OWNERS VALUES (1,' 张三丰',1,'2-2','5678',sysdate,1);
commit;

2. Modify data

  • grammar
UPDATE 表名 SET 列名 1=1,列名 2=2....WHERE 修改条件;
  • Example: Change the registration date of owner with ID 1 to a date three days ago
update T_OWNERS set adddate=adddate-3 where id=1;
commit;

3. Delete data

  • Syntax 1:
DELETE FROM 表名 WHERE 删除条件;
  • Example: Delete owner information whose owner ID is 2
delete from T_OWNERS where id=2;
commit;
  • Grammar 2
TRUNCATE TABLE 表名称

Compare truncat and delete to achieve data deletion?

  1. delete Deleted data can be rolled back.
  2. delete Deletes may fragment and do not free space.
  3. truncate first destroys the table structure and then reconstructs the table structure.

4. Pay attention to committing transactions

All data operations must be committed by a transaction.

  • Method 1:
    After execution, click the submit button
    insert image description here
  • Method 2:
    Use commit to submit after each statement
commit;

4. Data export and import

1. Whole library export and import

1.1. Whole library export command

  • Add the parameter full=y to export the whole library
exp system/itcast full=y

After executing the command, a file called EXPDAT.DMP will be generated in the current directory, which is a backup file.

  • If you want to specify the name of the backup file, just add the file parameter, the command is as follows
exp system/itcast file=文件名 full=y

1.2. Whole library import command

  • If this command does not specify the file parameter, the backup file EXPDAT.DMP will be used for import by default
imp system/itcast full=y
  • If the file parameter is specified, it will be restored according to the backup file specified by file
imp system/itcast full=y file=water.dmp

2. Export and import by user

2.1. Export by user

exp system/itcast owner=wateruser file=wateruser.dmp

2.2. Import by user

imp system/itcast file=wateruser.dmp fromuser=wateruser

3. Export and import by table

3.1. Export by table

Use the tables parameter to specify the tables to be exported. If there are multiple tables, separate them with commas.

exp wateruser/itcast file=a.dmp tables=t_account,a_area

3.2. Import by table

imp wateruser/itcast file=a.dmp tables=t_account,a_area

Finish! ! ! ! ! !
hy:3


									恕自己一过,则万过由之而生。---苏格拉底

Guess you like

Origin blog.csdn.net/weixin_49107940/article/details/130897052