MySQL数据库表操作

1.添加新字段

1.1.语法:alter table 表名 add 字段 类型;

mysql> create table t3(subject int);
#添加math字段
mysql> alter table t3  add math int(10);  
#添加多个字段,中间用逗号隔开
mysql> alter table t3  add (chinese int(10),english int(10));

1.2.把添加的字段放到english后面
语法alter table 表名 add 添加的字段(和类型) after 字段;

mysql> alter table t3  add physic int(10) after english;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t4;
+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| subject | int(11) | YES  |     | NULL    |       |
| math    | int(10) | YES  |     | NULL    |       |
| english | int(10) | YES  |     | NULL    |       |
| physic  | int(10) | YES  |     | NULL    |       |
| chinese | int(10) | YES  |     | NULL    |       |
+---------+---------+------+-----+---------+-------+

1.3.把添加的字段放在第一个
语法alter table 表名 add 添加的字段(和类型) first;

mysql> alter table t3  add history int(10) first;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t4;
+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| history | int(10) | YES  |     | NULL    |       |
| subject | int(11) | YES  |     | NULL    |       |
| math    | int(10) | YES  |     | NULL    |       |
| english | int(10) | YES  |     | NULL    |       |
| physic  | int(10) | YES  |     | NULL    |       |
| chinese | int(10) | YES  |     | NULL    |       |
+---------+---------+------+-----+---------+-------+
6 rows in set (0.01 sec)

2.修改字段和类型

2.1.change修改字段名称,类型,约束,顺序

语法alter table 表名 change 旧字段 新字段 类型 after 字段名;

#修改字段名称与约束并更换了位置
mysql> alter table t4 change subject id int(15) after chinese;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
​
mysql> desc t4;
+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| history | int(10) | YES  |     | NULL    |       |
| math    | int(10) | YES  |     | NULL    |       |
| english | int(10) | YES  |     | NULL    |       |
| physic  | int(10) | YES  |     | NULL    |       |
| chinese | int(10) | YES  |     | NULL    |       |
| id      | int(15) | YES  |     | NULL    |       |
+---------+---------+------+-----+---------+-------+
6 rows in set (0.00 sec)

2.2.modify修改字段类型,约束,顺序

语法alter table 表名 modify 字段 类型;

modify 和 change 的区别是:

  • modify 只修改字段的类型,不修改字段名
  • change 可以修改字段的类型和列名
#修改字段类型并更换位置
mysql> alter table t3 modify history int(20) after math;    
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
​
mysql> desc t4;
+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| math    | int(10) | YES  |     | NULL    |       |
| history | int(20) | YES  |     | NULL    |       |
| english | int(10) | YES  |     | NULL    |       |
| physic  | int(10) | YES  |     | NULL    |       |
| chinese | int(10) | YES  |     | NULL    |       |
| ids     | int(15) | YES  |     | NULL    |       |
+---------+---------+------+-----+---------+-------+
6 rows in set (0.00 sec)

​2.3.删除字段

语法alter table 表名 drop 字段;

#drop 丢弃的字段
mysql> alter table  t4 drop ids;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
​
mysql> desc t4;
+---------+---------+------+-----+---------+-------+
| Field   | Type    | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| math    | int(10) | YES  |     | NULL    |       |
| history | int(20) | YES  |     | NULL    |       |
| english | int(10) | YES  |     | NULL    |       |
| physic  | int(10) | YES  |     | NULL    |       |
| chinese | int(10) | YES  |     | NULL    |       |
+---------+---------+------+-----+---------+-------+
5 rows in set (0.00 sec)

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

3.1.添加一条记录

语法insert into 表名(字段1,字段2,字段3,字段4) values(''值1'',''值2'',''值3'',''值4'');
字符串必须引号引起来,单引或者双引都可以

mysql> create table t4(id int, name varchar(20), sex enum('m','f'), age int);
mysql> insert into t4 values(1,"tom","m",18);
Query OK, 1 row affected (0.00 sec)
#注:添加的记录与表头要对应

3.2.添加多条记录

mysql> insert into t4(id,name,sex,age) values(2,"jack","m",19),(3,"xiaoli","f",20);
Query OK, 2 rows affected (0.34 sec)

​3.3.用set添加记录

mysql> insert into t4 set id=4,name="zhangsan",sex="m",age=21;
Query OK, 1 row affected (0.00 sec)

3.4.更新记录

语法update 表名 set 修改的字段 where 条件;

mysql> update t4 set id=6 where name="xiaoli";

3.5.删除记录

#删除单条记录
mysql> delete from t4 where id=6;
Query OK, 1 row affected (0.35 sec)

#删除所有记录
mysql> delete from t4;

4.查询

案例准备:
#创建一个库
mysql> create database company;#创建一个测试表
​mysql> \e​
     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),
     age int,
     office int,
     dep_id int
     )
-> ;
Query OK, 0 rows affected (0.01 sec)
#插入数据:
mysql> \e
insert into company.employee5(name,sex,hire_date,post,job_description,age,office,dep_id) values 
    ('jack','male','20180202','instructor','teach',23,501,100),
    ('harry','male','20180202','hr',NULL,26,502,101),
    ('lili','female','20180206','sale','salecc',27,503,102),
    ('xiaoguo','male','20180205','sale','',35,503,102)
    -> ;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0    
