1、添加字段

alter table 表名 add 字段 修饰符;
mysql> alter table t3  add math int(10);-------添加的字段
mysql> alter table t3  add (chinese int(10),english int(10));------添加多个字段,中间用逗号隔开。
alter table 表名 add 添加的字段(和修饰) after name; -------把添加的字段放到name后面
alter table 表名 add 添加的字段(和修饰) first; ----------把添加的字段放在第一个

2.修改字段数据类型、修饰符

1.修改名称、数据类型、修饰符
alter table 表名 change 旧字段 新字段 修饰符; #change修改字段名称,类型,约束,顺序
mysql> alter table t3 change max maxs int(15) after id;  #修改字段名称与修饰并更换了位置
2.修改字段类型,约束,顺序
alter table 表名 modify 字段 属性 修饰符; #modify 不能修改字段名称
mysql> alter table t3 modify maxs int(20) after math;    #修改修饰并更换位置
3.删除字段
mysql> alter table t3 drop maxs;  #drop 丢弃的字段。

3.插入数据(添加纪录)

字符串必须引号引起来
记录与表头相对应,表头与字段用逗号隔开。
1.添加一条记录
insert into 表名(字段1,字段2,字段3,字段4) values(1,"tom","m",90);
mysql> insert into t3(id,name,sex,age) values(1,"tom","m",18);
Query OK, 1 row affected (0.00 sec)
注:添加的记录与表头要对应,
2.添加多条记录
mysql> insert into t3(id,name,sex,age) values(2,"jack","m",19),(3,"xiaoli","f",20);
Query OK, 2 rows affected (0.34 sec)
3.用set添加记录
mysql> insert into t3 set id=4,name="zhangsan",sex="m",age=21;
Query OK, 1 row affected (0.00 sec)
4.更新记录
update 表名 set  修改的字段  where  给谁修改;
mysql> update t3 set id=6 where name="xiaoli";
5.删除记录
1.删除单条记录
mysql> delete from t3 where id=6;   #删除那个记录,等于几会删除那个整条记录
Query OK, 1 row affected (0.35 sec)
2.删除所有记录
mysql> delete from t3;

4.单表查询

