数据库操作中,对表和表字段的增删改查是常用的操作 今天复习了一下 顺便加到博客里面
话不多说我们从最简单的开始
创建表
语法:create table 表名(字段名1 字段类型(长度) 字段是否为空,字段名2,字段类型(长度) 字段是否为空)
在建立表格oracle常用的字段类型有:
char 固定长度的字符串
varchar2 可变长度的字符串
number(m,n) 数字型m是位数总长度, n是小数的长度
date 日期类型
示例:
先建立主键参考表
create table t_stu (
stu_id char(5) not null,
stu_name varchar2(8) not null,
constraint pk_t_stu primary key (stu_id)
);主键和外键一起建立:
create table t_score (
exam_score number(5,2),
exam_date date,
autoid number(10) not null,
stu_id char(5),
sub_id char(3),
constraint pk_t_score primary key(autoid),
constraint fk_t_score_refe foreign key(stu_id)
references t_stu (stu_id)
);我们这里建表时同时加入了 主键和外键的约束,翻译一下就是
建立主键为autoid主键名称为pk_t_score,
建立外键stu_id 参考表t_stu的stu_id
1.表
1.1修改表名
语法:alter table 旧表名 rename to 新表名
示例:alter table t_stu rename to t_stu1;
1.2删除表
语法:drop table 表名
示例:drop table test_table
2.表字段
2.1增加表字段
语法:alter table 表名 add 字段名字 字段描述
示例:alter table t_stu add high;
2.2删除表字段
语法:alter table 表名 drop column 字段名
示例:alter table t_stu drop column high;
2.3修改表字段
语法:alter table rename column 旧字段名 to 新字段名
示例:alter table t_stu1 rename column stu_id to t_stu2;