mysql> use company

语法select 字段名称,字段名称2 from 表名 条件

4.1.​简单查询

mysql> select * from employee5;

4.2.​多字段查询

mysql> select id,name,sex from employee5;

4.3.有条件查询:where

mysql> select id,name from employee5 where id<=3;
mysql> select id,name from employee5 where name='xiaoguo';

​4.4.统计记录数量:count()

mysql> select count(*) from employee5;#统计字段得到数量:
mysql> select count(id) from employee5;

4.5.多条件查询: and 和

语法select 字段1,字段2 from 表名 where 条件1 and 条件2;

mysql> select name,sex from employee5 where post='hr' AND office=502;

4.6.​多条件查询: or 或者

语法select 字段1,字段2 from 表名 where 条件1 or 条件2;

mysql> select id,name from employee5 where post='instructor' and job_description='teach' or  office=503;
+----+---------+
| id | name    |
+----+---------+
|  1 | jack    |
|  3 | lili    |
|  4 | xiaoguo |
+----+---------+
3 rows in set (0.00 sec)

4.7.关键字 IS NULL 空的

mysql> select name,post from employee5 where job_descriptio
n is null;
+-------+------+
| name  | post |
+-------+------+
| harry | hr   |
+-------+------+
1 row in set (0.00 sec)
​
mysql> select name,job_description from employee5 where job_description IS NOT NULL;  #取反 不是null
+---------+-----------------+
| name    | job_description |
+---------+-----------------+
| jack    | teach           |
| lili    | salecc          |
| xiaoguo |                 |
+---------+-----------------+
3 rows in set (0.00 sec)
​
mysql> select name,job_description from employee5 where job_description=''; #什么都没有==空
+---------+-----------------+
| name    | job_description |
+---------+-----------------+
| xiaoguo |                 |
+---------+-----------------+
1 row in set (0.00 sec)

NULL说明:
1、等价于没有任何值、是未知数。
2、NULL与0、空字符串、空格都不同,NULL没有分配存储空间。
3、对空值做加、减、乘、除等运算操作,结果仍为空。
4、比较时使用关键字用“is null”和“is not null”。
5、排序时比其他数据都小(索引默认是升序排列,小→大),所以NULL值总是排在最前。

4.8.limit 限制

mysql> select * from employee5 limit 2;  #只显示前2行
+----+-------+------+------------+------------+-----------------+--------+--------+
| id | name  | sex  | hire_date  | post       | job_description | office | dep_id |
+----+-------+------+------------+------------+-----------------+--------+--------+
|  3 | jack  | male | 2018-02-02 | instructor | teach           |    501 |    100 |
|  4 | harry | male | 2018-02-02 | hr         | NULL            |    502 |    101 |
+----+-------+------+------------+------------+-----------------+--------+--------+
2 rows in set (0.00 sec)
​
mysql> select * from employee5 limit 2\G;
*************************** 1. row ***************************
             id: 3
           name: jack
            sex: male
      hire_date: 2018-02-02
           post: instructor
job_description: teach
         office: 501
         dep_id: 100
*************************** 2. row ***************************
             id: 4
           name: harry
            sex: male
      hire_date: 2018-02-02
           post: hr
job_description: NULL
         office: 502
         dep_id: 101
2 rows in set (0.00 sec)

4.9.分组查询 group by

mysql> select count(name),post from employee5 group by post;
+-------------+------------+
| count(name) | post       |
+-------------+------------+
|           1 | hr         |
|           1 | instructor |
|           2 | sale       |
+-------------+------------+
#count可以计算字段里面有多少条记录,这个案例就是:以岗位为组,计算每个岗位下有多少名员工

4.10.排序查询 order by

#默认从小到大排序。
mysql> select name,office from employee5 order by id;
#降序,从大到小
mysql> select name,office from employee5 order by id desc;

4.11.关键字 between and

mysql> select name,sex from employee5 where age BETWEEN 20 AND 25;
+------+------+
| name | sex  |
+------+------+
| jack | male |
+------+------+
1 row in set (0.00 sec
​
mysql> select name,sex from employee5 where age BETWEEN 30 AND 35;
+---------+------+
| name    | sex  |
+---------+------+
| xiaoguo | male |
+---------+------+
1 row in set (0.00 sec)

4.12.表复制

key不会被复制,即:主键、外键和索引不会复制
a.复制表结构+记录 (key不会复制,也就是主键、外键和索引)
语法create table 新表 select * from 旧表;

mysql> use company;
mysql> create table new_t1 select * from employee5;
mysql> show tables;
mysql> select * from new_t1;
b.复制单个字段和记录:
mysql> create table new_t2(select id,name from employee5);
mysql> show tables;
mysql> select * from new_t2;

5.重置root账户密码

修改配置文件vim /etc/my.cnf在[mysqld]下添加
skip-grant-tables

跳过授权表,就会跳过密码验证
重启Mysql使用下面方式进入Mysql直接修改表权限
5.6/5.7版本:
# mysql -uroot

mysql> use mysql;
mysql> update mysql.user set authentication_string=password('Jianglt@123') where user='root';
mysql> flush privileges;

修改完root密码后,需要编辑配置文件将 skip-grant-tables 参数前加#号注释或删除skip-grant-tables此行
重启mysql

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TA548464

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值