MySQL入门和常用命令行

本文创建大致的学习清单,详情的话移到各大学习网站。

图形化工具
  • Navicat (破解版)
  • SQLyog
  • PhpMyadmin
  • Workbench
  • DBeaver
连接数据库
mysql -uroot -p

在这里插入图片描述

查看数据库
show datebases;

在这里插入图片描述
ps: 命令行 结尾; 不可以少。

创建某一数据库
create database test charset utf8;
修改某一数据库
alter database  [数据库名] { character set  <字符集名> |  collate <校对规则名> }

在这里插入图片描述

删除某一数据库
drop database if exists test;
外部SQL文件导入
source xxx  // 文件位置
使用某一数据库
use test

在这里插入图片描述

常见数据类型

在 MySQL 中常见的数据类型如下:

  1. 整数类型
    包括 TINYINT、SMALLINT、MEDIUMINT、INT、BIGINT,浮点数类型 FLOAT 和 DOUBLE,定点数类型 DECIMAL。
  2. 日期/时间类型
    包括 YEAR、TIME、DATE、DATETIME 和 TIMESTAMP。
    ps:
    timestamp:1970-2038
    year:1901-2155
    datetime: 9999 vb
  3. 字符串类型
    包括 CHAR、VARCHAR、BINARY、VARBINARY、BLOB、TEXT、ENUM 和 SET 等。
  4. 二进制类型
    包括 BIT、BINARY、VARBINARY、TINYBLOB、BLOB、MEDIUMBLOB 和 LONGBLOB。
创建表
mysql>use test
mysql> CREATE TABLE test1(
    -> id INT NOT NULL AUTO_INCREMENT,
    -> title VARCHAR(100) NOT NULL,
    -> author VARCHAR(40) NOT NULL,
    -> PRIMARY KEY ( id )
    -> );

删除表
DROP TABLE if EXISTS test1;
插入测试数据
mysql> INSERT INTO test1
    -> (title,author)
    -> VALUES
    -> ("莎士比亚全集","莎士比亚");
Query OK, 1 row affected (0.01 sec)
查询表内容
select * from test1; // 查看所有
select author from test1; // 查看表某一字段
where 条件

在这里插入图片描述

select * from test1 where author='莎士比亚';
like 复制表
create Table test2 like test1; // 只是把表结构复制一份

insert into test2  select * from test1;

// 另一种方法
create table test3 select * from test; // 复制的某一主键之说      

可以用到的别名 比如说数据是一样的,只是字段名字不一样 as

NULL值
select null = null s; // false 所以不能等于号判断
select * from test1 where is not null;
select ifnull(anthor,"匿名") from test1; // null值做处理
排序
select * from test1 order by id asc [,...]; // asc 正排 从小到大

select * from test1 order by id desc limit 1; // dec 反排 limit 1  取数量 1 从第一个开始 的部分数据
更新数据
update test1 set author="莎士比亚3" where author="莎士比亚2";  // 加条件 不然全局修改
删除数据
delete from test1 where author="莎士比亚3";  // 加条件 不然删表
数据表维护
  • 更表名
alter table  test1 rename test3;
rename table test3 to test1;
  • 更改表某个属性
alter table test1 change author author varchar(100) defalt null;
  • 复制一份备用表
create table test4 select * from test1;
  • 更改字符集
alter table test4 charset gbk; // 现在主流utf8 知道这么一回事就行了。
  • 删除
delete from test4; // 删数据 性能较差
truncate test4; // 删数据 建议
drop table if exists test2; 删表

  • 修改主键

在这里插入图片描述

// 删主键钱需要把自增量取消
alter table test1 modify id init not null; // 先把自增量取消掉
alter table test1 drop primary key; // 删主键

// 添加主键 自增量
alter table test1 add primary key (id);
alter table test1 modify id init not null primary key;
  • group 中不存在字段,报错
set sql_mode=(select replace(@@sql_mode,'ONLY_FULL_GROUP_BY',''))
正则表达式
select * from test1 where author regexp "^st" // 以'st'为开头的所有数据
select * from test1 where author regexp "ok$" // 以'ok'为结尾的所有数据
select * from test1 where author regexp "ok"  // 包含'ok'的所有数据
校验规则

大小写 字符串比较.查找 排序的算法(大小写改变,如果区分,就会导致编码不一样,就会重新排列)

utf8_general_ci // 不区别大小写
utf8-bin // 区分大小写
时间格式化

具体详情字段可参照各大网站 sql date_format()方法

select name, date_format(时间字段,"%Y年%m月%d日 %h:i%:%s") from test;

alter table test add updated_at timestamp default current_timestamp; /添加一个时间戳类型

更新数据,数据不变,时间戳不变,数据便,就变。
SQL常用函数
  • count
select count(*) from test1; // *处可以自己属性(非空 null)
  • max/min
select max(字段) from test1;
  • avg
select avg (字段) from test1;
  • sum
select sum(字段) from test1;
  • distinct
select distinct 字段 from test1 where xxx;
  • group by
select xx from test1 where xx group by xx [, xx];
  • having
select xx , count(*) from test1 group by xx having count(*)>=2;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值