测试表:company.employee5
mysql> create database company;   #创建一个库;
创建一个测试表:
mysql> CREATE TABLE company.employee5(id int primary key AUTO_INCREMENT not null,name varchar(30) not null,sex enum('male','female') default 'male' not null,hire_date date not null,post varchar(50) not null,job_description varchar(100),salary double(15,2) not null,office int,dep_id int);
插入数据:
mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values ('jack','male','20180202','instructor','teach',5000,501,100),('tom','male','20180203','instructor','teach',5500,501,100),('robin','male','20180202','instructor','teach',8000,501,100),('alice','female','20180202','instructor','teach',7200,501,100),('tianyun','male','20180202','hr','hrcc',600,502,101),('harry','male','20180202','hr',NULL,6000,502,101),('emma','female','20180206','sale','salecc',20000,503,102),('christine','female','20180205','sale','salecc',2200,503,102),('zhuzhu','male','20180205','sale',NULL,2200,503,102),('gougou','male','20180205','sale','',2200,503,102);
mysql> use company
语法:
mysql> select   字段名称,字段名称2    from  表名   条件
简单查询
mysql> select * from employee5;
多字段查询:
mysql> select id,name,sex from employee5;
有条件查询:where
mysql> select id,name from employee5 where id<=3;
mysql> select id,name,salary from employee5 where salary>2000;
设置别名:as
mysql> select id as "id_num" from employee5 where id>5;
给 id 的值起个别名,显示值的表头会是设置的别名
统计记录数量:count()
mysql> select count(*) from employee5;
统计字段得到数量:
mysql> select count(id) from employee5;
避免重复DISTINCT:表里面的数据有相同的
mysql> select distinct post from employee5;#字段    表名
表复制:key不会被复制: 主键、外键和索引
复制表
1.复制表结构+记录 (key不会复制: 主键、外键和索引)
语法:create table 新表 select * from 旧表;
mysql> create table new_t1 select * from employee5;
2.复制单个字段:
mysql> create table new_t2(select id,name from employee5);
3.多条件查询:  and   ----和
语法: select   字段,字段2 from   表名   where   条件 and where 条件;
mysql> SELECT name,salary from employee5 where post='hr' AND salary>1000;
mysql> SELECT name,salary from employee5 where post='instructor' AND salary>1000;
4.多条件查询:  or   ----或者
语法:       select   字段,字段2 from   表名   where   条件   or   条件;
mysql> select name from employee5 where salary>5000 and salary<10000 or dep_id=102;
mysql> select name from employee5 where salary>2000 and salary<6000 or dep_id=100;
5.关键字 BETWEEN AND  什么和什么之间。
mysql> SELECT name,salary FROM employee5 WHERE salary BETWEEN 5000 AND 15000;
mysql> SELECT name,salary FROM employee5 WHERE salary NOT BETWEEN 5000 AND 15000;
mysql> select name,dep_id,salary from employee5 where  not salary>5000 ;
注:not  给条件取反
6.关键字IS NULL   空的
mysql> SELECT name,job_description FROM employee5 WHERE job_description IS NULL;
mysql> SELECT name,job_description FROM employee5  WHERE job_description IS NOT NULL;  #-取反 不是null
mysql> SELECT name,job_description FROM employee5 WHERE job_description=''; #什么都没有==空
NULL说明:1、等价于没有任何值、是未知数。2、NULL与0、空字符串、空格都不同,NULL没有分配存储空间。3、对空值做加、减、乘、除等运算操作,结果仍为空。4、比较时使用关键字用“is null”和“is not null”。5、排序时比其他数据都小(索引默认是降序排列,小→大),所以NULL值总是排在最前。
7.关键字IN集合查询
一般查询:
mysql> SELECT name,salary FROM employee5 WHERE salary=4000 OR salary=5000 OR salary=6000 OR salary=9000;
IN集合查询
mysql> SELECT name, salary FROM employee5 WHERE salary IN (4000,5000,6000,9000);
mysql> SELECT name, salary FROM employee5 WHERE salary NOT IN (4000,5000,6000,9000); #取反
8.排序查询    order by  :命令指令,在mysql是排序的意思。
mysql> select name,salary from employee5 order by salary; #-默认从小到大排序。
mysql> select name,salary from employee5 order by salary desc; #降序,从大到小
9.limit 限制
mysql> select * from employee5 limit 5;  #只显示前5行
mysql> select name,salary from employee5 order by salary desc limit 0,1; #从第几行开始,打印一行
查找什么内容从那张表里面降序排序只打印第二行。
注意:
0-------默认第一行
1------第二行  依次类推...
mysql> SELECT * FROM employee5 ORDER BY salary DESC LIMIT 0,5;  #降序,打印5行
mysql> SELECT * FROM employee5 ORDER BY salary DESC LIMIT 4,5;  #从第5条开始,共显示5条
mysql> SELECT * FROM employee5 ORDER BY salary  LIMIT 4,3;  #默认从第5条开始显示3条。
10.分组查询 :group  by
mysql> select count(name),post from employee5 group by post;
+-------------+------------+
| count(name) | post       |
+-------------+------------+
|           2 | hr         |
|           4 | instructor |
|           4 | sale       |
+-------------+------------+count可以计算字段里面有多少条记录,如果分组会分组做计算mysql> select count(name),group_concat(name) from employee5 where salary>5000;查找 统计(条件:工资大于5000)的有几个人(count(name)),分别是谁(group_concat(name))
+-------------+----------------------------+
| count(name) | group_concat(name)         |
+-------------+----------------------------+
|           5 | tom,robin,alice,harry,emma |
+-------------+----------------------------+
11.GROUP BY和GROUP_CONCAT()函数一起使用
GROUP_CONCAT()-------组连接
mysql> SELECT dep_id,GROUP_CONCAT(name) FROM employee5 GROUP BY dep_id; #以dep_id分的组,dep_id这个组里面都有谁
mysql> SELECT dep_id,GROUP_CONCAT(name) as emp_members FROM employee5 GROUP BY dep_id; #给组连接设置了一个别名
12.函数
max() 最大值
mysql> select max(salary) from employee5;
查询薪水最高的人的详细信息:
mysql> select name,sex,hire_date,post,salary,dep_id from employee5 where salary = (SELECT MAX(salary) from employee5);
min()最小值
select min(salary) from employee5;
avg()平均值
select avg(salary) from employee5;
now()  现在的时间
select now();
sum()  计算和
select sum(salary) from employee5 where post='sale';

5.使用正则

