MySQL的启动和关闭
-
启动MySQL服务
net start mysql
-
关闭MySQL服务
net stop mysql
MySQL登录/退出
-
登录 mysql 参数
mysql -uroot -p -P3306 -h127.0.0.1
参数:
-u --user=name ,用户名 -p --password[=name] ,密码 -D --database=name ,打开指定数据库 -h --host=name ,服务器名称 -P --port=# ,端口号
-
退出
exit;
三种退出:
exit; quit; \q;
修改MySQL提示符
-
登录时通过参数指定
mysql -uroot -proot --prompt 提示符
-
登陆后,通过
prompt
命令修改mysql>prompt 提示符
-
MySQL提示符
\D 完整的日期 \d 当前数据库 \h 服务器名称 \u 当前用户
-
示例:
mysql> prompt \u@\h \d> root@localhost (none)>
MySQL常用命令
-
显示当前服务器版本
SELECT VERSION();
mysql> SELECT VERSION(); +------------+ | VERSION() | +------------+ | 5.5.20-log | +------------+ 1 row in set (0.00 sec)
-
显示当前日期时间
SELECT NOW();
mysql> SELECT NOW(); +---------------------+ | NOW() | +---------------------+ | 2016-07-13 13:24:27 | +---------------------+ 1 row in set (0.00 sec)
-
显示当前用户
SELECT USER();
mysql> SELECT USER(); +----------------+ | USER() | +----------------+ | root@localhost | +----------------+ 1 row in set (0.00 sec)
MySQL语句的规范
- 关键字与函数名称全部大写
- 数据库名称、表名称、字段名称全部小写
- SQL语句必须以分号结尾
操作数据库
-
创建数据库
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_namemysql> CREATE DATABASE t1; // 创建一个名为t1的数据库 Query OK, 1 row affected (0.00 sec)
-
如果输入
IF NOT EXISTS
,服务器将忽略掉错误的产生// 创建已有的数据库时,客户端报错 mysql> CREATE DATABASE t1; ERROR 1007 (HY000): Can't create database 't1'; database exists // 创建已有的数据库时,有(IF NOT EXISTS),服务器将忽略掉错误的产生 mysql> CREATE DATABASE IF NOT EXISTS t1; Query OK, 1 row affected, 1 warning (0.00 sec) // 创建数据库的同时,指定编码方式为 gbk mysql> CREATE DATABASE IF NOT EXISTS t2 CHARACTER SET gbk; Query OK, 1 row affected (0.00 sec)
-
查看警告信息 SHOW WARNINGS;
mysql> SHOW WARNINGS; // 显示警告信息 +-------+------+---------------------------------------------+ | Level | Code | Message | +-------+------+---------------------------------------------+ | Note | 1007 | Can't create database 't1'; database exists | +-------+------+---------------------------------------------+ 1 row in set (0.00 sec)
-
查看数据库的编码方式 SHOW CREATE DATABASE t1;
mysql> SHOW CREATE DATABASE t1; +----------+---------------------------------------------------------------+ | Database | Create Database | +----------+---------------------------------------------------------------+ | t1 | CREATE DATABASE `t1` /*!40100 DEFAULT CHARACTER SET latin1 */ | +----------+---------------------------------------------------------------+ 1 row in set (0.00 sec)
-
-
查看当前服务器下的数据库列表
SHOW {DATABASES | SCHEMAS}mysql> SHOW DATABASES; // 查看数据库列表 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | t1 | | test | +--------------------+ 5 rows in set (0.00 sec)
-
修改数据库
ALTER {DATABASE | SCHEMA} [db_name] [DEFAULT] CHARACTER SET [=] charset_name// 修改数据库 t1 的字符编码 mysql> ALTER DATABASE t1 CHARACTER SET = utf8; Query OK, 1 row affected (0.00 sec) // 查看 t1 的字符编码 mysql> SHOW CREATE DATABASE t1; +----------+-------------------------------------------------------------+ | Database | Create Database | +----------+-------------------------------------------------------------+ | t1 | CREATE DATABASE `t1` /*!40100 DEFAULT CHARACTER SET utf8 */ | +----------+-------------------------------------------------------------+ 1 row in set (0.00 sec)
-
删除数据库
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name// 显示数据库列表 mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | t1 | | t2 | | test | +--------------------+ 6 rows in set (0.00 sec) // 删除 t2 mysql> DROP DATABASE t2; Query OK, 0 rows affected (0.01 sec) // 再次显示数据库列表 mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | t1 | | test | +--------------------+ 5 rows in set (0.00 sec)
数据类型
指列、存储过程参数、表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型。
加UNSIGNED
表示无符号值
-
整型
-
浮点型
-
日期时间型
-
字符型
数据表
数据表(或称表)是数据库最重要的组成部分之一,是其他对象的基础
-
打开数据库
USE 数据库名称;
// 列出所有数据库 mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | t1 | | test | +--------------------+ 5 rows in set (0.00 sec) // 打开指定数据库 mysql> USE t1; Database changed // 显示当前打开的数据库 mysql> SELECT DATABASE(); +------------+ | DATABASE() | +------------+ | t1 | +------------+ 1 row in set (0.00 sec)
-
创建数据表
CREATE TABLE [IF NOT EXISTS] table_name( column_name data_type, ... )
column_name:列名,字段
-
data_type:数据类型
mysql> CREATE TABLE tb1( -> username VARCHAR(20), -> age TINYINT UNSIGNED, -> salary FLOAT(8,2) UNSIGNED -> ); Query OK, 0 rows affected (0.01 sec)
END
-
查看数据表列表
SHOW TABLES [FROM db_name] [LIKE 'pattern' | WHERE expr]// 打开数据库 mysql> USE t1; Database changed // 查看当前数据库中的数据表 mysql> SHOW TABLES; +--------------+ | Tables_in_t1 | +--------------+ | tb1 | +--------------+ 1 row in set (0.00 sec) // 在当前数据库中查看其他数据库中的数据表 mysql> SHOW TABLES FROM mysql; +---------------------------+ | Tables_in_mysql | +---------------------------+ | columns_priv | | ... | | user | +---------------------------+ 24 rows in set (0.01 sec)
-
查看数据表结构
SHOW COLUMNS FROM tbl_name// 打开数据库 mysql> USE t1; Database changed // 查看数据库中指定数据表的结构 mysql> SHOW COLUMNS FROM tb1; +----------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+-------+ | username | varchar(20) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | salary | float(8,2) unsigned | YES | | NULL | | +----------+---------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
-
插入记录
INSERT [INTO] tbl_name [(col_name,...)] VALUES(val,...)// 省略 col_name 后,值必须跟每列匹配 mysql> INSERT tb1 VALUES('duminghong',24,2333.33); Query OK, 1 row affected (0.00 sec) // 列数不匹配会报错 mysql> INSERT tb1 VALUES('dmh',23); ERROR 1136 (21S01): Column count doesn't match value count at row 1 // 给指定字段赋值 mysql> INSERT tb1(username,salary) VALUES('dmh',6666.66); Query OK, 1 row affected (0.00 sec)
-
查找记录
SELECT expr,... FROM tbl_name// 打开数据库 mysql> USE t1; Database changed // 显示数据表中的所有记录( * 为过滤所有的字段) mysql> SELECT * FROM tb1; +------------+------+---------+ | username | age | salary | +------------+------+---------+ | duminghong | 24 | 2333.33 | | dmh | NULL | 6666.66 | +------------+------+---------+ 2 rows in set (0.00 sec)
空值与非空
NULL,字段值可以为空
NOT NULL,字段值禁止为空
* 创建表 tb2 ,username 值禁止为空
mysql> CREATE TABLE tb2(
-> username VARCHAR(20) NOT NULL,
-> age TINYINT UNSIGNED
-> );
Query OK, 0 rows affected (0.01 sec)
* 显示表 tb2 的字段
mysql> SHOW COLUMNS FROM tb2;
+----------+---------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| username | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+----------+---------------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
* 插入字段值 username不为空,成功
mysql> INSERT tb2 VALUES('xxoo',NULL);
Query OK, 1 row affected (0.00 sec)
* 显示所有字段值
mysql> SELECT * FROM tb2;
+----------+------+
| username | age |
+----------+------+
| xxoo | NULL |
+----------+------+
1 row in set (0.00 sec)
* 插入字段值 username为空,报错
mysql> INSERT tb2 VALUES(NULL,24);
ERROR 1048 (23000): Column 'username' cannot be null
- 自动编号
AUTO_INCREMENT
自动编号,且必须与主键组合使用
默认情况下,起始值为1,每次的增量为1
AUTO_INCREMENT
必须与主键一起使用,而主键不一定与AUTO_INCREMENT
一起使用。
* 自动编号必须与主键组合使用,否则会报错
mysql> CREATE TABLE tb3(
-> id SMALLINT UNSIGNED AUTO_INCREMENT,
-> username VARCHAR(30) NOT NULL,
-> age TINYINT UNSIGNED
-> );
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
* 主键约束 ```PRIMARY KEY```
> 每张数据表只能存在一个主键
> 主键保证记录的唯一性
> 主键自动为NOT NULL
// 创建表 tb3
mysql> CREATE TABLE tb3(
-> id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> age TINYINT UNSIGNED
-> );
Query OK, 0 rows affected (0.00 sec)
// 查看表 tb3 的所有字段
mysql> SHOW COLUMNS FROM tb3;
+-------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+----------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
// 插入记录
mysql> INSERT tb3(age) VALUES(24);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT tb3(age) VALUES(32);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT tb3(age) VALUES(27);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT tb3(age) VALUES(12);
Query OK, 1 row affected (0.01 sec)
// 查看表的所有记录
mysql> SELECT * FROM tb3;
+----+------+
| id | age |
+----+------+
| 1 | 24 |
| 2 | 32 |
| 3 | 27 |
| 4 | 12 |
+----+------+
4 rows in set (0.00 sec)