[root@host50 ~]# mysql -hlocalhost -uroot -p'*-ZPqB——第一次登陆是要默认密码登陆
mysql> show databases; ————查看库,提示需要修改默认密码
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user root@"localhost"identified by "123456"————修改默认密码,提示不符合密码规则,要么符合密码规则,或者修改密码规则。密码太负责不利于记忆 不进行修改密码就不能进行创建库等操作 -> ;
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set global validate_password_policy=0;——修改密码规则
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=6;——修改密码规则
Query OK, 0 rows affected (0.00 sec)
mysql> alter user root@"localhost" identified by "123456";——修改密码
Query OK, 0 rows affected (0.00 sec)
mysql> qiut
[root@host50 ~]# mysql -hlocalhost -uroot -p——再一次登陆
Enter password:
mysql> show databases;——展示库名单
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
mysql> create database gamedb; ——创建库
Query OK, 1 row affected (0.00 sec)
mysql> create database buydb;——创建库
Query OK, 1 row affected (0.00 sec)
mysql> create database bbsdb;——创建库
Query OK, 1 row affected (0.00 sec)
mysql> show databases;——展示库
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbsdb |
| buydb |
| gamedb |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.00 sec)
命令行创建库和sql命令创建库的不同之处
mkdir /var/log/mysql/db1——命令行进行创建库
————不管那一种方法都是可以创建一个文件夹,但是创建的目录的所属者和所属组就不一样。一个是root一个是MySQL
这样建立时没有w权限
[root@host50 ~]# mkdir /var/lib/mysql/db2
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| buydb |
| db1 |
| db2 |
| gamedb |
| mysql |
| performance_schema |
| sys |
+--------------------+
[root@host50 ~]# ls -ld /var/lib/mysql/db1
drwxr-x---. 2 mysql mysql 20 7月 10 14:12 /var/lib/mysql/db1
[root@host50 ~]# ls -ld /var/lib/mysql/db2
drwxr-xr-x. 2 root root 6 7月 10 14:27 /var/lib/mysql/db2——其他用户没有w权限,在SQL没有写入的权限
修改方法1.给其他用户添加w权限。
SQL 命令 ————结构化查询语言(中文名称)
库的管理命令??? 查看 删除 撤换 创建
数据库的命名规则
可以用数字 字母 下划线 不能有纯数字;
区分大小写,,具有唯一性;
以;结束命令 \c终止命令
表的管理命令 创建表 查看表内容 查看表结构 删除表 写入数据
表必须创在库里面 表相当于系统文件
数据的存储取决于对象
创建表 创建表结构
mysql> create database studb;——创建库
mysql> use studb;——进入库 ,表必须创建在库里面
mysql> create table studb.stuinfo( name char(15), sex char(10), age int, tel char(11) );————创建表
mysql> desc studb.stuinfo;——进入表
mysql> select * from studb.stuinfo;——查看表记录
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| sex | char(10) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| tel | char(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
mysql> select * from studb.stuinfo;——还没有写入数据,显示为空
Empty set (0.00 sec)
mysql> insert into studb.stuinfo values("bob","boy",21,"88888") , ——数字可以不用双引 ("tangmingqi","boy",27,"13527898646");
Query OK, 2 rows affected (0.05 sec)————给表写入数据
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from studb.stuinfo;————查看表里的记录
+------------+------+------+-------------+
| name | sex | age | tel |
+------------+------+------+-------------+
| bob | boy | 21 | 88888 |
| tangmingqi | boy | 27 | 13527898646 |
+------------+------+------+-------------+
2 rows in set (0.00 sec)
mysql> update studb.stuinfo set sex="girl";——更改表里的数据
Query OK, 2 rows affected (0.04 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from studb.stuinfo; ——查看表里的数据
+------------+------+------+-------------+
| name | sex | age | tel |
+------------+------+------+-------------+
| bob | girl | 21 | 88888 |
| tangmingqi | girl | 27 | 13527898646 |
+------------+------+------+-------------+
2 rows in set (0.00 sec)
mysql> select database(); ———查看当前的信息
+------------+
| database() |
+------------+
| studb |
+------------+
1 row in set (0.00 sec)
mysql> delete from studb.stuinfo; ——删除表里的数据
Query OK, 2 rows affected (0.02 sec)
mysql> select * from studb.stuinfo; ————查看表里数据
Empty set (0.00 sec)
mysql> show tables; ————查看表
+-----------------+
| Tables_in_studb |
+-----------------+
| stuinfo |
+-----------------+
1 row in set (0.00 sec)
mysql> drop table stuinfo; ————删除表
Query OK, 0 rows affected (0.08 sec)
mysql> show tables; ————查看表
Empty set (0.00 sec)
字符集??
mysql> use studb; ——进入库
mysql> create table 学生表(姓名 char(10),年龄 int)default charset=utf8; —创建中文的数据表
Query OK, 0 rows affected (0.20 sec)
mysql> show create table 学生表; ————展示表结构
| 学生表 | CREATE TABLE `学生表` (
`姓名` char(10) DEFAULT NULL,
`年龄` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-----------+---------------------------------
insert into 学生表 values("张三丰",108); ————中文插入数据
Query OK, 1 row affected (0.02 sec)
mysql> select * from 学生表; ————展示表数据
+-----------+--------+
| 姓名 | 年龄 |
+-----------+--------+
| 张三丰 | 108 |
+-----------+--------+
1 row in set (0.00 sec)
Mysql 数据类型:
建表的时候设计一个合理的数据类型 ———表结构
整数型——又可以分好几类
用得最多的是INT
小数型(浮点型)
DOUBLE 双精度浮点数 8个字节
常见的信息种类
mysql> use db1
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| db1 |
+------------+
1 row in set (0.00 sec)
mysql> create table t1(level tinyint); 数值 -128 ~127 之间的数值 建有符合的数据存储表
Query OK, 0 rows affected (0.25 sec)
mysql> desc t1;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| level | tinyint(4) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> select * from t1;————表里是否有记录
Empty set (0.00 sec)
mysql> create table t2(level tinyint unsigned);
mysql> desc t2
-> ;
+-------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+-------+
| level | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> select * from t2;
Empty set (0.00 sec)
mysql> insert into t2 values(-1); ————超出存储范围
ERROR 1264 (22003): Out of range value for column 'level' at row 1
mysql> insert into t2 values(0); ————存储范围0~255
Query OK, 1 row affected (0.03 sec)
mysql> insert into t2 values(255); ————存储范围0~255
Query OK, 1 row affected (0.03 sec)
mysql> insert into t2 values(256); ————超出存储范围
ERROR 1264 (22003): Out of range value for column 'level' at row 1
mysql> select * from t2; ————表的存储数据
+-------+
| level |
+-------+
| 0 |
| 255 |
+-------+
2 rows in set (0.00 sec)
mysql> insert into t2 values(1.256);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t2;
+-------+
| level |
+-------+
| 0 |
| 255 |
| 1 |
+-------+
3 rows in set (0.00 sec)
mysql> create table t3(pay float(5,2));
Query OK, 0 rows affected (0.27 sec)
mysql> desc t3;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| pay | float(5,2) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
1 row in set (0.01 sec)
建立一个浮点型的数据表
mysql> insert into t3 values(1118.26);
ERROR 1264 (22003): Out of range value for column 'pay' at row 1
mysql> insert into t3 values(118.26);
Query OK, 1 row affected (0.03 sec)
mysql> insert into t3 values(280);
Query OK, 1 row affected (0.03 sec)
mysql> insert into t3 values(0.26002225555);
Query OK, 1 row affected (0.03 sec)
mysql> insert into t3 values(1111.26002225555);
ERROR 1264 (22003): Out of range value for column 'pay' at row 1
一共是位数,小数位只有两位,不管数多少位小数, 都精确到两位。。但是整体不能超过五个数
mysql> select * from t3;
+--------+
| pay |
+--------+
| 118.26 |
| 280.00 |
+--------+
2 rows in set (0.00 sec)
字符类型
char ————定长255个字符 存储不够指定的长度时,就会在右边自动补齐 超出宽带就不能存储成功,生产环境用得比较多
varchar ————变长 66532个字符,有大量数据写入的时候,CPU 就会暂用 ,没有时间处理其他访问
text
blob
mysql 数据库能存那些文件?? char varchar text blob ——用空间换时间
数值 字符 音频文件 图片 视频文件
/root/media/film/xxx.avi
mysql> create table t7 (gamemame char(15),level smallint,pay float(7.2));
Query OK, 0 rows affected (0.21 sec)
mysql> select * from t7;
Empty set (0.00 sec)
mysql> desc t7;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| gamemame | char(15) | YES | | NULL | |
| level | smallint(6) | YES | | NULL | |
| pay | float | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into t7 values("zbj",250,1880.22);
Query OK, 1 row affected (0.02 sec)
mysql> select * from t7;
+----------+-------+---------+
| gamemame | level | pay |
+----------+-------+---------+
| zbj | 250 | 1880.22 |
+----------+-------+---------+
1 row in set (0.00 sec)
枚举类型 :让字段的值,在规定的范围内选择
enum (值列表) 单选
set (值列表) 多选
mysql> create table t8 (name char(15),sex enum("boy","girl","no"),likes set ("game","film","eat","sleep"));
Query OK, 0 rows affected (0.25 sec)
mysql> desc t8;
+-------+----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------------------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| sex | enum('boy','girl','no') | YES | | NULL | |
| likes | set('game','film','eat','sleep') | YES | | NULL | |
+-------+----------------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into t8 values("bob","man","it,book");
ERROR 1265 (01000): Data truncated for column 'sex' at row 1
mysql> insert into t8 values("bob","boy","eat,sleep");
Query OK, 1 row affected (0.03 sec)
mysql> select * from t8;
+------+------+-----------+
| name | sex | likes |
+------+------+-----------+
| bob | boy | eat,sleep |
+------+------+-----------+
1 row in set (0.00 sec)
日期时间类型
年 year yyyy 2018
日期 date yyyymmdd 20181138 都有自己的存储空间1901~2055年
时间 time HHMMSS 083000
日期时间 datetime YYYYMMDDHHMMSS
timestamp 20180807123723
mysql> create table t2(name char(15), brithday date, work time, s_year year, meetting datetime);
——为一个表赋值
Query OK, 0 rows affected (0.19 sec)
mysql> desc t2;
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| name | char(15) | YES | | NULL | |
| brithday | date | YES | | NULL | |
| work | time | YES | | NULL | |
| s_year | year(4) | YES | | NULL | |
| meetting | datetime | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> insert into t2 values("liuhai",19891223,180000,2013,20181009203000);——值入数据
Query OK, 1 row affected (0.02 sec)
mysql> select * from t2; ——查看表记录
+--------+------------+----------+--------+---------------------+
| name | brithday | work | s_year | meetting |
+--------+------------+----------+--------+---------------------+
| liuhai | 1989-12-23 | 18:00:00 | 2013 | 2018-10-09 20:30:00 |
+--------+------------+----------+--------+---------------------+
1 row in set (0.00 sec)
mysql> insert into t2 values("zuhai",19761232,180000,2013,20181009203000);——语法格式错误
ERROR 1292 (22007): Incorrect date value: '19761232' for column 'brithday' at row 1
mysql> insert into t2 values("liuhai2",19881223,070000,95,20131009210000),("zuhai1",19781223,200000,23,20180807123000);——可以以,分割继续插入信息
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t2;——查看表信息
+---------+------------+----------+--------+---------------------+
| name | brithday | work | s_year | meetting |
+---------+------------+----------+--------+---------------------+
| liuhai | 1989-12-23 | 18:00:00 | 2013 | 2018-10-09 20:30:00 |
| zuhai | 1976-12-12 | 18:00:00 | 2013 | 2018-10-09 20:30:00 |
| liuhai2 | 1988-12-23 | 07:00:00 | 1995 | 2013-10-09 21:00:00 |
| zuhai1 | 1978-12-23 | 20:00:00 | 2023 | 2018-08-07 12:30:00 |
+---------+------------+----------+--------+---------------------+
4 rows in set (0.00 sec)
Datetime 和 timestamp 的区别
占据的空间和赋值的范围不一样
mysql> create table t3(meetting datetime, party timestamp);
Query OK, 0 rows affected (0.16 sec)
mysql> desc t3;
+----------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+-------------------+-----------------------------+
| meetting | datetime | YES | | NULL | |
| party | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+----------+-----------+------+-----+-------------------+-----------------------------+
2 rows in set (0.00 sec)
mysql> insert into t3 values(20170711122356,20180807122345);
Query OK, 1 row affected (0.06 sec)
mysql> select * from t3;
+---------------------+---------------------+
| meetting | party |
+---------------------+---------------------+
| 2017-07-11 12:23:56 | 2018-08-07 12:23:45 |
+---------------------+---------------------+
1 row in set (0.00 sec)
mysql> insert into t3(party) values (20160711103625);
Query OK, 1 row affected (0.03 sec)
mysql> select * from t3;
+---------------------+---------------------+
| meetting | party |
+---------------------+---------------------+
| 2017-07-11 12:23:56 | 2018-08-07 12:23:45 |
| NULL | 2016-07-11 10:36:25 |
+---------------------+---------------------+
2 rows in set (0.00 sec)
mysql> insert into t3(meetting) values (20180912101512);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t3;
+---------------------+---------------------+
| meetting | party |
+---------------------+---------------------+
| 2017-07-11 12:23:56 | 2018-08-07 12:23:45 |
| NULL | 2016-07-11 10:36:25 |
| 2018-09-12 10:15:12 | 2018-07-11 10:38:15 |
+---------------------+---------------------+
3 rows in set (0.00 sec)
[root@host50 ~]# date
2018年 07月 11日 星期三 10:39:24 CST
用函数给时间赋值 ——MYSQL服务的内置的命令 年月日的操作方法都是一样
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2018-07-11 10:43:16 |
+---------------------+
1 row in set (0.00 sec)
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
| 2018 |
+-------------+
1 row in set (0.01 sec
mysql> select month(20180410);
+-----------------+
| month(20180410) |
+-----------------+
| 4 |
+-----------------+
1 row in set (0.00 sec)
mysql> select date( now() ); ——当前系统的年月日
+-----------------+
| date( now() ) |
+-----------------+
| 2018-07-11 |
+-----------------+
1 row in set (0.00 sec)
mysql> select day( now() );
+---------------+
| day( now() ) |
+---------------+
| 11 |
+---------------+
1 row in set (0.00 sec)
mysql> select time( now() );
+-----------------+
| time( now() ) |
+-----------------+
| 10:48:43 |
+-----------------+
1 row in set (0.00 sec)
mysql> select month( now() );
+----------------+
| month( now() ) |
+----------------+
| 7 |
+----------------+
1 row in set (0.00 sec)
mysql> select year( now() );
+---------------+
| year( now() ) |
+---------------+
| 2018 |
+---------------+
1 row in set (0.00 sec)
mysql> insert into t2 values("lihhai1",date(1223),090000,year(19881225),now());
Query OK, 1 row affected (0.02 sec)
mysql> select * from t2;
+---------+------------+----------+--------+---------------------+
| name | brithday | work | s_year | meetting |
+---------+------------+----------+--------+---------------------+
| liuhai | 1989-12-23 | 18:00:00 | 2013 | 2018-10-09 20:30:00 |
| zuhai | 1976-12-12 | 18:00:00 | 2013 | 2018-10-09 20:30:00 |
| liuhai2 | 1988-12-23 | 07:00:00 | 1995 | 2013-10-09 21:00:00 |
| zuhai1 | 1978-12-23 | 20:00:00 | 2023 | 2018-08-07 12:30:00 |
| lihhai1 | 2000-12-23 | 09:00:00 | 1988 | 2018-07-11 11:04:19 |
+---------+------------+----------+--------+---------------------+
5 rows in set (0.00 sec)
unsigned ——无符号
mysql> create table t4(id int(4) unsigned zerofill,age int(2) unsigned zerofill);
Query OK, 0 rows affected (0.14 sec)
mysql> desc t4;
+-------+--------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------------------+------+-----+---------+-------+
| id | int(4) unsigned zerofill | YES | | NULL | |
| age | int(2) unsigned zerofill | YES | | NULL | |
+-------+--------------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> insert into t4 values (5,7);
Query OK, 1 row affected (0.03 sec)
mysql> select t4;
ERROR 1054 (42S22): Unknown column 't4' in 'field list'
mysql> select * from t4;
+------+------+
| id | age |
+------+------+
| 0005 | 07 |
+------+------+
1 row in set (0.00 sec)
mysql> insert into t4 values (1199,211);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t4;
+------+------+
| id | age |
+------+------+
| 0005 | 07 |
| 1199 | 211 |
+------+------+
2 rows in set (0.00 sec)