表空间是数据库的逻辑存储结构,所有数据库对象均存储于指定的表空间内。
执行CREATE TABLESPACE语句创建表空间。
SYSTEM/SYSAUX/UNDO/SWAP/TEMPORARY/DEFAULT表空间在CREATE DATABASE时创建,不能用CREATE TABLESPACE语句创建。
SQL> CREATE TABLESPACE ts_yashan;
SQL> CREATE TABLESPACE remotebak DATAFILE '/yashandb/data/db-1-1/dbfiles/remotebak01.dbf' SIZE 4M AUTOEXTEND ON NEXT 4M MAXSIZE 1G PARALLEL 2;
create tablespace remotebaktemp;
或
create tablespace remotebaktemp tempfile '/yashandb/data/db-1-1/dbfiles/remotebaktemp01.dbf'
通过查询DBA_TABLESPACES视图查看当前数据库中存在的所有表空间:
SQL> SELECT TABLESPACE_NAME FROM DBA_TABLESPACES;
--查表空间和数据文件关系
SQL> select tablespace_name,file_name from dba_data_files;
执行ALTER TABLESPACE语句修改表空间的相关属性:
SQL> ALTER TABLESPACE ts_yashan ADD DATAFILE;
SQL> ALTER TABLESPACE ts_yashan SHRINK SPACE;
执行DROP TABLESPACE语句删除表空间:
SQL> DROP TABLESPACE ts_yashan; --不删除操作系统上数据文件
SQL> drop tablespace remotebak including contents and datafiles; --同时删除操作系统上数据文件
创建表空间语句含区管理段空间管理,oracle中创建成功,yashan中报错。
--oracle中成功
create tablespace remotebak datafile '/oradata/topdh/remotebak01.dbf' size 200m autoextend on next 50m maxsize unlimited logging extent management local segment space management auto;
--yashan db中报错
SQL> create tablespace remotebak datafile '/yashandb/data/db-1-1/dbfiles/remotebak01.dbf' size 200m autoextend on next 50m maxsize unlimited logging extent management local segment space management auto;
[1:137]YAS-00207 end of text expected but l found
语法和oracle不一样
extent_management_clause
该语句用于指定表空间里的对象申请extent时的空间分配方式:
create tablespace remotebak datafile '/yashandb/data/db-1-1/dbfiles/remotebak01.dbf' size 200m autoextend on next 50m maxsize unlimited EXTENT UNIFORM SIZE 64K ;
创建临时表空间失败,oracle中创建成功,yashan中报错。
--oracle中成功
create temporary tablespace remotebaktemp tempfile '/oradata/topicis/remotebaktemp01.dbf'
--yashan db中报错
SQL> create temporary tablespace remotebaktemp tempfile '/yashandb/data/db-1-1/dbfiles/remotebaktemp01.dbf';
YAS-00004 feature "creating temporary tablespace" has not been implemented yet
语法和oracle不一样,需要去掉temporary 参数
去掉temporary 参数,创建成功。
create tablespace remotebaktemp tempfile '/yashandb/data/db-1-1/dbfiles/remotebaktemp01.dbf';
表是数据库用来存放数据的一个集合,一般与实体对象一一对应,如人员表、部门表、公司表等,一般由行和列这两个二维信息来组织表数据。
执行CREATE TABLE语句创建表:
SQL> CREATE TABLE tb_yashan(c1 INT,C2 VARCHAR(10));
通过查询USER_TABLES视图查看当前用户中已存在的表名称:
SQL> SELECT TABLE_NAME,TABLE_TYPE FROM USER_TABLES limit 1;
TABLE_NAME TABLE_TYPE
---------------------------------------------------------------- ----------
TB_YASHAN HEAP HEAP
执行SELECT语句查看表中具体信息:
SQL> SELECT * FROM tb_yashan;
C1 C2
------------ -------------
0 rows fetched.
查看表结构:
SQL> desc tb_yashan
NAME NULL? DATATYPE
---------------------------------------------------------------- --------- ---------------------------------
C1 INTEGER
C2 VARCHAR(10)
执行ALTER TABLE语句修改表的相关属性:
SQL> ALTER TABLE tb_yashan RENAME TO tab_yashan;
Succeed.
SQL> ALTER TABLE tab_yashan ADD(c3 NUMBER);
Succeed.
ALTER TABLE tab_yashan MODIFY c3 FLOAT;
执行DROP TABLE语句删除表:
SQL> DROP TABLE tab_yashan;
Succeed.
索引是一种物理的对数据库表中一列或多列的值进行排序的存储结构,它是某个表中一列或若干列值的集合,是指向表中物理标识这些值所在行的逻辑指针清单。
执行CREATE INDEX语句创建索引:
SQL> CREATE TABLE tb_index(c1 INT,c2 INT);
SQL> CREATE INDEX inde1 ON tb_index(c1);
通过查询USER_INDEXES视图查看当前用户的索引信息:
SQL> SELECT * FROM USER_INDEXES;
INDEX_NAME INDEX_TYPE TABLE_OWNER TABLE_NAME TABLE_TYPE UNIQUENESS COMPRESSION PREFIX_LENGTH TABLESPACE_NAME INI_TRANS MAX_TRANS PCT_FREE LOGGING BLEVEL LEAF_BLOCKS DISTINCT_KEYS AVG_LEAF_BLOCKS_PER_KEY AVG_DATA_BLOCKS_PER_KEY STATUS NUM_ROWS SAMPLE_SIZE LAST_ANALYZED PARTITIONED TEMPORARY GENERATED VISIBILITY DATABASE_MAINTAINED CONSTRAINT_INDEX
------------- -------------- --------------- --------------- ---------------- ---------- ----------- ------------- ------------------- ------------ ------------ ------------ ------- ------------ --------------------- --------------------- ----------------------- ----------------------- --------- --------------------- --------------------- --------------------- ----------- --------- --------- ------------- ------------------- ----------------
INDE1 NORMAL YASHAN TB_INDEX TABLE N DISABLED 0 USERS 2 255 8 Y VALID N N N VISIBLE N N
SQL> SELECT INDEX_NAME FROM USER_INDEXES;
INDEX_NAME
-------------
INDE1
执行DROP INDEX语句删除索引:
SQL> DROP INDEX inde1;
通过执行INSERT语句往表中插入数据:
SQL> CREATE TABLE insert_tb(c1 INT,c2 CHAR(10));
SQL> INSERT INTO insert_tb VALUES(4,'hello');
SQL> INSERT INTO insert_tb VALUES(1,'world'),(2,'nihao'),(3,'shijie');
SQL> COMMIT;
YashanDB中可选DELETE和TRUNCATE TABLE两种方式对表数据进行删除:
SQL> DELETE FROM insert_tb WHERE c1=1;
SQL> DELETE insert_tb;
SQL> TRUNCATE TABLE insert_tb;
通过执行UPDATE语句更新表中数据:
INSERT INTO insert_tb VALUES(1,'nihao'),(2,'hello'),(3,'shijie'),(4,'world');
SELECT * FROM insert_tb;
C1 C2
------------ -------------
1 nihao
2 hello
3 shijie
4 world
UPDATE insert_tb SET c1=5 WHERE c1=1;
SELECT * FROM insert_tb;
C1 C2
------------ -------------
5 nihao
2 hello
3 shijie
4 world
update insert_tb set (c1,c2)=(7,'newvalue') where c1=3;
SELECT * FROM insert_tb;
C1 C2
------------ -------------
5 nihao
2 hello
7 newvalue
4 world
通过执行SELECT语句查询表数据:
SELECT * FROM insert_tb;
C1 C2
------------ -------------
5 nihao
2 hello
7 newvalue
4 world
SELECT * FROM insert_tb ORDER BY c1;
C1 C2
------------ -------------
2 hello
4 world
5 nihao
7 newvalue
SELECT C2 FROM insert_tb WHERE c1=5;
C2
-------------
nihao
提交事务前,用户在事务过程做的任何修改只有自己能看到,其他用户无法看到,并可以通过回滚操作将数据恢复。
提交事务后,其他用户可看到修改后的数据,此时无法通过回滚操作将数据恢复。
执行COMMIT语句提交事务:
CREATE TABLE COM_TB(c1 INT);
INSERT INTO COM_TB VALUES(1),(2),(3);
COMMIT;
执行ROLLBACK语句回退事务:
INSERT INTO COM_TB VALUES(6);
SELECT * FROM COM_TB;
C1
------------
1
2
3
6
ROLLBACK;
SELECT * FROM COM_TB;
C1
------------
1
2
3
执行如下SQL命令创建新用户yashan,并为其指定密码yashan:
CREATE USER yashan IDENTIFIED BY yashan;
执行如下SQL命令创建新角色yashan_role:
CREATE ROLE yashan_role;
执行如下SQL命令为用户yashan授予登录会话和创建资源的权限:
GRANT CONNECT TO yashan;
GRANT RESOURCE TO yashan;
执行如下SQL命令切换至用户yashan:
conn yashan/yashan;
Connected to:
YashanDB Server Personal Edition Release 23.1.0.100 x86_64 - X86 64bit Linux
Note:
切换对象须具有登录会话的权限方可进行切换操作。
执行如下SQL命令将yashan用户的密码修改为yashandb:
ALTER USER yashan IDENTIFIED BY "yashandb";
yasldr是YashanDB提供的客户端导入工具,用于执行CSV格式的数据文件导入,每次导入单个数据文件至单个表中,有关yasldr工具的具体使用语法及限制可参考yasldr使用指导章节。
su - yashan
cd /home/yashan
vi datafile
1|load|101
2|load|201
[yashan@localhost ~]$ yasql 'sys/"Admin@2023"'
YashanDB SQL Personal Edition Release 23.1.1.100 x86_64
Connected to:
YashanDB Server Personal Edition Release 23.1.1.100 x86_64 - X86 64bit Linux
CREATE USER yasldr_user IDENTIFIED BY yasldr;
GRANT CONNECT TO yasldr_user;
GRANT RESOURCE TO yasldr_user;
conn yasldr_user/yasldr
Connected to:
YashanDB Server Personal Edition Release 23.1.0.100 x86_64 - X86 64bit Linux
CREATE TABLE loadData (c1 INT,c2 CHAR(10),c3 INT);
SQL> exit
$ yasldr yasldr_user/yasldr batch_size=4032 control_text="'LOAD DATA OPTIONS(DEGREE_OF_PARALLELISM=2) INFILE '/home/yashan/datafile' FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '\"' APPEND INTO TABLE loadData(c1,c2,c3) '"
YashanDB Loader Release 23.1.0.100 x86_64 297f388
[YASLDR] execute succeeded
$ yasql yasldr_user/yasldr
YashanDB SQL Personal Edition Release 23.1.0.100 x86_64
Connected to:
YashanDB Server Personal Edition Release 23.1.0.100 x86_64 - X86 64bit Linux
SQL>
如上命令将datafile文件中数据根据|进行分隔,分别导入loadData表中的c1、c2和c3列字段中。
SELECT * FROM loadData;
C1 C2 C3
------------ ------------- ------------
1 load 101
2 load 201
exp工具是YashanDB的配套导出工具,提供元数据导出及CSV导出能力;
imp为YashanDB的配套导入工具,提供元数据导入能力。
用户可通过使用exp工具将YashanDB数据库中的表结构、索引、约束等所有数据生成一个元数据文件,该元数据文件可通过配套的导入工具imp导入至同构的YashanDB数据库中。或者通过exp工具将指定表结构排列的数据导出至CSV文件中,并通过yasldr工具将该CSV文件导入至YashanDB数据库中。
[yashan@localhost ~]$ yasql 'sys/"Admin@2023"'
YashanDB SQL Personal Edition Release 23.1.1.100 x86_64
Connected to:
YashanDB Server Personal Edition Release 23.1.1.100 x86_64 - X86 64bit Linux
CREATE USER import_user IDENTIFIED BY import;
GRANT DBA TO import_user;
conn import_user/import
Connected to:
YashanDB Server Personal Edition Release 23.1.0.100 x86_64 - X86 64bit Linux
CREATE TABLE test(c1 INT,c2 CHAR(10));
INSERT INTO test VALUES(1,'h'),(2,'a'),(3,'c');
CREATE TABLE test1(c1 INT,c2 INT);
INSERT INTO test1 VALUES(1,2),(3,4),(5,6);
COMMIT;
SQL> exit
$ exp 'sys/"Admin@2023"' FILE=export.owner.export OWNER=import_user
YashanDB Export Release 23.1.1.100 x86_64 297f388
export terminated successfully
cd /home/yashan
...
-rw-r----- 1 yashan yashan 16771 Aug 14 21:08 export.owner.export
...
$ yasql import_user/import
YashanDB SQL Personal Edition Release 23.1.1.100 x86_64
Connected to:
YashanDB Server Personal Edition Release 23.1.1.100 x86_64 - X86 64bit Linux
DROP TABLE test;
DROP TABLE test1;
COMMIT;
SELECT table_name FROM USER_TABLES;
TABLE_NAME
----------------------------------------------------------------
SQL> exit
$ imp sys/password FILE=export.owner.export FROMUSER=import_user
YashanDB Import Release 23.1.1.100 x86_64 297f388
import terminated successfully
$ yasql import_user/import
YashanDB SQL Personal Edition Release 23.1.1.100 x86_64
Connected to:
YashanDB Server Personal Edition Release 23.1.1.100 x86_64 - X86 64bit Linux
SQL> SELECT table_name FROM USER_TABLES;
TABLE_NAME
----------------------------------------------------------------
TEST1
TEST
SQL> SELECT * FROM test;
C1 C2
------------ -------------
1 h
2 a
3 c
SQL> SELECT * FROM test1;
C1 C2
------------ ------------
1 2
3 4
5 6