mysql常用语句(补充中)
程序员文章站
2022-05-12 17:51:50
...
1. 登录数据库
本地:
mysql -u root -p
远程:
mysql -h[远程公网IP地址] -u [mysql用户名] -p [mysql密码];
2. 备份
2.1 备份mysql数据库
mysqldump -h[远程公网IP地址] -u[mysql用户名] -p[mysql密码] [数据库名] > [数据库文件名.sql];
2.2 备份mysql数据库,带删除表,即恢复时不需要手动删除原数据库。
mysqldump --add-drop-table -h[远程公网IP地址] -u[mysql用户名] -p[mysql密码] [数据库名] > [数据库文件名.sql];
2.3 同时备份mysql数据库某些表
mysqldump -h[远程公网IP地址] -u[mysql用户名] -p[mysql密码] [数据库名1] [数据表1] [数据表2] [数据表3] > [数据库文件名.sql];
3. 恢复数据库
4. 常用语句
4.1 删除字段
格式:ALTER TABLE 表名 DROP 属性名;
mysql> desc test;
+----------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------------+------+-----+---------+----------------+
| create_date | datetime | YES | | NULL | |
| modified_date | datetime | YES | | NULL | |
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| user_id | bigint(20) unsigned | NO | | NULL | |
| sync_time | varchar(16) | YES | | NULL | |
| status | varchar(16) | NO | | NULL | |
| first_name | varchar(64) | YES | | NULL | |
| last_name | varchar(64) | YES | | NULL | |
| shop_id | bigint(20) unsigned | YES | | NULL | |
+----------------+---------------------+------+-----+---------+----------------+
mysql> alter table test drop shop_id;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc test;
+----------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------------+------+-----+---------+----------------+
| create_date | datetime | YES | | NULL | |
| modified_date | datetime | YES | | NULL | |
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| user_id | bigint(20) unsigned | NO | | NULL | |
| sync_time | varchar(16) | YES | | NULL | |
| status | varchar(16) | NO | | NULL | |
| first_name | varchar(64) | YES | | NULL | |
| last_name | varchar(64) | YES | | NULL | |
+----------------+---------------------+------+-----+---------+----------------+
4.2 增加表字段
格式:ALTER TABLE 表名 ADD 属性名;
4.3 获取表结构
格式:
- describe 表名;
- desc 表名
- show columns from 表名;
4.4
5.其他语句
5.1 mysql启动和关闭外键约束的方法
禁用外键约束:
SET FOREIGN_KEY_CHECKS=0;
打开外键约束:
SET FOREIGN_KEY_CHECKS=1;
查看外键约束:
SELECT @@FOREIGN_KEY_CHECKS;
5.2 查看所有table的相关信息
SHOW TABLE STATUS;
结果如下:
mysql> show table status where name="user";
+------+--------+---------+------------+-------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+------+--------+---------+------------+-------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
| user | InnoDB | 10 | Compact | 11494 | 229 | 2637824 | 0 | 737280 | 9437184 | 11986 | 2017-04-24 10:48:18 | NULL | NULL | utf8_unicode_ci | NULL | | |
+------+--------+---------+------------+-------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-----------------+----------+----------------+---------+
1 row in set (0.01 sec)
Name(table名)
Auto_increment(当前自增id值)
Create_time(创建时间)
其中,更改Auto_increment可以使用下面的语句
alter table user AUTO_INCREMENT=12345;
将user的自增id值设置为12345