Mysql常用语法

0、
新建数据库
create database samp_db;
删除整个数据库
drop database 数据库名;
drop database samp_db; 
查看所有的数据库:
show databases;

1、
创建用户 用户授权
create user demo@`%` identified by 'demo';
grant all on demo.* to demo@`%`;
flush privileges ;


update mysql.proc set definer='demo@%' where db='demo'; 

2、 
创建表
create table 表名称(列声明);
create table students
(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);

DML相关:
insert into students values(NULL, "王刚", "男", 20, "13811371377");
select * from students where age > 21;
update students set tel=default where id=5;
delete from students where id=2;

增加列
alter table 表名 add 列名 列数据类型 [after 插入位置];
alter table students add birthday date after age;
修改列 
alter table 表名 change 列名称 列新名称 新数据类型;
alter table students change tel telphone char(13) default "-";
alter table students change name name char(16) not null;
删除列
alter table 表名 drop 列名称;
alter table students drop birthday;
重命名表
alter table 表名 rename 新表名;
alter table students rename workmates;
删除表
drop table workmates; 

======================================================================================================================================================================== 
一、序列
不支持序列 提供了auto_increment

二、索引
1、创建
create [unique\fulltext\spatial] index index_name on tablename(columname); 
2、设计索引的原则
a.最合适的索引列是出现在where子句中列,或连接子句中指定的列,而不是出现在select关键字后面的选择列表的列
b.使用唯一索引,需要考虑列中某个值得分布,如果索引列种的基数越大,则索引的效果越好。举个例子:订单号就可以设置唯一索引,因为订单号的不一样。而对于rowstatus就无须了,因为rowstatus要么是有效要么是无效。这样的筛选出的范围还是很多,没有意义
c.不要过度索引。因为所有也要占用额外的磁盘空间,如果一个索引很少使用,那么会不必要的减缓表的修改速度

三、视图:不支持子查询
1、创建
create or replace view v_students as select * from v_students ;
drop view v_students;

四、存储过程、函数
http://www.cnblogs.com/lxs1314/p/5945428.html

五、触发器
http://www.cnblogs.com/nicholas_f/archive/2009/09/22/1572050.html

六、事件调度器(Event Scheduler) oracle:DBMS_jobs
http://blog.csdn.net/mer1234567/article/details/7514855

七、不支持自定义类型

八、系统函数
http://www.jb51.net/article/40179.htm

九、关于mysql大小写处理方法

问题背景:
oracle中不存在大小写区分问题,库里保存的表都是大写,导致导入到mysql全部变成了大写,(navicat在导出时未设置对象名为小写导致)
mysql中的每个表至少对应数据库目录中的一个文件(也可能是多个,取决于存储引擎)。
因此,所使用操作系统的大小写敏感性决定了数据库名和表名的大小写敏感性。
查看操作系统是否区分大小写:
SHOW VARIABLES LIKE '%lower_case_file_system%';
ON为不区分,OFF为区分,linux下是OFF。 
linux下:
数据库名与表名,严格区分大小写 ;
表的别名,严格区分大小写;
变量名,严格区分大小写;
列名与列的别名在所有的情况下均是忽略大小写的;

对于已存在的库,表名全部是大写的情况:
1、拼凑sql语句(rename语句),批量处理表名。
2、设置my.cnf文件 lower_case_tables_name=1;保证客户端sql书写无需区分大小写。
3、命名规则:后期新建表的命名规则: 定义数据库、表、列的时候全部采用小写字母加下划线的方式,不使用任何大写字母

十、Mysql使用注意:
1、没有闪回。没有调试功能。
2、Navicat客户端mysql自动提交事务(update、delete) 可使用如下方法修改 手动提交/回滚事务

  show variables like 'autocommit';
set autocommit = 0;   --  作用域为当前事务  非系统全局设置

select * from  proposal t where t.zh_title = 'Test 0815 青岛海洋科学与技术国家实验室 2016年度开放基金项目申请书';

update proposal t set t.zh_title = 'AAAA' where t.zh_title = 'Test 0815 青岛海洋科学与技术国家实验室 2016年度开放基金项目申请书'; 
select * from  proposal t where t.zh_title = 'AAAA';

delete  from proposal where   zh_title = 'AAAA'; 
select * from  proposal t where t.zh_title = 'AAAA';

-- 回滚和提交操作
ROLLBACK ; 
      3、大小写
     创建数据库、表、列时,全部采用小写字母加下划线的方式,不使用任何大写字母
      4、尽量少使用存储过程、函数。不方便调试、迁移、扩展。
      5、对于oracle中的过程,最好保留包名。如appprj.SP_ProposalToProject,迁移到mysql存储过程名为appprj_SP_ProposalToProject
      6、不得使用外键与级联,一切外键概念必须在应用层解决。
      7、禁用保留字,如desc、range、match、delayed、key等,请参考MySQL官方保留字。
         对于oracle迁移过来的,已存在的保留字,sql使用``转义 ,如`key` 。
     7、小数类型为decimal,禁止使用float和double。
     8、char与varchar:
   如果存储的字符串长度几乎相等,使用char定长字符串类型。 
   varchar是可变长字符串,不预先分配存储空间,长度不要超过5000,
   如果存储长度大于此值,定义字段类型为text,独立出来一张表,用主键来对应,避免影响其它字段索引效率。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值