mysql常用操作(1)

数据库基本操作

#客户端连接数据库服务端
mysql -h 主机名 -u 用户名 -p 密码

#展示数据库
show databases;

#创建数据库
create database 数据库名;
create database if not exists 数据库名;

#进入某个数据库
use 数据库
mysql -h 主机名 -u 用户名 -p密码 数据库名

#删除数据库
drop database 数据库名;
drop database if exists 数据库名;

表的基本操作

#显示数据库中的表
show tables from 数据库名;
show tables;

#创建表
create table 表名 (
	列名1	 数据类型   [列的属性],
        列名1	 数据类型   [列的属性],
        .....
        列名1	 数据类型   [列的属性]
);
create table if not exists 表名 (
     各个列的信息...
);

#删除表
drop table 表1,表2,......,表n;
drop table if exists 表名;

#查看表结构
describe 表名;
desc 表名;
explain 表名;
show columns from 表名;
show fields from 表名;
show create table 表名\G
show create table 数据库名.表名\G   #直接使用某个数据库中的某个表结构

#修改表名
alter table 旧表名 rename to 新表名;
rename table 旧表名 to 新表名1,旧表名2 to 新表名2,.....,旧表名n to 新表名n,

#增加列
alter table 表名 add column 列名 数据类型 [列的属性]
alter table 表名 add column 列名 数据类型 [列的属性] first  #添加到第一列
alter table 表名 add column 列名 数据类型 [列的属性] after 指定列名  #添加到指定列的后面

#删除列
alter table 表名 drop colum 列名;

#修改列信息
alter table 表名 modify 列名 新数据类型 [新属性];
alter table 表名 change 旧列名 新列名 新数据类型 [新属性];
alter table 表名 add column 列名 列的类型 列的属性 first;
alter table 表名 add column 列名 列的类型 列的属性 after 指定列名;

#一条语句包含多个操作
alter table 表名 操作1,操作2,.....,操作n;
posted @ 2022-03-10 15:11  骑马挎枪打天下  阅读(27)  评论(0编辑  收藏  举报