MySQL基本命令

=====================================

MySQL数据库

一、概念:
数据: data
数据库: DB
数据库管理系统:DBMS
数据库系统:DBS
MySQL:数据库
mysql:客户端命令(用来连接服务或发送sql指令)
SQL:结构化查询语言 ,其中MySQL支持这个。
SQL语言分为4个部分:DDL、DML、DQL、DCL

======================================

二、连接数据库:
mysql -h 主机名 -u 用户名 -p密码 库名

C:>mysql –采用匿名账号和密码登陆本机服务
C:>mysql -h localhost -u root -proot –采用root账号和root密码登陆本机服务
C:>mysql -u root -p –推荐方式默认登陆本机
Enter password: **

C:>mysql -u root -p lamp61 –直接进入lamp61数据库的方式登陆

=======================================

三、授权,删除权限:
格式:grant 允许操作 on 库名.表名 to ‘账号’@’来源’ identified by ‘密码’;

--实例:创建zhangsan账号,密码123,授权lamp61库下所有表的增/删/改/查数据,来源地不限
mysql> grant select,insert,update,delete on lamp61.* to 'zhangsan'@'%' identified by '123';
Query OK, 0 rows affected (0.00 sec)

******************************************
*#新加用户服务xcj,新加的用户不能马上生效
*mysql> grant all on *.* to 'xcj'@'%' identified by "xcj_passwd";
*Query OK, 0 rows affected (0.04 sec)

*#生效新加用户xcj权限
*mysql> flush privileges;
*Query OK, 0 rows affected (0.03 sec)
*****************************************
-- 查看授权语句
mysql> show grants for zhangsan@'%';

--移除zhangsan用户对lamp61库的修改和添加权限
mysql> revoke update,insert on lamp61.* from zhangsan@'%';
    Query OK, 0 rows affected (0.00 sec)

--删除了整个用户及其权限(包括数据字典中的数据)
mysql> drop user zhangsan@'%';
    Query OK, 0 rows affected (0.00 sec)

============================================

–四、SQL的基本操作

mysql>\h – 快捷帮助
mysql>\c – 取消命令输入
mysql>\s – 查看当前数据库的状态
mysql>\q – 退出mysql命令行

–显示所有的数据库
mysql> show databases;

–查看当前所在的数据库
mysql> select database();

–创建数据库
mysql> create database test;
mysql> create database if not exists test;

–删除数据库
mysql> drop database test;
mysql> drop database if exists test;

–使用数据库
mysql> use test;

–查看建表的语句
mysql> show create table stu\G

–创建表
mysql> create table stu(
-> id int not null auto_increment primary key,
-> name varchar(16) not null,
-> sex enum(‘m’,’w’) not null default ‘m’,
-> age int not null);

–显示当前数据库下所有的表
mysql> show tables;

–查看表结构
mysql> desc stu;

–表中插入数据
mysql> insert into stu(name,sex,age) values(‘zhangsan’,’m’,22),
-> (‘lisi’,’w’,’20’),
-> (‘wangwu’,’m’,’30’),
-> (‘zhaoliu’,’m’,’20’);

–显示表中的数据
mysql> select * from stu;

–删除一条数据
mysql> delete from stu where id=1;

–更新一条数据
mysql> update stu set age=25 where id=2;

–添加一个字段
mysql> alter table stu add classid int not null;–注意一定要加类型

–修改字段属性
mysql> alter table stu modify sex varchar(32) default ‘w’;–注意类型不能少
mysql> alter table stu change sex sex varchar(32) default ‘w’;??????????

–在某字段之后添加一个字段
mysql> alter table stu add idnum varchar(18) not null 【after age】;

–删除一个字段
mysql> alter table stu drop idnum;

–添加索引
mysql> alter table stu add index(tel);

–删除索引
mysql> alter table stu drop index tel;–注意此处不加()

–查看建表帮助
mysql> show create table s tu\G;

–修改表名
mysql> alter table stu rename as stu_msg;

– 重设表的自增id值(建议先删除数据)
mysql> alter table users rement=1;

–查看当前时间
mysql> select now();

====================================================

五、 MySQL数据库的数据类型:

MySQL的数据类型分为四大类:数值类型、字串类型、日期类型、NULL。

5.1 数值类型:
    *tinyint(1字节)
    smallint(2字节)
    mediumint(3字节)
    *int(4字节)
    bigint(8字节)
    *float(4字节)   float(6,2)
    *double(8字节)   (可以数据放大几倍,可以把小数点往后移动)
    decimal(自定义)字串形数值 用于价格,涉及钱的的字段(一般精确到8位)

 5.2 字串类型
    普通字串
    *char  定长字串 字符长度:
    *varchar 可变字串 规则:字节长度65535   字符长度与字符集长度
            1、最长长度远远大于平均长度
            2、数据修改少的情况下
            2、varchar处理复杂的数据比较有效。



    二进制类型
    tinyblob
    blob
    mediumblob
    longblob

    文本类型
    tinytext
    *text
    mediumtext
    longtext

    *enum枚举 
    set集合