select * from t1 where name regexp '^ali';
select * from t1 where name regexp 'wei$';
select * from t1 where name regexp 'm{2}';
#regexp 正则标志对字符串匹配的方式
where name = 'tom';
where name like 'to%';   #锚定开头
where name regexp 'wei$';

6.破解密码
先将这个修改成简单密码注释掉

image.png

root账户没了或者root密码丢失:
关闭Mysql使用下面方式进入Mysql直接修改表权限                       5.6/5.7版本:# mysqld --skip-grant-tables --user=mysql &    # mysql -urootmysql> UPDATE mysql.user SET authentication_string=password('QianFeng@123') WHERE user='root' AND host='localhsot';mysql> FLUSH PRIVILEGES;

7.多表查询
多表的连接查询:
1.交叉连接: 生成笛卡尔积,它不使用任何匹配条件

2.内连接: 只连接匹配的行

3.外连接之左连接: 会显示左边表内所有的值,不论在右边表内匹不匹配
外连接之右连接: 会显示右边表内所有的值,不论在左边表内匹不匹配

4.全外连接: 包含左、右两个表的全部行

建立两个测试表:
mysql> create table t2(-> id int auto_increment primary key, -> name varchar(30), -> age int,-> dep_id int
);
mysql> insert into t2(name,age,dep_id) values -> ('tom',19,200),-> ('jack',20,203),-> ('alice',18,200), -> ('robin',20,200);
mysql> create table t3(dep_id int,name varchar(30));
mysql> insert into t3 values(200 'hr'),(203 'it');                                 

1、交叉连接

mysql> select t2.name,t3.name from t2,t3 where t2.name='tom';
+------+------+
| name | name |
+------+------+
| tom  | hr   |
| tom  | it   |
+------+------+
交叉连接就是将两表的数据进行交叉组合,显示出组合(1和1,2进行组合,结果可以是1,1 | 1,2)mysql> select t2.name,t2.age,t2.dep_id,t3.name from t2,t3;
+-------+------+--------+------+
| name  | age  | dep_id | name |
+-------+------+--------+------+
| tom   |   19 |    200 | hr   |
| tom   |   19 |    200 | it   |
| jack  |   20 |    203 | hr   |
| jack  |   20 |    203 | it   |
| alice |   18 |    200 | hr   |
| alice |   18 |    200 | it   |
| robin |   20 |    200 | hr   |
| robin |   20 |    200 | it   |
+-------+------+--------+------+

2、内连接

连接条件:两表之间有一个共同的纽带,纽带名可以不一样#只显示匹配到的行
mysql> select t2.id,t2.name,t2.age,t3.name from t2,t3 where t2.dep_id=t3.dep_id;
mysql> select id,t2.name,age,t3.name from t2,t3 where t2.dep_id=t3.dep_id;
#两表字段名不同,可以不指定表名
+----+-------+------+------+
| id | name  | age  | name |
+----+-------+------+------+
|  1 | tom   |   19 | hr   |
|  2 | jack  |   20 | it   |
|  3 | alice |   18 | hr   |
|  4 | robin |   20 | hr   |
+----+-------+------+------+

3、外连接
外连接语法:
select 字段列表 from 表1 left|right join 表2 on 表1.字段 = 表2.字段;

左连接:
mysql> insert into t2(name,age,dep_id) values('awei',18,204);
mysql> insert into t3(name,dep_id) values('sale',206);
为了显示出效果,插入两条内连接无法匹配的数据
mysql> select id,t2.name,age,t3.name -> from t2 left join t3 on t2.dep_id=t3.dep_id; 

