MySQL第二课(库的操作、表的操作、数据类型)


库操作&表操作:

mysql> create database a;   //创建数据库a
Query OK, 1 row affected (0.00 sec)
mysql> show create database a;
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| a        | CREATE DATABASE `a` /*!40100 DEFAULT CHARACTER SET latin1 */ |       //latin1是当前使用的字符编码格式
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec

mysql> show charset;     //显示字符编码
……
mysql> help create database;    //获取create database的帮助
……
mysql> create database b default charset=utf8;     //创建数据库b,并指定(更改)默认字符编码为utf8
mysql> show create database b;   //显示创建数据库b的信息
+----------+------------------------------------------------------------+
| Database | Create Database                                            |
+----------+------------------------------------------------------------+
| b        | CREATE DATABASE `b` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+------------------------------------------------------------+

mysql> create table if not exists student (name varchar(32));   //创建student表
Query OK, 0 rows affected (0.03 sec)
mysql> insert into student values ('a'),('A'),('b'),('B');   //写入四条数据
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> select * from student;     //显示表中数据
+------+
| name |
+------+
| a    |
| A    |
| b    |
| B    |
+------+
4 rows in set (0.00 sec)
mysql> select * from student where name ='a';       //默认的校验规则不分大小写,所以a和A都显示  
+------+
| name |
+------+
| a    |
| A    |
+------+
2 rows in set (0.00 sec)

show collation;      //显示校验规则

mysql> create database if not exists c default charset=utf8 collate utf8_bin;  
//创建数据库c,默认编码为utf8,默认校验规则为 collate utf8_bin
Query OK, 1 row affected (0.00 sec)
mysql> show create database c;   //显示?……
+----------+-----------------------------------------------------------------------------+
| Database | Create Database                                                             |
+----------+-----------------------------------------------------------------------------+
| c        | CREATE DATABASE `c` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin */ |
+----------+-----------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> use c;
Database changed
mysql> create table if not exists student (name varchar(32));   //创建student表
Query OK, 0 rows affected (0.01 sec)
mysql> insert into student values ('a'),('A'),('b'),('B');   //写入数据
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0
mysql> select * from student where name='a';       //utf8_bin区分大小写,所以只有a
+------+
| name |
+------+
| a    |
+------+
1 row in set (0.00 sec)
mysql> select * from student order by name;      //依据名字排序    校验规则也会影响排序
+------+
| name |
+------+
| A    |
| B    |
| a    |
| b    |
+------+
4 rows in set (0.00 sec)

校验规则的影响:
(1)大小写  (不区分大小写:邮箱、windows创建文件夹的名字;  区分大小写:密码、linux系统下的文件名)
(2)排序

mysql> exit   //退出数据库
Bye
[du@localhost ~]$ which mysql   //查看mysql所在位置
/usr/bin/mysql
[du@localhost ~]$ ls /usr/bin/mysql*   
/usr/bin/mysql                       /usr/bin/mysql_fix_privilege_tables
/usr/bin/mysqlaccess                 /usr/bin/mysqlhotcopy
/usr/bin/mysqladmin                  /usr/bin/mysqlimport
/usr/bin/mysqlbinlog                 /usr/bin/mysql_install_db
/usr/bin/mysqlbug                    /usr/bin/mysql_secure_installation
/usr/bin/mysqlcheck                  /usr/bin/mysql_setpermission
/usr/bin/mysql_config                /usr/bin/mysqlshow
/usr/bin/mysql_convert_table_format  /usr/bin/mysqlslap
/usr/bin/mysqld_multi                /usr/bin/mysqltest
/usr/bin/mysqld_safe                 /usr/bin/mysql_tzinfo_to_sql
/usr/bin/mysqldump                   /usr/bin/mysql_upgrade
/usr/bin/mysqldumpslow               /usr/bin/mysql_waitpid
/usr/bin/mysql_find_rows             /usr/bin/mysql_zap
/usr/bin/mysql_fix_extensions
[du@localhost ~]$ mysqldump  //备份命令
Usage: mysqldump [OPTIONS] database [tables]
OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
For more options, use mysqldump --help
[du@localhost ~]$ mysqldump -u root -p  --databases c > /c_db.sql     //备份数据库c (重定义>为/c_db.sql)
bash: /c_db.sql: Permission denied     //转到root用户下再执行
[du@localhost ~]$ su - root   
[root@localhost ~]# mysqldump -u root -p  --databases b > /c_db.sql   //备份数据库c到/c_db.sql
Enter password:
[root@localhost ~]# mysql -u  root -p     //再次连接数据库
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> drop database b;       //前面已经备份成功,这里可以删除数据库b
Query OK, 1 row affected (0.00 sec)
mysql> show databases;    / /显示当前存在的数据库,已经没有了数据库b
+--------------------+
| Database           |
+--------------------+
| information_schema |
| a                  |
| c                  |
| mysql              |
| test               |
+--------------------+
5 rows in set (0.00 sec)
mysql> source /c_db.sql       //恢复/c_db.sql   ???
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 1 row affected (0.00 sec)
Database changed
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 8 rows affected (0.00 sec)
Records: 8  Duplicates: 0  Warnings: 0
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;   //恢复成功,再来显示当前存在的数据库,数据库b又回来了
+--------------------+
| Database           |
+--------------------+
| information_schema |
| a                  |
| b                  |
| c                  |
| mysql              |
| test               |
+--------------------+
6 rows in set (0.00 sec)

