iis服务器助手广告广告
返回顶部
首页 > 资讯 > 数据库 >MySQL 常用操作
  • 440
分享到

MySQL 常用操作

MySQL常用操作 2021-04-21 08:04:31 440人浏览 猪猪侠
摘要

1 创建/打开/删除数据库 create database db; create database db character set utf8mb4; use db; drop database db; alter databas

MySQL 常用操作

1 创建/打开/删除数据库

create database db;
create database db character set utf8mb4;
use db;
drop database db;
alter database db character set utf8mb4;

2 修复表

Mysqlcheck --all-databases
mysqlcheck --all-databases --fast

3 查询

select * from table1;
select * from table1,table2;
select field1,field2,... from table1,table2;

select ... from ... where condition;
select ... from ... where condition group by field;
select ... from ... where condition1 group by field having condition2;
select ... from ... where condition order by field;
select ... from ... where condition order by field1,field2 desc;
select ... from ... where condition limit 10;

select distinct field1,field2 from ...
select distinct table1.field1,table2.field2 from table1,table2 where condition1 group by field4 having condition2 order by field3 desc limit 20;

4 插入/删除/更新数据

# 插入
insert into table1(field1,field2,...) values(value1,value2,...);

# 删除
delete from table1;
truncate from table1;
delete from table1 where condition;
delete table1,table2 from table1,table2 where condition;

# 更新
update table1 set field=new_value where condition;
update table1,table2 set field1=new_value1,field2=new_value2,... where table1.id=table2.id and condition;

5 创建/删除/修改表

# 创建
create table table1(field1 type,field2 type,...);
create table table1(field type,...,index(field));
create table table1(field type,...,primary key(field));
create table table1(field1 type,field2 type,...,primary key(field1,field2));
create table table1(field type,...,foreign key(field) references table2(field));
create table table1(field1 type,field2 type,...foreign key(field1,field2) references table2(field1,field2));
create table if not exists table1(field type,...);
create temporary table table1(field type,...);

# 删除
drop table table1;
drop table if exists table1;
drop table table1,table2;

# 修改
alter table table1 modify old_name new_type;
alter table table1 modify old_name new_type not null;
alter table table1 change old_name new_name new_type;
alter table table1 change old_name new_name new_type not null;
alter table table1 alter field set default ...;
alter table table1 alter field drop default;
alter table table1 add new_name new_type;
alter table table1 add new_name new_type first;
alter table table1 add new_name new_type after field;
alter table table1 drop field;
alter table table1 add index(field);

# 改变字段顺序
alter table table1 modify field type first;
alter table table1 modify field1 type after field2;
alter table table1 change old_name new_name new_type first;
alter table table1 change old_name new_name new_type after field;

6 重置root密码

# 停止服务,各个机器具体不一样,可以使用图形界面停止
systemctl stop mysqld
mysqld_safe --skip-grant-tables
# 若失败可以尝试
# mysqld_sage --shared-memory --skip-grant-tables

# 另一个终端
mysql
# 进入之后
# MySQL 8+
flush privileges;
alter user "root"@"localhost" identified by "new_pass";
# MySQL 旧版
update mysql.user set passWord=password("new_password") where user="root";

7 备份与恢复

# 备份
mysqldump -u username -p dbname > backup.sql

# 恢复
mysql -u username -p dbname < backup.sql

8 浏览

show databases;
show tables;
show fields from table1;
describe table1;
show create table table1;
show processlist;

9 连接查询

select ... from t1 join t2 on t1.id = t2.id where condition;
select ... from t1 left/right join t2 on t1.id = t2.id where condition;
select ... from t1 join (t2 join t3 on ...) on ...;

10 条件

field = value
field <> value
field like "value%"
filed is null
field is not null
field in (value1,value2,...)
field not in (value1,value2,...)
condition1 and conditoin2
condition1 or condition2

11 用户与权限

grant all privileges on database.table to "user"@"localhost" identified by "password";
grant select,insert,delete on database.* to "user"@"xxx.xxx.xxx.xxx" identified by "password";

revoke select on database.table from "user"@"host";
revoke all privileges,grant option from "user"@"host";

alter user "user"@"host" identified with mysql_native_password by "new_password";

drop user "user"@"host";
您可能感兴趣的文档:

--结束END--

本文标题: MySQL 常用操作

