MySQL
MySQL 的下载
一定要从官网下载安装包,用安装包下载
官网
MySQl Community Server(GPL)
- 记住版本信息
- 版本不会改变
- 版本与企业一致
卸载
- 安装与卸载必须重启电脑
必须重启 - MySQl是服务不是软件,卸载只有唯一一种办法就是:只能用安装包卸载
- 重新安装就可以卸载(安装即卸载)
- Remove(移除)
- 最后两个“yes”都要勾选
安装
- 选第一个 Developer Default
- 默认端口号3306 初次安装使用默认 只能用一次
- 一台电脑可以有多个版本的MySQl,更改端口号
- root密码越简单越好 1234;mysql 中最高权限账号
- Windows 服务名称:MySQl57 默认(使用版本命名)
- Mysql安装成功的唯一服务中显示正在运行(此电脑——右键管理——服务和应用程序——服务)
登录
-
C盘-windows-System32-cmd.exe(以管理员方式运行)
-
初次使用不识别,找不到(‘mysql’ 不是内部命令或外部命令,也不是可运行的程序或批处理文件)属于外部命令
-
配置Path变量
-
C盘——Program Files——MySQl——MySQl Servers——bin——mysql.exe
(复制途径) -
此电脑——属性——高级系统设置——环境变量——系统变量Path——点进去——在空白处把路径粘贴进去
Win7(英文输入;再粘贴路径)
mysql -u root -p
或者mysql -h 127.0.0.1 -P(大写) 3306 -u root -p(小写)
或者mysql -h localhost -P(大写) 3306 -u root -p(小写)
root密码:1234
localhost(本地用户)
127.0.0.1默认本机地址
当mysql不在同一台电脑时
首先找到这台电脑IP地址,然后端口号;
mysql -h 127.0.0.1 -P(大写) 3306 -u root -p(小写)
-h hostnane 服务器地址
大写-P表示端口号
-u用户
-p(小写)密码
P没有大写报错
C:\Windows\system32>mysql -h localhost -p 3306 -u root -p
Enter password: ****
ERROR 1049 (42000): Unknown database '3306'
不识别数据库3306
后面多加;
C:\Windows\system32>mysql -h localhost -p 3306 -u root -p;
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
报错:[警告]在命令行界面可以使用密码不安全,把;识别成密码,所以密码也不能直接跟在后面,否则报错。
正确写法
C:\Windows\system32>mysql -h localhost -P 3306 -u root -p
Enter password: ****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.7.9-log MySQL Community Server (GPL)
Copyright (c) 2000, 2015, 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>
初识MySQl
数据类型
往数据库存东西,必须指定数据类型
- FLOAT DOUBLE(不常用)
- 定点数据型解决了浮点型的不精确
- CHAR(确定只存一个字符:男/女、是/否、0/1)
MySQl中的约束
- NOT NULL(非空)
- DEFAULT (默认)
- UNIQUE KEY(UK) (唯一)
- PRIMARY KEY(PK) (主键)
主键包含非空且唯一 - AUTO_INCREMENT (自动增长)
- FOREIGN KEY(FK) (外键)
子表的取值受父表影响称之为外键约束,子表指向父表
先有父表再有子表
先删除子表再删父表
了解SQL语句
背 练 错
SQL(结构化查询语句)
三种
- 数据定义语言(DDL)
- 数据操作语言(DML)
- 数据控制语言(DCL)
show databases;
show databases;
show databases;
显示所有数据库,databases是复数
;或者\g 称为命令结束标识符
没有命令不执行
mysql> show dabataese;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'dabataese' at line 1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
在表中有6行,操作sh0.00秒。
##有几个;就执行几次,几个命令之间不互相影响,按顺序分开执行
mysql> show databases;show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
mysql>
mysql> show database;show databases;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'database' at line 1
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
mysql>
第一个报错你有一处错误,在‘database’附近检查,而第二个命令照样执行,互不干扰。
4个数据库东西不能动
逐个执行
创建数据库
create database hello(创建是单数 database)
mysql> create database hello;
Query OK, 1 row affected (0.00 sec)
##创建成功,1行
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hello | 新的存进去
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)
mysql>
mysql> create databases hello1; (单词错误,databases)
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databases hello1' at line 1
mysql>
你有一个错误在您的SQL语法;检查手册对应于你的MySQL服务器版本正确的语法使用“数据库hello1”在1号线附近
再次创建不能重名
mysql> create database hello;
ERROR 1007 (HY000): Can't create database 'hello'; database exists
mysql>
无法创建数据库“hello”;数据库已经存在
drop database hello(删除)
mysql> drop database hello; (删除hello)
Query OK, 0 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database | 表中已经不存在hello
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
mysql>