您的位置:首页 > 数据库 > MySQL

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

2017-03-23 17:30 211 查看
Mysql基本指令

启动mysql

net start mysql


关闭mysql

net stop mysql


登陆mysql

mysql -uroot -p123


查看数据库

show databases;


切换数据库

use test;


查看数据库有哪些表

show tables;


查看表结构或表字段

desc user;


查看表记录

select * from user;


退出mysql终端

exit;


mysql基础篇

一. 数据库操作

创建数据库

creat database y1;


查看数据库

show databases;


切换数据库

use y1;


删除数据库

drop databse y1;


二. 表操作

查看表

show tables;


创建表

create table user1(

id int,

name varchar(30),

pass varchar(30)

);


修改表名

rename table user1 to user2;


删除表

drop table user1;


查看表结构

desc user1;


查看表记录

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语句

DDL//数据定义语言creat,drop,alter

DML//数据操作语言insert,update,delete

DQL//数据查询语言select

DCL//数据控制语言grant,commit,rollback

insert-增

insert into t1(username) values(‘f’);


update-改

update t1 set username=’g’ where id=6;

update t1 set username=’g’,age=20 where id=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);


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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 数据库 select