数据库表的基本操作1

tech2025-05-26  5

数据库表的基本操作

– 创建表 mytable

create table mytable( myid char(10), myname varchar2(10) default ‘myname’, myage number(3,0) );

– 显示表结构

desc mytable;

– 根据现在已经存在的表创建新表

create table newTable(newId, newName, newAge) as (select myid, myname, myage from mytable);

desc newTable

– 说明:

– 在表名后定义新表的列名 – 顶顶一的列名与查询中的列名一一对应 有先后顺序 – 也可以直接省略列名 那么新创建的表的列名 是和查询的列名一样的

– 对表结构进行修改

– 添加一列 – 添加 学号列 alter table newTable add sno char(12); desc newTable

– 修改列

– 注意 – 1 如果表中已经含有数据 类型是不能进行更改的 长度只能变大 – 2 默认值 只对新添加的数据 有效 alter table newTable modify sno char(3); insert into scott.newTable values (1,‘kongge’,12,‘1’); alter table newTable modify sno char(10);

– 删除列

alter table newTable drop column sno; desc newTable

– 修改表 名

rename newTable to new_table;

– 修改列 名

alter table new_table rename column newId to new_new_id desc new_table

– 截断表 相当于把数据全部删除 但是结构依然是存在的

– 一般用于将测试数据进行删除 truncate table new_table;

– 删除表

drop table new_table;

如果在 插入时出现SQL 错误: ORA-01950: 对表空间 ‘USERS’ 无权限可以执行如下操作

在 sys 中对用户赋权

alter user 用户名 quota unlimited on USERS; alter pluggable database pdborcl open; alter session set container = pdborcl;
最新回复(0)