用户登录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 59
Server version: 5.7.20 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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
2
mysql> create database Leetcode;
Query OK, 1 row affected (0.01 sec)

选择

1
2
mysql> use Leetcode;
Database changed

删除

1
mysql> drop database Leetcode;

表处理

创建

选择数据库后,输入建表语句:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> create table if not exists Question (
-> p_id INT NOT NULL,
-> frontend_id INT NOT NULL,
-> title_slug TINYTEXT,
-> title TINYTEXT,
-> difficulty INT,
-> status INT,
-> paid_only INT,
-> is_favor INT,
-> frequency INT,
-> progress INT,
-> likes INT,
-> dislikes INT,
-> PRIMARY KEY (p_id))
-> ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.03 sec)

ENGINE 设置存储引擎,CHARSET 设置编码。

查看表结构:

1
2
3
4
5
6
7
mysql> show tables;
+--------------------+
| Tables_in_leetcode |
+--------------------+
| Question |
+--------------------+
1 row in set (0.00 sec)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mysql> desc Question;
+-------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| p_id | int(11) | NO | PRI | NULL | |
| frontend_id | int(11) | NO | | NULL | |
| title_slug | tinytext | YES | | NULL | |
| title | tinytext | YES | | NULL | |
| difficulty | int(11) | YES | | NULL | |
| status | int(11) | YES | | NULL | |
| paid_only | int(11) | YES | | NULL | |
| is_favor | int(11) | YES | | NULL | |
| frequency | int(11) | YES | | NULL | |
| progress | int(11) | YES | | NULL | |
| likes | int(11) | YES | | NULL | |
| dislikes | int(11) | YES | | NULL | |
+-------------+----------+------+-----+---------+-------+
12 rows in set (0.01 sec)

修改表名

将表明orders改为Orders:

1
2
mysql> rename table orders to Orders;
Query OK, 0 rows affected (0.03 sec)

删除表

删除Orders表:

1
mysql> drop table Orders;

字段处理

修改类型

1
mysql> alter table Question modify column p_id int;

增加

1
mysql> alter table Question add brief_introduce text not Null;

删除

[]内内容可重复多条。

1
mysql> alter table Question drop column likes[, drop column dislikes];