当前位置:Gxlcms > 数据库问题 > mysql常用命令

mysql常用命令

时间:2021-07-01 10:21:17 帮助过:3人阅读

创建表

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常用命令

标签:

人气教程排行