当前位置:Gxlcms > 数据库问题 > oracle 表的管理

oracle 表的管理

时间:2021-07-01 10:21:17 帮助过:9人阅读


--学生表
create table student (
   xh number(4), --学号
   xm varchar2(20), --姓名
   sex char(2), --性别
   birthday date, --出生日期
   sal number(7,2) --奖学金
);

--班级表
create table class(
  classid number(2),
  cname varchar2(40)
);

      

--修改表
--添加一个字段
sql>alter table student add (classid number(2));
--修改一个字段的长度
sql>alter table student modify (xm varchar2(30));
--修改字段的类型或是名字(不能有数据) 不建议做
sql>alter table student modify (xm char(30));
--删除一个字段 不建议做(删了之后,顺序就变了。加就没问题,应该是加在后面)
sql>alter table student drop column sal;
--修改表的名字 很少有这种需求
sql>rename student to stu;

          
--删除表
sql>drop table student;

           

--添加数据
--所有字段都插入数据
insert into student values (‘a001‘, ‘张三‘, ‘男‘, ‘01-5 月-05‘, 10);
--oracle中默认的日期格式‘dd-mon-yy’ dd 天 mon 月份 yy 2位的年 ‘09-6 月-99’ 1999年6月9日
--修改日期的默认格式(临时修改,数据库重启后仍为默认;如要修改需要修改注册表)
alter session set nls_date_format =‘yyyy-mm-dd‘;
--修改后,可以用我们熟悉的格式添加日期类型:
insert into student values (‘a002‘, ‘mike‘, ‘男‘, ‘1905-05-06‘, 10);
--插入部分字段
insert into student(xh, xm, sex) values (‘a003‘, ‘john‘, ‘女‘);
--插入空值
insert into student(xh, xm, sex, birthday) values (‘a004‘, ‘martin‘, ‘男‘, null);
--问题来了,如果你要查询student表里birthday为null的记录,怎么写sql呢?
--错误写法:select * from student where birthday = null;
--正确写法:select * from student where birthday is null;
--如果要查询birthday不为null,则应该这样写:
select * from student where birthday is not null;

         

--修改数据
--修改一个字段
update student set sex = ‘女‘ where xh = ‘a001‘;
--修改多个字段
update student set sex = ‘男‘, birthday = ‘1984-04-01‘ where xh = ‘a001‘;
--修改含有null值的数据
不要用 = null 而是用 is null;
select * from student where birthday is null;

        

--删除数据
delete from student; --删除所有记录,表结构还在,写日志,可以恢复的,速度慢。
--delete的数据可以恢复。
savepoint a; --创建保存点
delete from student;
rollback to a; --恢复到保存点
一个有经验的dba,在确保完成无误的情况下要定期创建还原点。

drop table student; --删除表的结构和数据;
delete from student where xh = ‘a001‘; --删除一条记录;
truncate table student; --删除表中的所有记录,表结构还在,不写日志,无法找回删除的记录,速度快。

参见:http://www.cnblogs.com/linjiqin/archive/2012/02/01/2335035.html

oracle 表的管理

标签:路径   add   记录   日期类型   col   结构   奖学金   day   sid   

人气教程排行