MySQL建表:
create table XXX(
id int not null auto_increment,
name varchar(100) null,
sdate date,
primary key(id)
);
ORACLE建表
create table XXX(
id number not null primary key, --自增与mysql不同,要用序列
name varchar2(100) null, --不同varchar2
sdate date
);
commit; --ORCACLE一定要加上
create sequence p_pro start with 1 increments by 1; --创建序列,从1开始每次加1
MySQL增删改
insert into XXX(name,pdate) values('张三',now());
--需要在表名后面添加该表除了主键的所有字段
delete from XXX where id=1;
--删除所有数据,不会影响表结构,数据可以恢复,只要没有commit
truncate table XXX
--删除所有数据,不会影响表结构,不会记录日志,数据不能恢复
drop table XXX
--删除所有数据,包括表结构一并删除,不会记录日志,数据不能恢复
update XXX set name='李四' where id='2';
ORACLE增删改
insert into XXX values(p_pro,'张三','2012-12-22');
commit;
delete from XXX; --ORACLE中没有from也可以,MySQL不行没有from
truncate table XXX
--删除所有数据,不会影响表结构,不会记录日志,数据不能恢复
drop table XXX
--删除所有数据,包括表结构一并删除,不会记录日志,数据不能恢复
update XXX set name='李四' where id='2';
MySQL修改表结构
--添加字段
alter table XXX add (sex number(2));
--添加多列
alter table XXX add (c1 number,c2 number);
--修改字段数据类型
alter table XXX modify name varchar2(50);
--修改多列
alter table XXX modify (c1 int,c2 int);
注:增加字段长度时很顺利;变更数据类型,可能需要清空当前列数据。
--修改字段名称
alter table XXX change name pname int(11) not null;
--修改表名
alter table XXX rename XXXX;
--删除一列
alter table XXX drop pname;
--添加注释(在建表时没有添加注释)
alter table XXX change column id id int not null default 0 comment '测试表id'
--创建表的时候写的注释
create table test1 (
field_name int comment '字段的注释'
)comment='表的注释';
--添加主键约束,约束都与ORACLE不同
alter table XXX add primary key id;
--添加唯一约束
alter table XXX add unique(id);
--添加非空约束
alter table XXX modify id not null;
--添加外键约束
alter table XXX add foreign key(uid) references XXXX(uid) ;
--添加默认值约束,没有检查约束
alter table XXX add name varchar(50) not null default 'abc';
ORACLE修改表结构
--添加字段
alter table XXX add (sex number(2));
--添加多列
alter table XXX add (c1 number,c2 number);
--修改字段数据类型
alter table XXX modify name varchar2(50);
--修改多列
alter table XXX modify (c1 int,c2 int);
注:增加字段长度时很顺利;变更数据类型,可能需要清空当前列数据。
--修改字段名称
alter table XXX rename column name to pname;
--修改表名
alter table XXX rename to XXXX;
--删除一列
alter table XXX drop column pname;
--添加注释
comment column on XXX.id is '注释内容';
--给表添加注释
comment on table XXX is '注释内容'
--添加主键约束
alter table XXX add constraint p_ky primary key id;
--添加唯一约束
alter table XXX add constraint U_k unique(id);
--添加非空约束
alter table XXX modify id not null;
--添加外键约束
alter table XXX add constraint fk_y foreign key(uid) references XXXX(uid) on delete cascade;
通过 on delete cascade 子句指定该外键列可级联删除;删除父表中的对应行,会将对应子表中的所有匹配行的外键约束列置为NULL,通过 on delete set null 子句实施
--添加检查约束
alter table XXX add constraint ck_y check(sex in('男','女'));