MySQL万字教程,从入门到精通,争取三篇拿下MySQL初级知识,加油 ⛽
我们终其一生不是为了迎合每个人,而是要找和我们共振的那部分人,这世界很喧嚣,做好你自己就好 |
对mysql数据库的基本操作,启动,关闭,登录,退出,帮助等等……
net start mysql
C:\Windows\System32>net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
net stop mysql
C:\Windows\System32>net stop mysql
MySQL 服务正在停止.
MySQL 服务已成功停止。
注意,地址就是ip,本地可以写localhost和127.0.0.1,端口就是mysql启动服务占用的端口号 默认是3306
mysql -h 地址 -P 端口 -u root -p 密码
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 3
Server version: 5.7.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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.
ctrl + z 回车
exit
quit
show processlist;
mysql> show processlist;
+----+------+-----------+------+---------+------+----------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+----------+------------------+
| 4 | root | localhost | NULL | Query | 0 | starting | show processlist |
+----+------+-----------+------+---------+------+----------+------------------+
1 row in set (0.03 sec)
help 关键字;
对数据库的操作,包括查看当前使用的哪个数据库,使用数据库,查看所有数据库,查看当前时间,查看当前用户,查看数据库版本,查看创建数据库语句,创建数据库,以及删除数据库等围绕着数据库的相关操作
select database();
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.03 sec)
show databases;
use 数据库;
mysql> use db12;
Database changed
select now();
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2022-05-20 00:01:35 |
+---------------------+
1 row in set (0.03 sec)
select user();
mysql> select user();
+--------+
| user() |
+--------+
| root@ |
+--------+
1 row in set (0.00 sec)
select version();
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.19 |
+-----------+
1 row in set (0.04 sec)
show create database 数据库名;
mysql> show create database db12;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| db12 | CREATE DATABASE `db12` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
create database 数据库名;
mysql> create database db14;
Query OK, 1 row affected (0.03 sec)
create database 数据库名 charset utf8;
mysql> create database db15 charset utf8;
Query OK, 1 row affected (0.03 sec)
create database if not exists 数据库名;
mysql> create database if not exists db15;
Query OK, 1 row affected, 1 warning (0.00 sec)
drop database 数据库名;
mysql> drop database db14;
Query OK, 0 rows affected (0.05 sec)
drop database if exists 数据库名;
mysql> drop database if exists db15;
Query OK, 0 rows affected (0.00 sec)
show tables;
mysql> show tables;
Empty set (0.00 sec)
mysql> show tables from db12;
Empty set (0.00 sec)
id 主键
grade_name 班名 varchar(100)
mysql> create table grade(
-> id int primary key auto_increment,
-> grade_name varchar(100) comment "班级名字"
-> );
Query OK, 0 rows affected (0.06 sec)
id 主键
name 学生姓名 varchar(100) not null
sex 学生性别
创建数据表 stu ,并在表 stu 上创建外键约束,让它的键 gradeId 作为外键关联到表 grade 的主键 id ,SQL这样写
[CONSTRAINT <外键名>] FOREIGN KEY 字段名 [,字段名2,…]
REFERENCES <主表名> 主键列1 [,主键列2,…]
CREATE TABLE stu (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR (100) NOT NULL,
age INT (3),
bir DATE,
english INT (3),
chinese INT (3),
gradeId INT,
CONSTRAINT fk_stu_grade FOREIGN KEY (gradeId) REFERENCES grade (id)
) ;
注意:从表的外键关联的必须是主表的主键,且主键和外键的数据类型必须一致。例如,两者都是 INT 类型,或者都是 CHAR 类型。如果不满足这样的要求,在创建从表时,就会出现“ERROR 1005(HY000): Can’t create table”错误
假如在创建表时,没有添加外键约束,那么可以在创建表后通过修改表给表添加外键约束,语法如下
ALTER TABLE <数据表名> ADD CONSTRAINT <外键名>
FOREIGN KEY(<列名>) REFERENCES <主表名> (<列名>);
CREATE TABLE stu (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR (100) NOT NULL,
age INT (3),
bir DATE,
english INT (3),
chinese INT (3),
gradeId INT
) ;
ALTER TABLE stu
ADD CONSTRAINT fk_stu_grage FOREIGN KEY (gradeId) REFERENCES grade (id) ;
注意:在为已经创建好的数据表添加外键约束时,要确保添加外键约束的列的值全部来源于主键列,并且外键列不能为空。
通过外键名删除外键,这里演示一下,删除之后会继续添加上的,因为需要这个外键约束
ALTER TABLE <表名> DROP FOREIGN KEY <外键约束名>;
ALTER TABLE stu DROP FOREIGN KEY fk_stu_grage;
DESC stu;
SHOW ENGINES;
mysql> alter table stu rename to stus;
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+----------------+
| Tables_in_db12 |
+----------------+
| grade |
| stus |
+----------------+
2 rows in set (0.00 sec)
alter table 表名 add 列名 列数据类型;
alter table 表名 modify 列名 新的数据类型;
alter table 表名 modify 字段名 字段属性 first;
alter table 表名 modify 字段 字段属性 after 字段;
alter table 表名 change 原列名 新列名 新数据类型;
alter table 表名 drop 列名;
drop table 表名;
drop table if exists 表名;
INSERT INTO 表名(字段,字段……) VALUES(与字段对应);
mysql> desc stus;
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| gradeId | int(11) | YES | MUL | NULL | |
| NAME | varchar(100) | NO | | NULL | |
| age | int(3) | YES | | NULL | |
| bir | date | YES | | NULL | |
| english | int(3) | YES | | NULL | |
| chinese | int(3) | YES | | NULL | |
+---------+--------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> desc grade;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| grade_name | varchar(100) | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
INSERT INTO stus(NAME,age,bir,english,gradeId) VALUES("热爱编程",18,"2002-06-01",78,1);
mysql> select * from stus;
+----+---------+-----------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+-----------+------+------------+---------+---------+
| 1 | 1 | 热爱编程 | 18 | 2002-06-01 | 78 | NULL |
+----+---------+-----------+------+------------+---------+---------+
1 row in set (0.00 sec)
mysql> insert into stus values(2,2,"张郁苗",18,"2002-11-07",130,120);
Query OK, 1 row affected (0.03 sec)
给全部列添加数据时,可以简写,不建议,因为代码可读性变差,推荐写成下面这样,一眼就能知道要插入的字段是什么
mysql> insert into stus(gradeId,name,age,bir,english,chinese) values
-> (1,"小猪佩奇",6,"2015-06-10",34,23);
Query OK, 1 row affected (0.03 sec)
mysql> select * from stus;
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 1 | 1 | 热爱编程 | 18 | 2002-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | 23 |
+----+---------+--------------+------+------------+---------+---------+
3 rows in set (0.00 sec)
mysql> insert into stus(gradeId,name,age,bir,english,chinese) values
-> (3,"猪妈妈",7,"2012-09-11",56,78),
-> (2,"谷歌",6,"2000-11-11",100,110);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from stus;
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 1 | 1 | 热爱编程 | 18 | 2002-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | 23 |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
| 6 | 2 | 谷歌 | 6 | 2000-11-11 | 100 | 110 |
+----+---------+--------------+------+------------+---------+---------+
6 rows in set (0.00 sec)
select last_insert_id();
mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
| 5 |
+------------------+
1 row in set (0.03 sec)
update 表 set 要更新字段 = 要更新成啥 where 更新条件;
注意:如果不使用 where 字句进行限制,就会把表里所有记录都修改了,凉凉了,可别怪本帅哥没提醒你
修改stus表名字叫热爱编程的生日,改为 2001-06-01
mysql> update stus set bir = "2001-06-01" where name = "热爱编程";
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from stus;
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 1 | 1 | 热爱编程 | 18 | 2001-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | 23 |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
| 6 | 2 | 谷歌 | 6 | 2000-11-11 | 100 | 110 |
+----+---------+--------------+------+------------+---------+---------+
6 rows in set (0.00 sec)
把id为6的名字改为 胡歌,年龄改为24
mysql> update stus set name = "胡歌",age = 24 where id = 6;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from stus;
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 1 | 1 | 热爱编程 | 18 | 2001-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | 23 |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
| 6 | 2 | 胡歌 | 24 | 2000-11-11 | 100 | 110 |
+----+---------+--------------+------+------------+---------+---------+
6 rows in set (0.00 sec)
当使用update语句进行多行更新,如果在更新过程中出错了,则整个update语句都会被取消,恢复到更新之前,如果你想即使发生错误,也继续进行更新,可以使用ignore
关键字
语法:
update ignore 表 set 要更新字段 = 要更新成啥 where 更新条件;
当我们想要将记录的某个字段或多个字段改为空,就可以将它修改为 null ,而不是空字符串,mysql中的空为null
例如:
修改id为3的中文成绩为null空
mysql> update stus set chinese = null where id = 3;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from stus;
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 1 | 1 | 热爱编程 | 18 | 2001-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | NULL |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
| 6 | 2 | 胡歌 | 24 | 2000-11-11 | 100 | 110 |
+----+---------+--------------+------+------------+---------+---------+
6 rows in set (0.00 sec)
delete from 表 where 限制条件;
删除id为6的数据
mysql> delete from stus where id = 6;
Query OK, 1 row affected (0.03 sec)
mysql> select * from stus;
+----+---------+--------------+------+------------+---------+---------+
| id | gradeId | NAME | age | bir | english | chinese |
+----+---------+--------------+------+------------+---------+---------+
| 1 | 1 | 热爱编程 | 18 | 2001-06-01 | 78 | NULL |
| 2 | 2 | 张郁苗 | 18 | 2002-11-07 | 130 | 120 |
| 3 | 1 | 小猪佩奇 | 6 | 2015-06-10 | 34 | NULL |
| 4 | 3 | 猪爸爸 | 8 | 2012-09-12 | 34 | 56 |
| 5 | 3 | 猪妈妈 | 7 | 2012-09-11 | 56 | 78 |
+----+---------+--------------+------+------------+---------+---------+
5 rows in set (0.00 sec)
警告:不要省略 where 子句,如果不使用 where 子句进行限制,别怪我没提醒你,你会把表中所有行全部干掉,准备后事吧!兄弟
delete from stus;
这里就不执行了
注意:delete语句从表中删除行,甚至删除表中所有行,但是,delete不删除表本身
如果你想从表中删除所有数据,不要使用delete,可以使用 truncate 表名,它完成相同的工作,但是速度更快,因为它实际上是删除原来的表并重新创建一张表,而不是逐行删除表中的数据
mysql> select * from user;
+------+------+
| id | name |
+------+------+
| 1 | hah1 |
+------+------+
1 row in set (0.00 sec)
mysql> truncate user;
Query OK, 0 rows affected (0.06 sec)
mysql> select * from user;
Empty set (0.00 sec)