create database name; 创建数据库
use database name; 选择数据库
drop database name 直接删除数据库,不提醒
show tables; 显示表
describe tablename; 表的详细描述
select 中加上distinct去除重复字段
mysqladmin drop database name 删除数据库前,有提示。
显示当前mysql版本和当前日期
select version(),current_date;
重命名表:
mysql > alter table t1 rename t2;
删除
mysql>
drop table depart_pos;
drop table department;
drop table s_position;
drop table staffer;
drop database staffer;
创建索引
alter table table1 add index ind_id (id);
create index ind_id on table1 (id);
create unique index ind_id on table1 (id);//建立唯一性索引
删除索引
drop index idx_id on table1;
alter table table1 drop index ind_id;
联合字符或者多个列(将列id与":"和列name和"="连接)
select concat(id,':',name,'=') from students;
MySQL不支持的功能
事务,视图,外键和引用完整性,存储过程和触发器
MySQL会使用索引的操作符号
=,>,=,between,in,不带%或者_开头的like
使用索引的缺点
1) 减慢增删改数据的速度;
2)占用磁盘空间;
3)增加查询优化器的负担;
当查询优化器生成执行计划时,会考虑索引,太多的索引会给查询优化器增加工作量,导致无法选择最优的查询方案;
查询时间:select now();
查询当前用户:select user();
查询数据库版本:select version();
查询当前使用的数据库:select database();
使用较短的定长列
1)尽可能使用较短的数据类型;
2)尽可能使用定长数据类型;
a)用char代替varchar,固定长度的数据处理比变长的快些;
b)对于频繁修改的表,磁盘容易形成碎片,从而影响数据库的整体性能;
c)万一出现数据表崩溃,使用固定长度数据行的表更容易重新构造。使用固定长度的数据行,每个记录的开始位置都是固定记录长度的倍数,可以很容易被检测到,但是使用可变长度的数据行就不一定了;
d)对于MyISAM类型的数据表,虽然转换成固定长度的数据列可以提高性能,但是占据的空间也大;
使用not null和enum
尽量将列定义为not null,这样可使数据的出来更快,所需的空间更少,而且在查询时,MySQL不需要检查是否存在特例,即null值,从而优化查询;
如果一列只含有有限数目的特定值,如性别,是否有效或者入学年份等,在这种情况下应该考虑将其转换为enum列的值,MySQL处理的更快,因为所有的enum值在系统内都是以标识数值来表示的;
删除表
mysql>drop TABLE MYTABLE;
清空表
mysql>delete from MYTABLE;
修改结构
mysql>
#表position增加列test
alter table position add(test char(10));
#表position修改列test
alter table position modify test char(20) not null;
#表position修改列test默认值
alter table position alter test set default 'system';
#表position去掉test默认值
alter table position alter test drop default;
#表position去掉列test
alter table position drop column test;
#表depart_pos删除主键
alter table depart_pos drop primary key;
#表depart_pos增加主键
alter table depart_pos add primary key PK_depart_pos (department_id,position_id);