Mysql的基础命令

一,数据库:

查询数据库:show database;

查询所在库:select database;

指定查询库:show database 数据库名称;

创建数据库:create database 数据库名称;

删除数据库:drop database 数据库名称; (永久性删除)

选择数据库:use/USE 数据库名;(不区分大小写)

二,数据表:

查询所有表:show tables;

查询指定表:show table 表名;

创建数据表:create table 表名(字段 类型(主键自增:auto_increment primary key,字段 varchar(20) not null))charset=utf8;

删除数据表:drop table 表名;

修改原表名:alter table 原表名 rename 新表名;

三,表字段:

添加表字段:alter table 表名 add 要添加的字段 带类型;

删除表字段:alter table 表名 drop 要删除的字段名;

修改表字段:alter table 表名 change 原字段名 新字段名 类型 not null;

查询表字段:desc 表名;

开头填字段:alter table 表名 要添加的字段带类型 frist;(一般为id主键类型很少用到)

指定填字段:alter table 表名 add 要添加的字段名加类型 after 要在哪个字段后面添加就写哪个字段名

四,表信息:

查询表信息:select * from 表名;

添加表信息:insert into 表名(字段1,字段2)values(值1,值2);

直接填信息:insert into 表名 values(值1,值2);

修改表信息:update 表名 set 字段名=‘要修改的信息’ where 条件 以id为目标=信息-1;

删除表信息:delete 表名 where 条件 id = 1;

清空表信息:delete 表名;(清空表内所有信息保留字段)

删除数据表:drop 表名;(直接删除数据表)

五,数据库编码:

查看库编码:show variables like ‘character_set_database’;

查看表编码:show create table 表名;

建库设编码:create table 表名(字段1,字段2)charset=utf8;

修改库编码:alter database 数据库名称 character set utf8;

修改表编码:alter table 表名 character set utf8;

修改列编码:alter table 表名 change 字段名 字段名 类型 character set utf8 not null;

六,表主键:

删除自增id:alter table 表名 drop id;

添加自增id:alter table 表名 add id int auto_increment primary key first;

主键自增id:alter table 表名 add change id int not null auto_increment primary key;

主键自增id:alter table 表名 add column id int auto_increment not null primary key (id);

清空数据 并将字段恢复至1重新开始计数:truncate table 表名;

删除表约束:alter table 表名 drop primary key;

有自增的情况下,需要先删除自增,之后才能删除主键约束;

七,表外键:

添加表外键:alter table 表名 constraint 外键名 foreign key 字段名 references 外表表名 字段名;

删除表外键:alter table 表名 drop foreign key 外键名;

八,总结:

查询:show database/table;(show 可以查询数据库 和 表)

创建 :create database/table;(create 可以创建数据库 和 表)

查表:select 查询表结构信息 desc 查询表结构

修改:alter table (条件)

删除:drop 与 delete 永久删除与 清空信息, 慎用;

九,内容不是很全,没有 确定 约束 , 表关联; 有不对的请指正;

猜你喜欢

转载自blog.csdn.net/sunny2429/article/details/83450328