5.3 时间和日期类型:
    date  年月日
    time  时分秒
    datatime 年月日时分秒
    timestamp 时间戳
    year 年

5.4 NULL值
    NULL意味着“没有值”或“未知值”
    可以测试某个值是否为NULL
    不能对NULL值进行算术计算
    对NULL值进行算术运算,其结果还是NULL
    0或NULL都意味着假,其余值都意味着真


零填充的作用:zero_fill  计算机底层处理时有零填充处理数据会很快。

=====================================================

六、 表的字段约束:
unsigned 无符号(正数)
zerofill 前导零填充
auto_increment 自增
default 默认值
not null 非空
PRIMARY KEY 主键
unique 唯一性
index 常规索引

==================================================

===================================================

MySQL第三天:

一、数据的增、删、改:

--指定字段来添加
mysql> insert into stu(id,name,age,sex,classid) values(null,'ee',22,"m",'lamp64');

mysql> insert into stu values(null,'ww',22,"m",'lamp64');  --不指定字段名添加数据

mysql> insert into stu(age,name) values(21,'kkk'); --指定部分字段来添加

mysql> insert into stu(age,name) values(22,'aaa'),(23,'bbbb'),(24,"eee"); --批量添加
  1. 修改
    格式:update 表名 set 字段名=修改值[,字段名=修改值…] [where 条件][order by ..][limit..]

    – 将stu表中id等于11这条数据的age改为20,sex改为w、classid改为lamp63。
    mysql> update stu set age=20,sex=’w’,classid=’lamp63’ where id=11;

  2. 删除:
    格式: delete from 表名 [where 条件][order by ..][limit..]

    – 删除stu表中id等于13的数据
    mysql> delete from stu where id=13;

  3. 查询:
    格式: select 字段信息 from 表名
    [where 条件][group by 分组[having 子条件]][order by 排序][limit 部分数据]

    mysql> select * from stu; – 查看stu表中所有字段的所有信息

    mysql> select id,name,age from stu; –只查看部分字段信息

    mysql> select id,name,sex,”beijing” from stu; –追加一列输出信息,值:beijing

    mysql> select id,name as username from stu; –为name字段名起个别名(通过as)

    mysql> select id,name username,sex from stu; –同上,as关键字可以省略

    mysql> select id,name username,sex,”beijing” city from stu;

    mysql> select id,name,age,age+4 age2,classid from stu; –同时获取4年后的年龄(别名age2)

    mysql> select id,concat(classid,”:”,name) stuname,age,sex from stu;
    –查询stu表数据,整合classid和name字段为一个值,并且起个别名为stuname

    mysql> select distinct classid from stu; –查看stu表中classid字段的值,使用distinct去除重复值

    4.2 –where条件
    mysql> select * from stu where sex=’m’; –查看sex值为m的所有信息

    mysql> select * from stu where sex=’m’ && classid=’lamp61’; –查看sex值为m的并且classid为lamp61的所有信息
    mysql> select * from stu where sex=’m’ and classid=’lamp61’; –推荐使用and与

    mysql> select * from stu where classid=’lamp61’ or classid=’lamp64’; –查看61期和64期信息

    mysql> select * from stu where classid !=’lamp61’; –不是lamp61期的学生信息

    mysql> select * from stu;
    +—-+———-+——+—–+———+
    | id | name | age | sex | classid |
    +—-+———-+——+—–+———+
    | 1 | zhangsan | 20 | m | lamp61 |
    | 2 | lisi | 22 | w | lamp61 |
    | 3 | wangwu | 21 | m | lamp61 |
    | 4 | qq | 24 | w | lamp62 |
    | 5 | aa | 20 | m | lamp62 |
    | 6 | bb | 25 | m | lamp63 |
    | 7 | dd | 23 | w | lamp62 |
    | 8 | ee | 22 | m | lamp64 |
    | 9 | ww | 22 | m | lamp64 |
    | 10 | kkk | 21 | m | NULL |
    | 11 | aaa | 20 | w | lamp63 |
    | 12 | bbbb | 23 | m | NULL |
    +—-+———-+——+—–+———+
    12 rows in set (0.00 sec)

    –查看年龄age在22岁以上的(含22)
    mysql> select * from stu where age>=22;

    –查看年龄age在22岁以上(含22)的所有男生(sex值为m)
    mysql> select * from stu where age>=22 and sex=’m’;

    –查看年龄在22到24岁之间的所有学生信息
    mysql> select * from stu where age>=22 and age<=24;
    mysql> select * from stu where age between 22 and 24;

    –查看年龄在22岁以下或24以上的学生信息
    mysql> select * from stu where age<22 or age>24;
    mysql> select * from stu where age not between 22 and 24;

    –查看classid值为null的学生信息
    mysql> select * from stu where classid is null;
    mysql> select * from stu where classid <=> null;

    –查看id号为1,5,6,10的学生信息
    mysql> select * from stu where id=1 or id=5 or id=6 or id=10;
    mysql> select * from stu where id in (1,5,6,10);

    –like是模糊查询,支持两个特殊字符:”_”:任意一个字符 “%”:任意数量的任意字符
    –查询姓名是由两个字母构成的信息。
    mysql> select * from stu where name like ‘__’;
    –查看姓名里含有a字母的。
    mysql> select * from stu where name like ‘%a%’;
    –查看姓名是由a字母开头的。
    mysql> select * from stu where name like ‘a%’;

    4.3 – order by 排序 格式: ….. order by 被排序的字段名 [asc(升序,默认)|desc(降序)][,其他字段排序]
    –查看学生信息(按年龄从小到大排序)
    mysql> select * from stu order by age; –默认升序asc
    mysql> select * from stu order by age asc;

    –查看学生信息(按年龄从大到小排序)
    mysql> select * from stu order by age desc;

    –查看学生信息(按年龄从大到小排序),年龄相同的,按id从大到小排
    mysql> select * from stu order by age desc,id desc;

    –查看lamp61期学生信息,按年龄从大到小排序
    mysql> select * from stu where classid=’lamp61’ order by age desc;

    4.4 –limit 子句 用于截取部分数据,(分页技术)
    – 格式: …limit n 或 …limit m,n (其中n表示取多少条数据,m表示起始位置)

    – 提取前3条数据
    mysql> select * from stu limit 3;

    – 获取年龄最大的4位
    mysql> select * from stu order by age desc limit 4;

    – 获取61期年龄最大的一位
    mysql> select * from stu where classid=”lamp61” order by age desc limit 1;

    mysql> select * from stu limit 0,4; –以4条/页,获取第一页
    mysql> select * from stu limit 4,4; –以4条/页,获取第二页
    mysql> select * from stu limit 8,4; –以4条/页,获取第三页
    mysql> select * from stu limit 12,4; –以4条/页,获取第四页

    –分页公式:….. limit (当前页-1)*页大小,页大小;

    4.5 –用于统计的统计函数(聚合函数):
    – count()统计条数, sum()求总和、 avg()平均、 max()最大、 min()最小
    –统计stu的总数据条数
    mysql> select count(id) from stu;
    mysql> select count(*) from stu;

    –统计61期学生条数、年龄总和、平均年龄、最大年龄、最小年龄
    mysql> select count(*),sum(age),avg(age),max(age),min(age) from stu where classid=’lamp61’;
    +———-+———-+———-+———-+———-+
    | count(*) | sum(age) | avg(age) | max(age) | min(age) |
    +———-+———-+———-+———-+———-+
    | 3 | 63 | 21.0000 | 22 | 20 |
    +———-+———-+———-+———-+———-+
    1 row in set (0.02 sec)

    4.6 –分组处理 group by
    –统计每个班都有多少人
    mysql> select classid,count(*) from stu group by classid;

    –统计每个班都有多少男生
    mysql> select classid,count(*) from stu where sex=’m’ group by classid;

    –统计每个班(去除null值)都有多少男生,要求按降序排序
    mysql> select classid,count(*) from stu where sex=’m’ and classid is not null
    group by classid order by count(*) desc;

    mysql> select classid,count(*) num from stu where sex=’m’ and classid is not null
    group by classid order by num desc;

    –统计每个班的平均年龄,从大到小排序
    mysql> select classid,avg(age) aa from stu group by classid order by aa desc;

    –统计每个班的平均年龄,从大到小排序,要求只保留22岁以上的。
    mysql> select classid,count(*) num from stu where sex=’m’ and classid is not null
    group by classid order by num desc;

二、 数据库的导出
1. 导出:

    --导出lamp61数据库到lamp61_20130328.sql文件中
    D:\>mysqldump -u root -p lamp61>lamp61_20130328.sql
    Enter password: ****

    --导出lamp61数据库中stu表到lamp61_stu_20130328.sql文件中
    D:\>mysqldump -u root -p lamp61 stu>lamp61_stu_20130328.sql
    Enter password: ****

 2. 恢复:
    --恢复stu表
    D:\>mysql -u root -p lamp61<lamp61_stu_20130328.sql
    Enter password: ****

    --恢复整个库
    D:\>mysql -u root -p lamp61<lamp61_20130328.sql
    Enter password: ****
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值