Mysql学习(基本指令、语句)

这篇博客详细介绍了MySQL的基本指令,包括启动、关闭、登录、查看及操作数据库,以及表的创建、修改和删除。还深入探讨了数据库设计,如数据类型、字段属性、字符集和索引设置。此外,讲解了SQL语句的四大类别,并通过实例展示了数据查询、更新、删除和插入的操作,以及多表查询技巧。
摘要由CSDN通过智能技术生成

Mysql基本指令

  1. 启动mysql
    net start mysql
  2. 关闭mysql
    net stop mysql
  3. 登陆mysql
    mysql -uroot -p123
  4. 查看数据库
    show databases;
  5. 切换数据库
    use test;
  6. 查看数据库有哪些表
    show tables;
  7. 查看表结构或表字段
    desc user;
  8. 查看表记录
    select * from user;
  9. 退出mysql终端
    exit;

mysql基础篇
一. 数据库操作

  1. 创建数据库
    creat database y1;
  2. 查看数据库
    show databases;
  3. 切换数据库
    use y1;
  4. 删除数据库
    drop databse y1;

二. 表操作

  1. 查看表
    show tables;
  2. 创建表
    create table user1(
    id int,
    name varchar(30),
    pass varchar(30)
    );
  3. 修改表名
    rename table user1 to user2;
  4. 删除表
    drop table user1;
  5. 查看表结构
    desc user1;
  6. 查看表记录
    select * from user1;

mysql 数据库设计篇
1. 数据表概念

  1) 数值
  int //int(3)与长度无关,不够3位前面补0,默认看不见  
  float
  
  2) 字符串
  char(n) //占用n个字节,
  varchar(n) //存多少用多少
  text //65535
  longtext //42亿
  
  3) 日期
  date
  datatime
  timestamp
  time
  year
  //建议日期类型存int
  
2. 数据字段类型
3. 数据字段属性
  unsigned//无符号,全正数
  zerofill//零填充,int(3),不够补0
  auto_increment//自增
  null//这一列值允许为null
  not null//这一列不允许为null
  default//默认值
  
4. 数据表的字符集
  \s //查看服务器的基本信息
  查看数据库字符集
  show creat database test;
  查看表字符集
  Show creat table user;
  
  php设置客户端和连接字符集
  $sql=”set names utf8”;
  [mysql]
  defult-character-set=utf8
  //客户端和连接字符集
  [mysqld]
  character-set-server =utf8
  //服务器、数据库和表字符集

  
5. 数据表索引设置

  1) 主键索引
  2) 普通索引
  3) 检测sql语句;
  desc select * from t1 where id=3\G//加\G把表颠倒一下
  //rows 1代表找id=3的人检索一行就找到了

  4) 创建带索引的表;
  creat table t2 (
  id int unsigned auto_increment,
  name varchar(30),
  primary key(id),
  index in_named(name)
  );

  5) 查看表中的所有索引
  show index from t2;
  6) 后期维护普通索引
  删除普通索引
  alter table t2 drop index in_named;
  增加普通索引
  alter table t2 add index in_named(name);
  
6. 后期维护数据表字段
  1) 添加字段
  alter table t1 add age int;
  2) 修改字段
  alter table t1 modify age int not null default 20;
  3) 删除字段
  alter talbe t1 drop age;
  4) 修改字段名
  alter table t1 change name username varchar(30);
  5) 重命名表
  rename table t1 to mess;


SQL语句

  1. DDL//数据定义语言creat,drop,alter
  2. DML//数据操作语言insert,update,delete
  3. DQL//数据查询语言select
  4. DCL//数据控制语言grant,commit,rollback

  5. insert-增
    insert into t1(username) values(‘f’);

  6. update-改
    update t1 set username=’g’ where id=6;
    update t1 set username=’g’,age=20 where id=7;
  7. delete-删
    delete from t1 where id=6;
    delete from t1 where id>=3 and id<=5;
    delete from t1 where id between 3 and 5;
    delete from t1 where id in (1,3,5);
  8. select-查
    1) 选择特定的字段
    select * from t1 where id=3;
    select id from t1 where id=3;

    2) 给字段取别名-as
    select pass as p, id from user where id=3;
    select pass p,id from user where id=3;

    3) 去列中重复值
    select distinct name from user;
    4) 使用where条件进行查询
    select * from t1 where id=3;
    5) 查询空值NULL
    select * from user where pass is null;
    6) between ,in的使用方法
    7) like使用方法(搜索like关键字)
    select * from user where name like ‘%a%’;//包含a
    8) 使用order by对查询结果排序
    select * from user order by name;
    select * from user order by id asc;
    //升序排序
    select * from user order by id desc;
    //降序排序

    9) 使用limit限制
    select * from user order by id desc limit 5;//相当于limit 0,5 前五个
    10) concat函数-字符串连接符
    select concat(id,name) idname from use;
    11) rand函数-随机排序
    select * from user order by rand() limit3;
    12) count统计
    select count(id) tot from user;
    select count(*) tot from user;

    //快速得到数据表多少行
    select count(id) from user where name=’user4’;//user4发帖数
    13) sun求和
    select sum(id) from user where name=’user4’;
    14) avg平均数
    select avg(id) from user;
    15) max最大值
    select max(id) from user;
    16) min最小值
    select min(id),max(id) from user;
    17) group by分组聚合使用
    select name,count(id) from mess group by name;
    select name,count(id) tot from mess group by name order by tot desc;//group byt必须写在order by 之前
    select name,count(id) tot from mess group by name having tot>=5 order by tot desc;
    //group by 必须卸载having之前,having是对分组的结果进行筛选,不能用where;

    18) 多表查询
    普通查询-多表(优先选择)
    //创建表user
    creat table user(
    id int unsigned auto_increment primary key,
    name varchar(30),
    age int);
    //创建表post
    creat table post(
    id int unsigned auto_increment primary key,
    title varchar(200),
    content test);
    //链接表user和post
    alter table post add uid int after id;
    insert into user(name,age) values('user1',21);
    select user.name,post.title,post.content from user,post where user.id=post.uid;
    select user.name,count(user.id) from user,post where user.id=post.uid group by post.uid;

    嵌套查询-多表
    左链接查询-多表
    19) 往title字段前加uid字段
    alter table post add uid after id;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值