mysql常用命令

來源:互聯網
上載者:User

標籤:

建立表

create table IF NOT EXISTS tbkt.msg_market (

id int(11) unsigned not null auto_increment,

type int(1) not null unique,

user_id int(11) not null,

phone_number varchar(11) not null,

content varchar(1000) not null default "",

add_time datetime not null,

primary key(id)

) engine=innodb default charset=utf8 auto_increment=1;

 

添加欄位

mysql> alter table knowledge_question add COLUMN multiple int(1) not NULL DEFAULT 0;

Query OK, 0 rows affected (1.14 sec)

Records: 0 Duplicates: 0 Warnings: 0

 

修改欄位

mysql> alter table knowledge_question modify column json_answer text not NULL;

Query OK, 2973 rows affected (1.42 sec)

Records: 2973 Duplicates: 0 Warnings: 0

mysql> alter table knowledge_question change area display int(1) not null default 0;

Query OK, 0 rows affected (0.03 sec)

Records: 0 Duplicates: 0 Warnings: 0

#modify適用於只改變欄位類型,change可以改變類型也可以改變列名稱,但使用change如果在不改變名稱的情況下也需要輸入新的欄位名稱。

 

刪除欄位

mysql> alter table knowledge_question drop column answer;

Query OK, 0 rows affected (0.62 sec)

Records: 0 Duplicates: 0 Warnings: 0

 

添加索引(普通索引)

mysql>ALTER TABLE `table_name` ADD INDEX index_name ( `column` );  # 索引名字定義表名加欄位名加“_index”標記 

 

刪除索引

mysql> drop index task_new_study_detail_1f92e550 on task_new_study_detail;

[SQL] drop index task_new_study_detail_1f92e550 on task_new_study_detail;

受影響的行: 0

時間: 0.098s

 

查看錶結構

desc 表名;

show columns from 表名;

show create table 表名;

 

刪除表

DROP TABLE IF EXISTS `table_name`;

 

刪除資料庫

drop database database_name;

 

更新表資料

update mmsc_word set send_status=2 where send_status=0;

update zy_book set `cover`=replace(`cover`, ‘ ‘, ‘‘); -- 更新空格為空白字串

select * from zy_book t where replace(t.cover, ‘ ‘, ‘a‘)="a" -- 查詢為空白格的資料

 

同時操作多個表

update zy_video t, zy_question_ask t2 set t2.video_id=t.id where t.object_id=t2.id and t.type=3;

 

根據條件刪除表資料

delete from mmsc_word where send_status=2;

串連查詢後刪除資料

delete ap from account_profile ap join auth_user au on ap.user_id=au.id where !(INSTR(au.username,"xs")>0 or INSTR(au.username,"js")>0 or INSTR(au.username,"jz")>0) and au.username!="dt001";

 

插入資料

INSERT INTO 目標表 SELECT * FROM 來源表; -- 插入全部欄位

INSERT INTO newArticles SELECT * FROM articles; -- 插入全部欄位

INSERT INTO 目標表 (欄位1, 欄位2, ...) SELECT 欄位1, 欄位2, ... FROM 來源表; -- 插入指定欄位

create table yy_role_image select * from tbkt.english_role_image; -- 建立表並插入資料

 

清空表資料

truncate table 表名;

mysql常用命令

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.