第五章 mysql表操作相关推荐

  1. mysql 多表操作实例_第五章 多表操作

    * 建库.建表.添加数据 CREATE DATABASE chapter06; USE chapter06; CREATE TABLE account( id INT primary key auto ...

  2. MySql 表操作指令小全与案例 2

    MySql 表增删查改小全 2 增删查改--进阶 键值约束与扩展属性 分组查询 ER关系图+三大范式 ER关系图 三大范式 多表联查 内连接 外连接 子查询 合并查询 注:本篇博客为 MySql 表操 ...

  3. 【MySQL】3.MySQL表操作

    文章目录 1.0 MySQL表操作详解 1.1 MySQL创建表 1.2 MySQL查看表结构 1.3 MySQL修改表 1.0 MySQL表操作详解 1.1 MySQL创建表 指令:create t ...

  4. (第十五集——第1章)MySQL库操作+表操作

    数据库管理软件的由来 基于我们之前所学,数据要想永久保存,都是保存于文件中,毫无疑问,一个文件仅仅只能存在于某一台机器上. 如果我们暂且忽略直接基于文件来存取数据的效率问题,并且假设程序所有的组件都运 ...

  5. MySQL必知必会——第十五章联结表

    联结表 本章将介绍什么是联结,为什么要使用联结,如何编写使用联结的SELECT语句. 联结 SQL最强大的功能之一就是能在数据检索查询的执行中联结(join)表. 在能够有效地使用联结前,必须了解关系 ...

  6. mysql 8.3_8.3 - mysql 表操作

    什么是存储引擎 mysql中建立的库===>文件夹 库中建立的表===>文件 存储引擎就是表的类型 现实生活中我们用来存储数据的文件有不同的类型,每种文件类型对应各自不同的处理机制:比如处 ...

  7. 【Mysql面试宝典】快速搞定Mysql表操作

    写在前面,大家好!我是[跨考菌],一枚跨界的程序猿,专注于后台技术的输出,目标成为全栈攻城狮!这博客是对我跨界过程的总结和思考.如果你也对Java.后端技术感兴趣,抑或是正在纠结于跨界,都可以关注我的 ...

  8. MYSQL——表操作

    一.表操作(表与字段密不可分,字段即表中的列) 1.新增数据表: --create table [if not exists]表名(字段名字 数据类型, 字段名字 数据类型, 最后一行不需要逗号)[表 ...

  9. 2.MySQL表操作

    引擎介绍 mysql中的存储引擎 表介绍 表就相当于文件,表中的一条记录就相当于文件的一行内容,不同的是,表中的一条记录有对应的标题,称为表的字段 还记得我们之前写过的'员工信息表作业'么?存储这员工 ...

最新文章

  1. 计算机科学界至今未解决的四大难题
  2. python列表推导式实现从1加到100_python之生成器和列表推导式
  3. SAP,IBM,AC实施之比较
  4. 【项目实战】vue+springboot项目使用富文本编辑器实现长文章发表和展示
  5. Angular html property的值如何被更新的
  6. 利用ECG关于HRV分析
  7. c语言程序装萝卜,萝卜花园练习win7系统安装SkyDrive的图文步骤
  8. php 微信支付 ca证书,微信公众号红包接口开发PHP开发 CA证书出错,请登陆微信支付商户平台下载证书...
  9. 中国的第一座基站,你知道在哪吗?
  10. 给大家推荐一本书啊啊~
  11. Atitit 命令行执行sql 跨语言 目录 1.1. 无需输入密码,那就不要-p参数即可 1 1.2. 4.使用mysql命令执行 1 1.3. 5.mysql命令执行sql,并将查询结果保存到
  12. linux修改vcf编码格式,VCF乱码终极解决大法
  13. mac VMware fusion配置nat网络
  14. 图谱实战 | 开源知识图谱融合工具剖析:Dedupe与OpenEA工具实现思想、关键环节与实操分析...
  15. 加速求解两个矩阵任意两行之间的pearson相关性
  16. Mac版 Pycharm 查看内建函数的源码
  17. win7音量图标不见了怎么办
  18. php 配置 error_reporting,关于php中的错误配置display_errors与error_reporting说明
  19. 填词作文:新的一年,新的梦想
  20. 西门子1200PLC程序SCL数控G代码功能块源文件 S7-1200PLC程序SCL数控G代码功能块源文件

热门文章

  1. 领峰:不同的交易模式要选择怎样的现货交易技巧
  2. 方舟自己的服务器无法运行,方舟适者生存闪退重启解决方法 怎么连接进入服务器?...
  3. Linux格式化磁盘-自动化脚本执行
  4. MessageBox、MessageBeep、Beep 和 messagedlg用法
  5. oracle列转行 listagg
  6. 泰凌微8258入门指导1-环境搭建
  7. 鸿蒙hap捷豹,鸿蒙手表真机展示Hello World!
  8. 【算法自由之路】前缀树 桶排序之计数排序和基数排序
  9. oracle 新建绑定变量,在Oracle中,绑定变量是什么?绑定变量有什么优缺点?
  10. 一文让你搞懂设计模式