mysql基础操作

show databases; # 查看库
show tables; # 查看表
use 库名 # 进入库
select database(); # 查看当前所在库
select * from 表名; # 查看表内所有内容
desc 表名; # 查看表结构
show create table|database 表名|库名 ;# 查看创建库或者表的详细信息
create database 数据库名称 default chatacter set 'UTF8';#创建库并制定字符集
create table 表名(字段名1 数据类型 约束,字段名2 数据类型 约束); # 创建数据表
insert into 表名(字段名1,字段名2) values(值1,值2) #插入数据
insert into 表名 values(值1,值2) #插入数据,当前标准只有2个字段
update 表名 set 字段名=值 where 主键或字段名=值
delete from 表名 # 不加where默认删除整张表内容 危险
delete from 表名 where 字段名=值 # 删除字段名称对应的值
drop database 库名 # 删除数据库 跑路必备技能
drop table 表名 # 删除数据表
alter table 表名 add 列名 约束 after 位置 # 添加一列
alter table 表名 drop 列名 # 删除某列
rename table 旧表名 to 新表名 # 修改表名
alter table 表名 modify 字段名 约束 #修改字段约束
alter table 表名 change 字段名 新字段名 约束 #修改字段
列名定义 #数字字母下划线组成,数字不能开头
create table t3(select name from t1 where 1=2); # 复制表结构
create table tt(select name from t1); # 复制表

命令行技巧
\g # 横向显示
\G # 竖向显示
\q # 退出
\c # 取消执行
edit # 使用文本编辑
limit #分页查询
`` # 将mysql关键字作为列名

数据类型
int #整数
bigint # 长整数
float # 小数
char # 字符(删除尾部空格)
varchar # 长字符 (保留尾部空格)
time # 时间 xx:xx:xx
date # 日期 xxxx-xx-xx
datetime # xxxx-xx-xx xx:xx:xx 日期时间

添加索引
create index big_name on t5(name);

创建索引
create table 表名(
字段名1 约束 unique,
字段名2 约束,
key|index 索引名(字段名),
primary key #指定主键
)ENGINE=innodb 字符集=utf-8

select TABLE_SCHEMA, concat(truncate(sum(data_length+index_length)/1024/1024/1024,2),' G') as total_size,concat(truncate(sum(data_length)/1024/1024/1024,2),' G') as data_size,concat(truncate(sum(index_length)/1024/1024/1024,2),' G') as index_size from information_schema.tables group by TABLE_SCHEMA ORDER BY data_size desc; # 查询当前系统所有库的大小

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 数据库基础知识 数据库客户端MySQL作为数据库服务器来运行,任何满足mysql通信规范的软件都可以作为客户端来连...
    littlexjing阅读 927评论 0 2
  • 手动不易,转发请注明出处 --Trance 数据库系统命令: (1).查看存储过程状态:show pro...
    Trance_b54c阅读 1,720评论 0 8
  • 系统用户: 1.sys system(sys权限最高) 2.sysman(操作企业管理器) 密码是安装的时候输入的...
    3hours阅读 1,616评论 0 0
  • 1;连接数据库 查看数据的各种方式 MySQL -hlocalhost -uroot -p123456; show...
    pangzhi阅读 398评论 0 0
  • ORACLE自学教程 --create tabletestone ( id number, --序号usernam...
    落叶寂聊阅读 1,138评论 0 0