登录 mysql -u root -p 123456;
查找数据库
show databases;
创建数据库
create database name;
create database if not exists 数据库名 default charset 字符集 collate 排序规则;
使用或切换数据库
use name;
删除数据库
drop database name;
查询当前所在数据库
select database;
创建新表
create table table_name(
列名 数据类型 comment '备注',
id int comment '序号',
............
age tinyint comment '年龄'
) comment '学生表';
查询当前数据库中所有表
show tables;
查询当前表结构
desc table_name;
查询表的创建信息
show create table table_name;
删除表
drop table if exists table_name;
或
drop table if exists table_name1,table_name2,...;
if exists是判断是否存在该表,若存在则删除,若不存在也不会报错,可以不写
给表添加字段
alter table table_name add 列名 列类型 comment '备注';
给表删除字段
alter table table_name drop 列名;
修改字段
alter table table_name change 原字段名 新字段名 新字段类型;--可以修改整个字段信息
alter table table_name modify 字段名 新字段类型;-------------------只能修改字段类型
修改表名
alter table 旧表名 rename to 新表名;
修改表的编码格式
alter table table_name convert to character set 新编码规则;
删除表
drop table if exists table_name;
删除指定表并重新创建该表
truncate table table_name;
查询表内容
select * from 表名; 全列查询
select 列名1,列名2,... from 表名; 指定列查询
select 表达式 from 列名; 表达式查询
select distinct 列名1,列名2... from 表名;
插入操作
insert into 表名(列名1,列名2,.....) values(1,'姓名',...);
insert into 表名 values(1,'姓名',...);
insert into 表名 values(1,'姓名1',...),(2,'姓名2',...),...; 批量插入