Table Management
New Table
grammar
create table 表名
(
列名1 类型(长度),
列名2 类型(长度),
列名3 类型(长度)
);
create table
: Keyword, followed by the construction of the table table name the new table, the table name length restrictions, no more than 32, the English word or abbreviation, which connects with an underscore between words- Table names must be unique in the database, duplicate error.
- Column name: English acronym composed of the word, not the length of more than 30, can not underscore the connection between the word starts with an underscore numbers, or special characters.
- After the end of the brackets must have a semicolon;
SQL> create table stu_test
2 (
3 stu_id number(6),
4 stu_name varchar2(50),
5 stu_age number(3)
6 );
表已创建。
add notes
SQL> comment on table stu_test is '学生表';
注释已创建。
SQL> comment on column stu_test.stu_id is '学生编号';
注释已创建。
SQL> comment on column stu_test.stu_name is '学生姓名';
注释已创建。
SQL> comment on column stu_test.stu_age is '学生年龄';
注释已创建。
- comment on table: the keywords that table notes
- comment on column: keyword column comments
Copy the table
Copy table structure, does not contain data
Copy table structure, does not contain data and does not contain indexes, constraints, comments
SQL> create table stu_test1 as
2 select stu_id,stu_name from stu_test
3 where 1=2;
表已创建。
Copy table structure, the data comprising
Copy table structure, and contain data, but does not include an index, constraints, comments
SQL> create table stu_test2 as
2 select stu_id,stu_name from stu_test
3 where 1=1;
表已创建。
New Partition Table
- Zoning principle: When the amount of data exceeds 2000W, consider using the partition table
Partition type | Type Description |
---|---|
range: | According range partitioning, usually in accordance with the time field partitions, such as application time, entry time |
list: | According to the distribution of partition, such as identity card numbers last |
hash: | In accordance with the hash value assigned partitions, does not guarantee a uniform distribution |
range partitioning example
SQL> create table emp_info
2 (
3 emp_no number(10),
4 join_date date
5 )
6 partition by range(join_date)
7 (
8 partition P1 values less than(to_date('20191120','YYYYMMDD')),
9 partition P2 values less than(to_date('20191121','YYYYMMDD')),
10 partition P3 values less than(to_date('20191122','YYYYMMDD')),
11 partition P_MAX values less than(maxvalue)
12 );
表已创建。
Keyword | description |
---|---|
partition by | Partition table is specified, range partitioning mode is determined, join_date partitioning key must be a table |
partition | Followed by partition name, partition name must be unique whole library, can not be repeated |
values less than | I.e., when the value is less than the partition key value of the subsequent data fall within the partitions |
maxvalue | The maximum partition |
When inserting data join_date date and time is less than 20, the partition into the p1
when the date and time is greater than 22 into P_MAX
list partitioning example
SQL> create table LIST_TAB_TEST
2 (
3 id_no number(10),
4 partition_id varchar2(2)
5 )
6 partition by list(partition_id)
7 (
8 partition P_0 values('0'),
9 partition P_1 values('1'),
10 partition P_2 values('2'),
11 partition P_default values(default)
12 );
表已创建。
Keyword | description |
---|---|
values: | That value is 0. When the partition key value represents a value subsequent data fall within the partitions |
default: | Key, the default value, when the value is not within the previous partition value of the partition key, the data fall within the partitions |
Query data for each partition
grammar
select * from 表名 partition(分区名);
SQL> insert into emp_info
2 values(1,sysdate);
已创建 1 行。
SQL> select * from emp_info partition(p3);
EMP_NO JOIN_DATE
---------- --------------
1 21-11月-19
SQL> insert into list_tab_test
2 values(1,6);
已创建 1 行。
SQL> select * from list_tab_test partition(P_default);
ID_NO PART
---------- ----
1 6
View the partition builtselect * from dba_part_key_columns where owner='SCOTT';
Delete table
SQL> drop table emp_info;
表已删除。
SQL> drop table list_tab_test;
表已删除。
Increase Column
SQL> create table student6
2 (
3 id number primary key
4 );
表已创建。
Increase in single row
SQL> alter table student6
2 add s_mpno number(6);
表已更改。
Increase the number of columns
SQL> alter table student6
2 add(s_name varchar2(50),s_deptno number(2));
表已更改。
Modify the name and attributes of table columns
Modify the table and column names
Modify the table name
SQL> alter table student6 rename to student_6;
表已更改。
Modify the column name
SQL> alter table student_6 rename column id to s_id;
表已更改。
Modify the properties of the column
The number to the number s_id (. 6)
SQL> alter table student_6 modify s_id number(6);
表已更改。
The s_mpno set to a unique, s_name provided nonempty
SQL> alter table student_6
2 modify(s_mpno number(6) unique,
3 s_name varchar2(50) not null);
表已更改。
Under existing data, the change column properties
The column properties stu_info stu_no table by number to varchar2
SQL> select * from stu_info;
STU_NO STU_NAME STU_AGE
------- -------- ---
1 张三 50
2 李四 50
3 王五 51
1, the stu_no empty column, and then change the type of the last data recovery
The first step, clearing the data, but the data can not be lost
SQL> alter table stu_info add stu_no1 number(20);
表已更改。
SQL> update stu_info set stu_no1=stu_no;
已更新3行。
SQL> update stu_info set stu_no=null;
已更新3行。
SQL> commit;
提交完成。
SQL> select * from stu_info;
STU_NO STU_NAME STU_AGE STU_NO1
---------- -------------- -------------- ----------
张三 50 1
李四 50 2
王五 51 3
The second step, the type of modification
SQL> alter table stu_info modify stu_no varchar(10);
表已更改。
The third step, data recovery, delete and add rows
SQL> update stu_info set stu_no=stu_no1;
已更新3行。
SQL> alter table stu_info drop column stu_no1;
表已更改。
Remove Columns
Delete single row
SQL> alter table stu_info drop column stu_no;
表已更改。
Delete multiple columns
SQL> alter table student_6 drop(s_mpno,s_deptno);
表已更改。