mysql> show columns from student;          //显示表结构(显示每一列)
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(32) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> show processlist;      //查看当前连接的人
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+   //只有一个id为11的连接者
| 11 | root | localhost | b    | Query   |    0 | NULL  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)
mysql> kill 11;               //删掉id为11的连接者
Query OK, 0 rows affected (0.00 sec)
mysql> show processlist;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...         //因为只有一个还删了,所以重新分配并连接
Connection id:    12
Current database: b
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
| 12 | root | localhost | b    | Query   |    0 | NULL  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
1 row in set (0.00 sec)

一个实例(包括库的操作和表的操作):

mysql> create database if not exists bit default character set utf8 collate utf8_general_ci;     
//没有简写  character set=charset
Query OK, 1 row affected (0.00 sec)
mysql> show create database bit;
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| bit      | CREATE DATABASE `bit` /*!40100 DEFAULT CHARACTER SET utf8 */ |   
//不显示校验规则是因为默认的校验规则就是utf8_general_ci
+----------+--------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> alter table user add column login varchar(32);          //给user表加一列login
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc user;       //显示user的属性设置
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| name     | varchar(32) | YES  |     | NULL    |       |
| password | varchar(32) | YES  |     | NULL    |       |
| login    | varchar(32) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> insert into user values (1,'bit',md5('123'),'123');      //给user表添加一条信息(采用加密密码)
Query OK, 1 row affected (0.00 sec)
mysql> select * from user;     //显示user表中的所有数据
+------+------+----------------------------------+-------+
| id   | name | password                         | login |
+------+------+----------------------------------+-------+
|    1 | bit  | 202cb962ac59075b964b07152d234b70 | 123   |
+------+------+----------------------------------+-------+
1 row in set (0.00 sec)
mysql> alter table user modify column name varchar(8);          //修改user表中name的属性(varchar(32)->varchar(8))
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0
mysql> desc user;   
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| name     | varchar(8)  | YES  |     | NULL    |       |
| password | varchar(32) | YES  |     | NULL    |       |
| login    | varchar(32) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table user modify column name varchar(1);   //修改user表中name的属性(varchar(8)->varchar(1))
Query OK, 1 row affected, 1 warning (0.01 sec)   //虽然有一条警告,但修改成功
Records: 1  Duplicates: 0  Warnings: 1
mysql> desc user;            //没有报错   数据库中有信息长于1个字符却没有报错,不正常
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| name     | varchar(1)  | YES  |     | NULL    |       |       //name已被修改为varchar(1)
| password | varchar(32) | YES  |     | NULL    |       |
| login    | varchar(32) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select * from user;
+------+------+----------------------------------+-------+
| id   | name | password                         | login |
+------+------+----------------------------------+-------+
|    1 | b    | 202cb962ac59075b964b07152d234b70 | 123   |                  //name=bit被截成name=b
+------+------+----------------------------------+-------+
1 row in set (0.00 sec)

mysql> set sql_mode=strict_trans_tables;      //严格校验,报错   之前不报错是因为版本低    >5.7.4版本的时候才能开启
Query OK, 0 rows affected (0.00 sec)
mysql> alter table user modify column password varchar(1);        
//再修改password为varchar(1)时报错,因为表中已经有md5加密后的密码“123”,长度大于1个字符
ERROR 1265 (01000): Data truncated for column 'password' at row 1

