前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Mysql学习(基本指令、语句)

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

作者头像
李智
发布2018-08-03 17:25:46
1.1K0
发布2018-08-03 17:25:46
举报
文章被收录于专栏:李智的专栏李智的专栏

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;
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年03月23日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档