MySQL的常用命令及应用示例

MySQL是一个开源关系型数据库管理系统,由瑞典MySQL AB公司开发,目前归属于Oracle公司。MySQL被广泛应用在Web应用程序、商业数据仓库、内部管理系统等方面。MySQL提供了许多管理工具,使得管理员可以方便地管理数据库,包括创建数据库、表、视图、存储过程、函数等。MySQL也支持SQL语言,可以使用SQL语句来进行数据的查询、更新、删除等操作。接下来,我们主要介绍一些MySQL的常用命令。

MySQL的常用命令包括:

1、连接数据库:

mysql -u username -p

2、选择数据库:

use database_name

3、显示数据库列表:

show databases

4、创建数据库:

create database database_name

5、删除数据库:

drop database database_name

6、显示表列表:

show tables

7、创建表:

create table table_name (column_name1 data_type, column_name2 data_type, ...)

8、插入数据:

insert into table_name (column1, column2, ...) values (value1, value2, ...)

9、更新数据:

update table_name set column_name = new_value where some_column = some_value

10、删除数据:

delete from table_name where some_column = some_value

11、查询数据:

select column1, column2, ... from table_name where some_column = some_value

综上,就是一些常用的MySQL命令。

以下是上述命令的应用示例:

  1. 连接数据库:
mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2021, 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.

mysql>
  1. 选择数据库:
mysql> use test;
Database changed
  1. 显示数据库列表:
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)
  1. 创建数据库:
mysql> create database new_database;
Query OK, 1 row affected (0.00 sec)
  1. 删除数据库:
mysql> drop database new_database;
Query OK, 0 rows affected (0.01 sec)
  1. 显示表列表:
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| employees       |
+----------------+
1 row in set (0.00 sec)
  1. 创建表:
mysql> create table employees (
    -> id int primary key auto_increment,
    -> name varchar(50),
    -> age int,
    -> department varchar(50)
    -> );
Query OK, 0 rows affected (0.01 sec)
  1. 插入数据:
mysql> insert into employees (name, age, department) values 
    -> ('John Doe', 30, 'Sales'),
    -> ('Jane Doe', 28, 'Marketing'),
    -> ('Jim Smith', 35, 'IT');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
  1. 更新数据:
mysql> update employees set age = 32 where name = 'John Doe';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
  1. 删除数据:
mysql> delete from employees where name = 'Jim Smith';
Query OK, 1 row affected (0.00 sec)
  1. 查询数据:
mysql> select * from employees;
+----+-------+-----+------------+
| id | name  | age | department |
+----+-------+-----+------------+
|  1 | John Doe  |  32 | Sales     |
|  2 | Jane Doe  |  28 | Marketing |
+----+-------+-----+------------+
2 rows in set (0.00 sec)
  1. 退出数据库:
mysql> exit
Bye

作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/mysql/mysql-commands-with-examples/

(0)
牛奇网牛奇网
上一篇 2023年2月4日 上午9:25
下一篇 2023年2月4日 上午10:55

相关推荐

发表回复

登录后才能评论