mysql和oracle常用操作

数据库

一、 ORACLE

1.创建表格并插入数据

create table demo

(

  id                NUMBER not null,

  org_id            NUMBER not null,

 email      VARCHAR2(100) not null,

  enabled           NUMBER default 1 not null

);

COMMENT ON table demo IS '20171017 add 添加客户类型问题处理完毕需要抄送的邮箱';

comment on column demo.id is '唯一ID';

comment on column demo.org_id is '20171017 add 客户类型ID';

comment on column demo.inform_email is '20171017 add 问题处理完毕需要通知的客户邮箱';

insert into demo(ID,ORG_ID,EMAIL,ENABLED) values(1,133,'123@sss.com',1);

2.ORACLE 创建自增序列

create sequence SEQ_TABLE_ICRE_ID

minvalue 1

maxvalue 999999999999999999999999999

start with 60

increment by 1

cache 20;

SEQ_USER_TABLE_ICRE_ID  start with 30;

3.ORACLE修改字段类型

alter table tbs_demo modify(fieldName varchar2(3000));

4.ORACLE增加列

alter table TBS_DEMO ADD(SOURCE_ID NUMBER(3) default 1 NOT NULL);

comment on column TBS_DEMO."SOURCE_ID" is '[新增2017/3/17新增]1-source1  2-source2';

5.在Oracle中,null和’’(空字符串)是一个意思。

6.update某个日期之前的工单

update TABLENAME SET status=20 WHERE created_date <= to_date('2017-06-05 23:59:59', 'yyyy-mm-dd hh24:mi:ss')

 

二、MYSQL

mysql添加表注释、字段注释、查看与修改注释

1 创建表的时候写注释

create table test1

(

field_name int comment '字段的注释'

)comment='表的注释';

e.g.:

CREATE TABLE t_session(

   id bigint(20) NOT NULL comment '自增ID',

   session_id varchar(200) NOT NULL comment 'sessionId',

   content text comment 'session内容,json字符串',

   expire_time int(11) comment '有效期长,单位为秒',

   update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间,更新时自动更新',

   create_time timestamp not null default current_timestamp comment '创建时间,插入时自动插入当前时间',

   enabled tinyint(2) NOT NULL default 1 comment '是否有效,1-有效,0-无效,默认值为1'

)comment='20180116新增,存放session';

 

2 修改表的注释

alter table test1 comment '修改后的表的注释';

 

3 修改字段的注释

alter table test1 modify column field_name int comment '修改后的字段注释';

--注意:字段名和字段类型照写就行

 

4 查看表注释的方法

--在生成的SQL语句中看

show create table test1;

--在元数据的表里面看

use information_schema;

select * from TABLES where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G

5 查看字段注释的方法

--show

show full columns from test1;

--在元数据的表里面看

select * from COLUMNS where TABLE_SCHEMA='my_db' and TABLE_NAME='test1' \G

来自 <https://www.cnblogs.com/xsj1989/p/6795382.html>

MySQL添加字段和修改字段等操作的实现方法,希望对您学习MySQL添加字段方面会有所帮助。

1.登录数据库

>mysql -u root -p 数据库名称

2.查询所有数据表

>show tables;

3.查询表的字段信息

>desc 表名称;

4.修改表字段

4.1添加表字段

alter table table1 add transactor varchar(10) not Null;

alter table table1 add id int unsigned not Null auto_increment primary key

4.2.修改某个表的字段类型及指定为空或非空

>alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空];

>alter table 表名称 modify 字段名称 字段类型 [是否允许非空];

>alter table 表名称 modify 字段名称 字段类型 [是否允许非空];

4.3.修改某个表的字段名称及指定为空或非空

>alter table 表名称 change 字段原名称 字段新名称 字段类型 [是否允许非空

4.4如果要删除某一字段,可用命令:ALTER TABLE mytable DROP 字段名;

Example:

#alter table fiscal_year add testField smallint(6) comment '测试字段'

#alter table fiscal_year drop testField

来自 <http://database.51cto.com/art/201011/234549.htm>

5.查询数据库中所有表的主键和数量

SELECT  

  t.TABLE_NAME,  

  t.CONSTRAINT_TYPE,  

  c.COLUMN_NAME,  

  c.ORDINAL_POSITION  

FROM  

  INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t,  

  INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c,  

  information_schema.TABLES AS ts  

WHERE  

  t.TABLE_NAME = c.TABLE_NAME  

  -- AND t.TABLE_SCHEMA = 数据库名称  

  AND t.CONSTRAINT_TYPE = 'PRIMARY KEY';  

来自 <http://blog.csdn.net/cai843383655/article/details/53906257>

6.修改主键

先删除主键

alter table table_test drop primary key;

然后再添加主键

alter table table_test add primary key(id);

注:在添加主键之前,必须先把重复的id删除掉。

来自 <http://blog.csdn.net/jaray/article/details/19814351>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值