Oracle basics (table spaces, users, authorization, tables, data types, data import and export, etc.
1 Create table space
1.1 Overview
1.2 Grammar:
– Table space type and name, the default is not to specify the type (permanent)
create [temporary | undo] tablespace table space name
– location and size of the data file
table space address stored in datafile size initial size
– whether to automatically expand, default 'off'
[ autoextend off] | [autoextend on next n maxsize m]
– Whether to generate logs, the default is 'loggin'
[loggin | nologgin]
– Automatic segment space management, the default is 'auto' Recommended
[segment space management auto]
– Table space management method, dictionary | local (default, recommended)
[extent management local [uniform size n]]
1.3 Example:
create tablespace waterspace
datafile 'C:\oracleData\waterspace.dbf'
size 100m
autoextend on
next 10m;
2 Create user
2.1 Grammar
– DBA user execution, default users table space (not recommended)
create user identified by ;
– In actual development,
create user identified by
default tablespace <tablespace_name> – Default tablespace
temporary tablespace temp – Temporary tablespace
quota unlimited on <tablespace_name> – Tablespace quota
grant create session to; – Authorization (can log in)
2.2 Example
create user zhangsan identified by wateruser
default tablespace waterspace
temporary tablespace temp
quota unlimited on waterspace;
grant create session TO zhangsan;
2.3 User authorization types
grant create session to zhangsan;//Grant the zhangsan user the permission to create a session, that is, the login permission, allowing the user to log in to the database
grant unlimited tablespace to zhangsan;//Grant the zhangsan user the permission to use the table space
grant create table to zhangsan;//Grant to create The permissions of the table
grante drop table to zhangsan;//Grant the permission to delete the table
grant insert table to zhangsan;//The permission to insert the table
grant update table to zhangsan;//The permission to modify the table
grant select on tablename to zhangsan;//Grant The permission for user zhangsan to view the specified table
grant drop on tablename to zhangsan; //Grant the permission to delete the table
grant insert on tablename to zhangsan; //Grant the permission to insert
grant update on tablename to zhangsan; //Grant the permission to modify the table
grant insert (id) on tablename to zhangsan;
grant update(id) on tablename to zhangsan;//Grant insert and modify permissions to specific fields of the specified table. Note that only insert and update
grant alert all table to zhangsan;//Grant User zhangsan alerts permissions on any table
grant dba to username;//Grant DBA permissions
3 Creation, modification, and deletion of tables
3.1 Table creation
3.1.1 Overview
- Table: used to 'store data' - is our most common database object
- Notes on table design
(1) When designing tables, try to follow the 'Third Normal Form (3NF)'
(2) The name cannot exceed 30 characters - an error will be reported if it exceeds 30 characters.
(3) The name can only start with a 'letter' and can be preceded by a 'number' , '_', '$' or '#'
3.1.2 Grammar
CREATE TABLE schema_name.table_name (
column_1 data_type column_constraint,
column_2 data_type column_constraint,
...
table_constraint
);
3.1.3 Example
-- 创建业主表(T_OWNERS)
create table T_OWNERS(
ID NUMBER PRIMARY KEY, -- 主键
NAME VARCHAR2(30) NOT NULL, -- 业主名称
ADDRESSID NUMBER NOT NULL, -- 地址ID
HOUSENUMBER VARCHAR2(30) NOT NULL, -- 门牌号
WATERMETER VARCHAR2(30) NOT NULL, -- 水表编码
ADDDATE DATE NOT NULL, -- 登记日期
OWNERTYPEID NUMBER NOT NULL, -- 业主类型ID
CONSTRAINT FK_T_OWNERS_ADDRESSID FOREIGN KEY(ADDRESSID) REFERENCES T_ADDRESS(ID),
CONSTRAINT FK_T_OWNERS_OWNERTYPEID FOREIGN KEY(OWNERTYPEID) REFERENCES T_OWNERTYPE(ID)
);
3.1.4 Table data types
1. Character type
(1) CHAR: fixed-length character type, can store up to 2000 characters
(2) VARCHAR2: variable-length character type, can store up to 4000 bytes
(3) LONG: large text type, can store up to 4000 bytes 2G
2. Numeric type
NUMBER: Numeric type
For example: NUMBER(5) The maximum number that can be stored is 99999
NUMBER(5,2) The maximum number that can be stored is 999.99
3. Date type
(1) DATE: Date and time type, accurate to second
(2) TIMESTAMP: accurate to 9 decimal places of second
4. Binary type (big data type)
(1) CLOB: stores characters, up to 4 G can be stored
(2) BLOB: stores binary data such as images, sounds, videos, etc., can store up to 4 G
3.2 Table modification
3.2.1 Insert table data
语法:insert into 表名(column1,colunm2...) values((column1,colunm2...) where 条件表达式
3.2.2 Add fields
grammar:
ALTER TABLE 表名称 ADD(列名 | 类型 [DEFAULT 默认值], 列明 | 类型 [DEFAULT 默认值]...)
Example:
ALTER TABLE T_OWNERS ADD
(
PEMARK VARCHAR2(20),
OUTDATE DATE
)
3.2.3 Modify fields
Syntax:
ALTER TABLE table name MODIFY (column name | type [DEFAULT default value], list | type [DEFAULT default value]…)
Example:
ALTER TABLE T_OWNERS MODIFY
(
PEMARK CHAR(20),
OUTDATE TIMESTAMP
)
3.2.4 Modify field name
grammar:
ALTER TABLE 表名称 RENAME COLUMN 原列表 TO 新列名
示例:
ALTER TABLE T_OWNERS RENAME COLUMN OUTDATE TO EXITDATE
3.2.5 Delete field name
grammar:
ALTER TABLE 表名称 DROP COLUMN 列明1,列明2...;
– 示例
ALTER TABLE T_OWNERS DROP COLUMN REMARK,OUTDATE;
3.3 Table deletion
Delete tables and structures
drop table T_OWNERS
The deleted table has a log and can be restored
delete table T_OWNERS
Clear the table, no logs, and cannot be restored
truncate table
4. Database import/export
4.1 Entire library import/export
4.1.1 Export the entire library
exp username/user secret full = y file=file name--file name, it is recommended to use the .dmp suffix
4.1.2 Whole library import
imp username/user secret full = y file=file name--file name, it is recommended to use the .imp suffix
4.2 Import/Export by user
4.2.1 Export by designated user
exp username/user secret owner = username file=file name--file name, it is recommended to use the .dmp suffix
4.2.2 Specify user import
imp username/user secret file=file name fromuser=user name--file name, it is recommended to use the .imp suffix
4.3 Import/export by table
4.3.1 Import by table
exp username/user secret file=file name tables=table name (separate multiple by commas) --file name, it is recommended to use the .dmp suffix
4.3.2 Export by table
imp username/user secret file=file name tables=table name (multiple ones separated by commas)