mysql> alter table user drop column name;         //删除user表的name列
Query OK, 1 row affected (0.01 sec)
Records: 1  Duplicates: 0  Warnings: 0
mysql> desc user;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| password | varchar(32) | YES  |     | NULL    |       |
| login    | varchar(32) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> drop table if exists user;      //删除user表
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> drop table user;     //再删报错(不加if exists)
ERROR 1051 (42S02): Unknown table 'user'
mysql> drop table if exists user;         
Query OK, 0 rows affected, 1 warning (0.00 sec)      //警告(加上if exists)
mysql> show warnings;         //查看警告
+-------+------+----------------------+
| Level | Code | Message              |
+-------+------+----------------------+
| Note  | 1051 | Unknown table 'user' |
+-------+------+----------------------+
1 row in set (0.00 sec)

mysql> create  table user (id int , name varchar(32), login varchar(32), password varchar(32));   //重建user表
Query OK, 0 rows affected (0.01 sec)
mysql> alter table user change column name nick varchar(32);   //修改name列的名字为nick
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc user;     //显示表的属性设置    修改成功
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+       //default即默认 表示默认值
| id       | int(11)     | YES  |     | NULL    |       |
| nick     | varchar(32) | YES  |     | NULL    |       |
| login    | varchar(32) | YES  |     | NULL    |       |
| password | varchar(32) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

数据类型:

mysql> help data types;   //出错
Nothing found
Please try to run 'help contents' for a list of all accessible topics
mysql> help contents;   //要先获取目录,再翻到具体的章节
……
mysql> help data types;   //再获取data types的帮助

mysql> create table t1 (a int , b bit(8));   //创建t1表 a为int类型,b为bit类型
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t1 values(10,10);     //给a 10,给b也是10   
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+------+------+
| a    | b    |
+------+------+                //10是一个不可见的字符(ASSII码)
|   10 |
    |
+------+------+
1 row in set (0.00 sec)
mysql> insert into t1 values(10,65);           //65对应字符A
Query OK, 1 row affected (0.00 sec)
mysql> select * from t1;
+------+------+
| a    | b    |
+------+------+
|   10 |
    |
|   10 | A    |
+------+------+
2 rows in set (0.00 sec)
mysql> create table t2 (name bit(65));           //bit类型的最大值为64位
ERROR 1439 (42000): Display width out of range for column 'name' (max = 64)

mysql> create table t3 (a int unsigned , b int unsigned);   //设置为无符号int型
Query OK, 0 rows affected (0.01 sec)
mysql> insert into t3 value (1,1);     //插入一条数据 a=1,b=1
Query OK, 1 row affected (0.00 sec)
mysql> select * from t3;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
+------+------+
1 row in set (0.00 sec)
mysql> select a-b from t3;     //查询a-b的值
+------+
| a-b  |
+------+
|    0 |
+------+
1 row in set (0.00 sec)

mysql> insert into t3 value (1,2);  
Query OK, 1 row affected (0.00 sec)
mysql> select * from t3;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    1 |    2 |
+------+------+
2 rows in set (0.00 sec)
mysql> select a-b from t3;     //无符号数类型下,1-2!=-1
+----------------------+
| a-b                  |
+----------------------+
|                    0 |
| 18446744073709551615 |
+----------------------+
2 rows in set (0.00 sec)
mysql> set sql_mode=no_unsigned_subtraction;   //为了得到想要的结果,用该命令更改设置
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t3;
+------+------+
| a    | b    |
+------+------+
|    1 |    1 |
|    1 |    2 |
+------+------+
2 rows in set (0.00 sec)
mysql> select a-b from t3;     //得到-1
+------+
| a-b  |
+------+
|    0 |
|   -1 |
+------+
2 rows in set (0.00 sec)

mysql> insert into t1 values (300,300);
Query OK, 1 row affected, 1 warning (0.00 sec)   //给出警告  但成功   不应该成功,应该报错
mysql> show warnings;
+---------+------+--------------------------------------------+
| Level   | Code | Message                                    |
+---------+------+--------------------------------------------+           //越界
| Warning | 1264 | Out of range value for column 'b' at row 1 |
+---------+------+--------------------------------------------+
1 row in set (0.00 sec
mysql> set sql_mode=strict_trans_tables;      //防止越界,严格校验,报错
Query OK, 0 rows affected (0.00 sec)
mysql> select * from t1;
+------+------+
| a    | b    |
+------+------+
|   10 |
    |
|   10 | A    |
|  300 | �    |                                   //乱码
+------+------+
3 rows in set (0.00 sec)
mysql> insert into t1 values (300,300);
ERROR 1406 (22001): Data too long for column 'b' at row 1         //设置后插入300大小的bit类型,会报错



















你可能感兴趣的:(数据库)