create database db_name character set charset collate collation; create table tb_name (...) character set charset collate collation; name char(10) character set charset collate collation;
create table log_partition {...} partition by range(year(dt)) ( partition p0 values less than (2005), partition p1 values less than (2006), partition p2 values less than (2007), partition p0 values less than MAXVALUE );
create table tb_name (...) engine = federated connection = 'mysql://user:pass@host/db/table';
create server serv_name foreign data wrapper mysql options (user 'user',passoword 'pass',host 'host',database 'db'); connection = 'serv_name/tb_name'
create table tb_name (...) engine='merge' union (tb1,tb2,tb3 ...)
create table tb_name like tb_name_orign; insert into tb_name select * from tb_name;
这样先创建了一个和原始表结构完全一样的表,包括索引等,然后将数据从其他表中导入。
方法2
create table tb_name select * from tb_orign where ...;
create table mytbl (id int ,ts time, value decimal) select 1 as id ,cast(curtime() as time) as ts, cast(PI() as decimal) as d;
create temporary table tb_name ...; drop temporary table tb_name;
Drop [temporary] table [if exists] tb_name;
create table tb_name ( ... index_type idx_name [using btree](column(n)) ... )
alter table tb_name add Index_type index_name (column(n) ...);
create index_type index idx_name on tb_name (column ...);
drop index idx_name on tbl_name; alter table tbl_name drop inx_name; //删除主键 drop index primary on tbl_name; alter table tbl_name drop primary key;
修改字段类型
alter table tbl_name modify col type...; alter table tbl_name change old new_col type...;
alter table tb_name engine = 'new_engine';
alter table db.tbl_name rename to db.new_name; rename table db.tbl_name to db.new_name; //当表名位于不同数据库时,将把数据复制到目的库表
SHOW AUTHORS SHOW CHARACTER SET [like_or_where] SHOW COLLATION [like_or_where] SHOW [FULL] COLUMNS FROM tbl_name [FROM db_name] [like_or_where] SHOW CREATE DATABASE db_name SHOW CREATE EVENT event_name SHOW CREATE FUNCTION funcname SHOW CREATE PROCEDURE procname SHOW CREATE TABLE tbl_name SHOW CREATE TRIGGER trigger_name SHOW CREATE VIEW view_name SHOW DATABASES [like_or_where] SHOW ENGINE engine_name {STATUS | MUTEX} SHOW [STORAGE] ENGINES SHOW ERRORS [LIMIT [offset,] row_count] SHOW [FULL] EVENTS SHOW FUNCTION CODE sp_name SHOW FUNCTION STATUS [like_or_where] SHOW GRANTS FOR user SHOW INDEX FROM tbl_name [FROM db_name] SHOW INNODB STATUS SHOW OPEN TABLES [FROM db_name] [like_or_where] SHOW PLUGINS SHOW PROCEDURE CODE sp_name SHOW PROCEDURE STATUS [like_or_where] SHOW PRIVILEGES SHOW [FULL] PROCESSLIST SHOW PROFILE [types] [FOR QUERY n] [OFFSET n] [LIMIT n] SHOW PROFILES SHOW SCHEDULER STATUS SHOW [GLOBAL | SESSION] STATUS [like_or_where] SHOW TABLE STATUS [FROM db_name] [like_or_where] SHOW TABLES [FROM db_name] [like_or_where] SHOW TRIGGERS [FROM db_name] [like_or_where] SHOW [GLOBAL | SESSION] VARIABLES [like_or_where] SHOW WARNINGS [LIMIT [offset,] row_count]
select * from score where stuent_id = (select stuent_id from student where name="Jobs" limit 1);
select * from student where age = (select min(age) from student);
select * from tbl where (col1 ,col2 ) = (select col11 ,col22 from tbl2 where ...);
select * from score where student_id in (select student_id from student where sexy = "FEMALE");
select * from student where student.age > all (select age from teacher);
select * from student where exists (select * from absence where absence.student_id = student.student_id);
select s.name from (select * from student where sexy = "Female") as s;
select f1 from t1 union select f2 from t2;
(select f1 from t1) union (select f2 from t2) order by f1;
create view vname [(col,col,...)] as select col1,col2 ... from tbl;
11 涉及多个数据表的删除与更新
单个库上的删除和更新形式如下:delete from tbl where cond.. update tbl set col = ? where ...
delete tbl1 from tbl1 join tbl2 where ...
delete tbl1,tbl2 from tbl1 join tbl2 where ...
update tbl1,tbl2 set tbl.col = ? where ...
start transaction; CURD... commit / rollback;
set autocommit = 0 CRUD... commit/rollback set autocommit = 1
CRUD... saveporint sp_name ... rollback to savepotint sp_name
隔离解别 | 脏读 | 不可重复读 | 幻读 |
Read Uncommitted | Y | Y | Y |
Read Committed | N | Y | Y |
Repeatable Read (default) | N | N | Y |
Serializable | N | N | N |
set [global|session] transaction isolation level lev;
... foreign key [fk_name] (columns) references tbl_name (columns) [on delete action] [on update action]
在设计数据库之前,应该对存放的数据做深入的思考,以避免在存入数据之后再做调整。
SQL语句中除了库名/表名外,字段、条件比较通常都是不区分大小写的。库名、表名随所在的操作系统而不同。在Win上不区分大小写,而在Linux上区分大小写。
随机查询的方法 select * from table order by rand() limit N;
变量可以在sql的查询上下文中起作用 select @var:= age ,name from student where id = 1; select @val;
sql_mode可以设置MySQL所处的模式,可以在启动时--sql-mode="",也可运行时set GLOBAL sql_mode=""