达梦数据库的表管理

达梦数据库的表管理

建表是最基本的动作了,建表前有必要稍微了解下字段的一些特性,比如:字段数据类型、字段默认缺省值、自增列等等。
达梦数据库支持的表有:默认的表(索引组织表),堆表,临时表,分区表,外部表等
数据类型有:int char varchar date clob blob number等

1、使用test1用户
重新给test1用户赋管理员权限:grant dba to test1;
我们有了新建的test1用户,那么接下来我们要用test1用户来操作了。
切换用户命令:conn TEST1/TEST123456
或者新开一个窗口连接:disql TEST1/TEST123456

2、创建新表
尝试创建一个学生表和课程表,后续做些测试使用
create table study
(
study_id int not null ,
name varchar(20) not null ,
sex char(1),
age int,
tel varchar(15),
address varchar(50),
primary key(study_id)
)
storage(on tbs1);
comment on table study is '学员信息表';
comment on column study.study_id is '学员编号';
comment on column study.name is '姓名';
comment on column study.sex is '性别';
comment on column study.age is '年龄';
comment on column study.tel is '电话';
comment on column study.address is '家庭住址';

create table course
(
study_id int not null ,
course_id int not null ,
course_name varchar(20) not null,
primary key(course_id),
foreign key(study_id) references study(study_id)
)
storage(on tbs1);
comment on table course is '课程表';
comment on column course.study_id is '学员编号';
comment on column course.course_id is '课程编号';
comment on column course.course_name is '课程名称';

初始化几条测试记录
insert into STUDY(STUDY_ID, NAME, SEX, AGE, TEL, ADDRESS)
VALUES(11, '学生1', '1', 21, 12345678901, NULL);
insert into STUDY(STUDY_ID, NAME, SEX, AGE, TEL, ADDRESS)
VALUES(12, '学生2', '1', 22, 12345678902, NULL);
insert into STUDY(STUDY_ID, NAME, SEX, AGE, TEL, ADDRESS)
VALUES(13, '学生3', '2', 23, 12345678903, NULL);

insert into COURSE(STUDY_ID, COURSE_ID, COURSE_NAME)
VALUES(11, 1, '语文');
insert into COURSE(STUDY_ID, COURSE_ID, COURSE_NAME)
VALUES(11, 2, '数学');
insert into COURSE(STUDY_ID, COURSE_ID, COURSE_NAME)
VALUES(11, 3, '英语');

3、查看当前用户所有表对象
select owner,table_name,tablespace_name from dba_tables where owner='TEST1';
达梦数据库的表管理

4、创建表其他功能测试
创建表有缺省值字段
create table tab1(c1 int , c2 varchar(10) default 'female');
insert into tab1(c1) values(1);
insert into tab1 values( 2, 'male');

创建表有自增列字段
create table tab2(c1 int identity(1,2), c2 char);

insert into tab2(c2) values('a');
insert into tab2(c2) values('b');
select * from tab2;

查询种子值,执行如下SQL命令
select IDENT_SEED('tab2');
查询增量值,执行如下SQL命令
select IDENT_INCR('tab2');

创建表有虚拟列
create table tab3 (
id varchar(15),
c1 varchar(15) as (substr(id,0,6)) VIRTUAL);
insert into tab3 (id) values('1234567890');
select * from tab3;

5、修改表结构
修改表结构,增加字段
alter table tab1 add column C3 varchar(10);
达梦数据库的表管理

修改表结构,删除字段
alter table tab1 drop column C3;
达梦数据库的表管理

6、删除表结构
drop table tab2;
达梦数据库的表管理

猜你喜欢

转载自blog.51cto.com/14615334/2452407