表和字段相关操作

--创建测试表
create or replace table student
(
xh number(4), --学号
xm varchar2(10), --姓名
sex char(2), --性别
birthday date, --日期
sal number(7,2) --奖学金
);

--添加一个字段
alter table student add (studentid number(10));

--添加多个字段
alter table student add 
(
xh number(4), --学号
xm varchar2(10), --姓名
sex char(2), --性别
birthday date, --日期
sal number(7,2) --奖学金
);

--删除一个字段
alter table student drop column xh;

--修改一个字段
--1.修改长度
alter table student modify (sex char(5));
--2.修改字段的类型
alter table student modify (sex varchar2(5));

--给表改名字
rename student to stuinfo;

--字段如何改名字
--1.先删除
alter table student drop column sal; 
--2.再添加
alter table student add(salary varchar2(6));

--数据完整性约束条件的修改 
--1.删除约束条件: 
--1.1删除主键约束: 
alter table product drop primary key; 
alter table product drop constraint p_id_pk; 
--1.2删除唯一性约束: 
alter table product drop unique(p_name); 
alter table product drop constraint p_id_uk;

--1.3删除非空约束 
alter table product modify(p_date null);

--1.4删除缺省值: 
alter table product modify(p_date default null);

--2.增加约束条件 
--2.1增加主键约束: 
alter table product add primary key(p_id); 
alter table product add constraint p_id_pk primary key (p_id);

--2.2增加非空约束 
alter table product modify ( p_date not null);

--2.3增加缺省值 
alter table product modify (p_date default sysdate-1); 
--查询缺省值
select table_name, column_name, data_default from user_tab_columns;

--创建表并添加约束
create table t_sequence
(
tablename varchar2(50) not null,
prestr varchar2(20),
endstr varchar2(20),
step number(10) default 1 not null, 
curval number(10) default 1 not null,
mn number(10) default 1 not null,
mx number(10) default 100000000 not null,
length number(10) not null,
constraint "pk_t_sequence" primary key(tablename),
constraint "check_step" check(step>0),
constraint "check_length" check(length>0),
constraint "compare_mx_mn" check(mx>mn)
);

--增加非空约束 
alter table t_cms_copyright modify
(
issuetime null, --发行时间
lyricauthor null, --词作者
songauthor null --曲作者
);
原文地址:https://www.cnblogs.com/huangbiquan/p/7763419.html