参考博客:
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.33-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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>
mysql> CREATE DATABASE threed; # 创建数据库
Query OK, 1 row affected (0.00 sec)
mysql> use threed; #使用数据库
Database changed
mysql> CREATE TABLE `user` ( # 创建表
-> `id` int(32) NOT NULL AUTO_INCREMENT,
-> `userName` varchar(32) NOT NULL,
-> `passWord` varchar(50) NOT NULL,
-> `realName` varchar(32) DEFAULT NULL,
-> PRIMARY KEY (`id`)
-> ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.21 sec)
mysql> show databases; # 查看所有数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| threed |
+--------------------+
5 rows in set (0.00 sec)
mysql> show tables; # 查看数据库中的所有表
+------------------+
| Tables_in_threed |
+------------------+
| user |
+------------------+
1 row in set (0.00 sec)
mysql> describe user; # 查看表结构
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(32) | NO | PRI | NULL | auto_increment |
| userName | varchar(32) | NO | | NULL | |
| passWord | varchar(50) | NO | | NULL | |
| realName | varchar(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> insert into `user` (userName,passWord,realName) values ('wang','123456','manager'); # 插入
Query OK, 1 row affected (0.04 sec)
mysql> insert into `user` (userName,passWord,realName) values ('lisi','567890','system manager');
Query OK, 1 row affected (0.03 sec)
mysql> describe user;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(32) | NO | PRI | NULL | auto_increment |
| userName | varchar(32) | NO | | NULL | |
| passWord | varchar(50) | NO | | NULL | |
| realName | varchar(32) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> select * from user; # 查询
+----+----------+----------+----------------+
| id | userName | passWord | realName |
+----+----------+----------+----------------+
| 3 | wang | 123456 | manager |
| 4 | lisi | 567890 | system manager |
+----+----------+----------+----------------+
2 rows in set (0.00 sec)
mysql> update `user` set id=1 where userName='wang'; # 修改
Query OK, 1 row affected (0.04 sec)
mysql> update `user` set id=2 where userName='lisi';
Query OK, 1 row affected (0.03 sec)
mysql> select * from user;
+----+----------+----------+----------------+
| id | userName | passWord | realName |
+----+----------+----------+----------------+
| 1 | wang | 123456 | manager |
| 2 | lisi | 567890 | system manager |
+----+----------+----------+----------------+
2 rows in set (0.00 sec)
mysql> exit
Bye