[Oracle]表的增删改查

来源:互联网 发布:网络学校国家承认吗 编辑:程序博客网 时间:2024/05/30 04:25

Oracle(表的管理)

表的命名规则

l        必须以字母开头

l        长度不能超过30个字符

l        不能用关键字,保留字

l        只能使用字母,数字,$,#

数据类型

n        字符型

char定长 最大2000字符,没有被占用的会用空格补全(!!查询的速度很快)

varchar变长 最大4000字符

clob字符型大对象,最大4g

n        数字型

number范围10-38次方到1038次方

number(5,2)5位,2个小数

number(5)一个五位整数

n        日期类型

date年月日时分秒

timestamp

n        blob 图片,声音,最大4g(很高的保密性)

 

建表:

SQL> --学生表SQL> create table student(  2  xh number(4),  3  xm varchar(20),  4  sex char(2),  5  birthday date,  6  sal number(7,2)  7  ); 表已创建。SQL> create table class(  2  classid number(2),  3  cname varchar2(40)  4  );


对表的操作(字段的操作,记录的操作)

            SQL> --添加一个字段SQL> alter table student add (classid number(2)); Table alteredSQL> --修改字段的类型、长度或者是名字(不能有数据)SQL> alter table student modify(xm varchar(30)); Table alteredSQL> --删除一个字段SQL> alter table student drop column sal; Table altered--修改表的名字:rename student to stu;--删除表drop table student;            SQL> --添加一个字段SQL> alter table student add (classid number(2)); Table alteredSQL> --修改字段的类型、长度或者是名字(不能有数据)SQL> alter table student modify(xm varchar(30)); Table alteredSQL> --删除一个字段SQL> alter table student drop column sal; Table altered--修改表的名字:rename student to stu;--删除表drop table student;

表的增删改查的操作:

l        添加数据

注意日期型数据SQL> --添加数据SQL> insert into student values(1,'xiaoming','男','1997-12-01',2); insert into student values(1,'xiaoming','男','1997-12-01',2) ORA-01861: 文字与格式字符串不匹配SQL> insert into student values(1,'xiaoming','男','01-12月-1997',2);1 row inserted!!!!Oracle日期类型的默认格式是日-月-年)解决的办法:SQL> --修改日期的默认格式SQL> alter session set nls_date_format='yyyy-mm-dd';Session altered            插入空值:插入空的时候用null,查询字段为空的记录时候用                                                SQL> insert into student values(2,'xiaoming',,,2);                                               insert into student values(2,'xiaoming',,,2)                                               ORA-00936: 缺失表达式(不能这样写)                                                !!!SQL> select *from student where sex is null;


l        修改记录

SQL> update student set sex='女' where xh=1;1 row updated

l        删除记录

SQL> delete from student where sex is 

回滚的用法:

     SQL> insert into student values(3,'xiaoming',null,null,2); 1 row inserted--创建保存点SQL> savepoint aa; Savepoint created--进行操作SQL> delete from student; 1 row deleted--进行回滚SQL> rollback to aa; Rollback complete--回滚成功SQL> select *from student;    XH XM                             SEX BIRTHDAY    CLASSID----- ------------------------------ --- ----------- -------3 xiaoming   快速删除表,没有日志记录Truncate table 

 

 

0 0