本文链接: https://www.lsjlt.com/news/7002.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • mysql数据库的常用操作
    这篇文章主要讲解了“mysql数据库的常用操作”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“mysql数据库的常用操作”吧!mysql的常用操作(添加用户)...
    99+
    2024-04-02
  • mysql——cmd进入mysql及常用的mysql操作
    在命令行打开mysql的方法:首先在cmd命令行中输入“net start mysql”;然后输入“mysql -hlocalhost -uroot -p”,回车;最后输入mysql的账号密码,回车即可打开mysql数据库。 在cmd命令行...
    99+
    2023-09-09
    mysql 数据库 服务器
  • MySQL常用的命令操作大全
    这篇文章主要介绍“MySQL常用的命令操作大全”,在日常操作中,相信很多人在MySQL常用的命令操作大全问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”MySQL常用的命令操作...
    99+
    2024-04-02
  • MySql常用表操作命令总结
    本篇内容介绍了“MySql常用表操作命令总结”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成! 1:使用SH...
    99+
    2024-04-02
  • MySQL常用的操作命令整理
    本篇内容主要讲解“MySQL常用的操作命令整理”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MySQL常用的操作命令整理”吧! 1、启动MySQL服务...
    99+
    2024-04-02
  • scala常用操作
    scala常用操作 版本信息python3.7pyspark2.4.0 from pyspark import SQLContext,SparkContext,SparkConf conf = SparkConf() sc = Spar...
    99+
    2023-01-31
    常用 操作 scala
  • MongoDB常用操作
    关系型数据库名词与MongoDB对比:关系数据库 MongoDB Database  DatabaseTable CollectionRow Do...
    99+
    2024-04-02
  • MySQL常用基本操作都有哪些
    MySQL常用基本操作都有哪些,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。 MySQL常用操作基本操作,以下都是MySQL5.0下...
    99+
    2024-04-02
  • 在linux下MySQL的常用操作命令
    一、启动与退出 1、启动 MySQL 服务 net start mysql   或   service mysql start 2、关闭 MySQL 服务 net stop mysql    或   service mysql stop 3...
    99+
    2023-09-03
    linux mysql
  • MySQL中常用指令操作有哪些
    小编给大家分享一下MySQL中常用指令操作有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!l  创建数据库:CRE...
    99+
    2024-04-02
  • Mysql数据库常用命令操作大全
    mysql连接: [root@host]# `mysql -u root -p` Enter password:****** 创建数据库:CREATE DATABASE 数据库名;删除数据库:drop da...
    99+
    2023-03-20
    mysql常用命令 mysql命令
  • Linux系统中MySQL的常用操作命令
    服务: # chkconfig --list        列出所有系统服务 # chkconfig --list | grep on...
    99+
    2024-04-02
  • MySQL数据库的常用操作和技巧
    本篇内容主要讲解“MySQL数据库的常用操作和技巧”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“MySQL数据库的常用操作和技巧”吧!  使用MySQL数据库,...
    99+
    2024-04-02
  • python MySQLdb 常用操作
    我采用的是MySQLdb操作的MYSQL数据库。先来一个简单的例子吧:import MySQLdb try:     conn=MySQLdb.connect(host='localhost',user='root',passwd='roo...
    99+
    2023-01-31
    常用 操作 python
  • python ftp常用操作
    需求快速进行ftp上传 ,下载,查询文件原来直接在shell下操作:需要【连接,输用户名,输密码,单文件操作,存在超时限制】太过于繁琐,容易操作失败改进一句命令,搞定多文件上传,下载,查询,列表等操作后期可以加入更强大的功能源代码#!/us...
    99+
    2023-01-31
    常用 操作 python
  • 3Mysql 的常用操作
    root@OBird ~]# mysql -uroot -pzaq12wsx   #入库mysql> show databases;  #查库ERROR 2006 (HY000): MySQL server has gone away...
    99+
    2023-01-31
    常用 操作 Mysql
  • Navicat操作数据库与Mysql常见命令操作实战
    一:Navicat下载与安装 官网下载链接:Navicat 下载完后直接安装即可 二:数据库的连接 1.打开Navicat软件,点击左上角连接按钮,选择mysql数据库 输入完成后双击连接名,连接成...
    99+
    2023-09-24
    数据库 mysql
  • Linux操作系统&&Linux20+常用入门操作
    目录 1. 前世今生 1.1. Linux发展史 1.2. 开源 1.3. 发行版本 2. OS/操作系统  3. 指令操作  00 man 01 adduser 02 passwd 03 userdel NAME 04 su - NAME...
    99+
    2023-09-01
    linux unix 服务器
  • MySQL数据库常用操作的示例分析
    小编给大家分享一下MySQL数据库常用操作的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!具体如下:一、查询不同表中同名...
    99+
    2024-04-02
  • MySQL 数据库常用操作语句的总结
    创建数据库: CREATE DATABASE database_name; 删除数据库: DROP DATABASE database_name; 选择数据库: USE database_na...
    99+
    2023-09-11
    数据库 mysql
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作