oracle 数据库的建表以及字段的增删改查
2016-02-17 15:25
477 查看
数据库操作中,对表和表字段的增删改查是常用的操作 今天复习了一下 顺便加到博客里面
话不多说我们从最简单的开始
创建表
语法: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;
话不多说我们从最简单的开始
创建表
语法: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;
相关文章推荐
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- Oracle Containers for J2EE远程安全漏洞(CVE-2014-0413)
- Oracle 10g R2不能使用EM的问题
- 表空间操作
- PreparedStatement中in子句的处理
- VMware下RedHat4.8_64位安装Oracle 10g RAC--简略脚本
- oracle sql日期比较
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- OS block size和Oracle block size,查找OS Blocksize的方法
- oracle中创建数据库和表空间的几点总结
- 数据库自动备份脚本
- oracle的nvl函数的使用介绍
- 解决oracle用户连接失败的解决方法
- oracle的一些tips技巧
- Oracle 下的开发日积月累
- Oracle存储过程之数据库中获取数据实例
- Windows下ORACLE 10g完全卸载的方法分析
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ORACLE LATERAL-SQL-INJECTION 个人见解