MySQL Cookbook学习笔记第四章
1,克隆表(創建一個恰好與某個已有表結構一致的表)
create table … like克隆表結構;使用insert into … select語句克隆部分或者全部表數據
2,將查詢結果保存到表中
a,使用insert into...select將語句查詢結果插入表中,若表不存在需要使用create table …select 語句為查詢結果新建一張表。
insert into dst_tb1(i,s) select val, name from src_tb1
insert into dst_tb1 select * from src_tb1
insert into dst_tb1 select * from src_tb1 where val>100 and name like ‘A%’
create table dst_tb1 select * from src_tb1
重命名某列:
create table dst_tb1 select inv_no, sum(unit_cost*quantity) as total_cost from src_tb1 group by inv_no;
在目的表中使用索引,可以在sql語句中指明:
create table dst_tb1 (primary key(id), index(state,city)) select * from src_tb1
有些列屬性不能克隆島目的表,例如auto_increment,可以在目的表創建并完成數據復制之后,使用alter table
來設置目的表相應地屬性。
create table dst_tb1 (primary key(id)) select * from src_tb1;
alter table dst_tb1 modify id int unsigned not null auto_increment;
3,使用臨時表
所謂臨時表就是僅供臨時使用,使用結束后MySQL自動刪除。
普通的建表語句:
create temporary table tbl_name (列定義);
克隆表:
create temporary table new_table like original_table;
根據查詢結果建表:
create temporaray table tbl_name select …;
臨時表使用原表的表名,可以創建一個普通表的臨時備份,對臨時表做任意更改不影響真實數據
4,檢查或改變某個表的存儲引擎
MySQL支持多種引擎,每一種都有不同的特性,例如InnoDB和BDB支持事務而MyISAM不支持事務
使用information_schema或者show table status或show create table
如獲取mail表的信息:
使用alter以及一個engine子句來更改一張表所用的引擎。
5,生成唯一的表名
a,采用隨機數;b,采用進程號(同一時間pid肯定不同),但不同時間不能保證;c,連接標識符是唯一值的另一個來源
轉載于:https://www.cnblogs.com/burness/p/3712551.html
總結
以上是生活随笔為你收集整理的MySQL Cookbook学习笔记第四章的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 刺客信条起源百万年神殿在哪
- 下一篇: oppo怎么拦截广告电话(这几个品牌是什