MySQL 基本使用教程

这里介绍下 基本的MySQL 使用教程

所有操作 都是在mac终端sql 环境下操作


  • 进入 MySQL mysql -u root -p

        // 进入MySQl 命令
        // mysql -u 数据库用户名  -p 密码(明码)  
        // 也可以不写密码, 回车后 再输入密码 可以让密码不回显
        yulilideMacBook-Pro:~ sky-fish$ mysql -u root -p
        // 输入 MySQL 密码
        Enter password: 
        Welcome to the MySQL monitor.  Commands end with ; or \g.
        Your MySQL connection id is 5
        Server version: 5.7.18 Homebrew
    
        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.
    
        // \h 获取帮助信息
        // \c 清除输入信息
        Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
  • 显示数据库列表

    mysql> show databases;
            +--------------------+
            | Database           |
            +--------------------+
            | information_schema |
            | mysql              |
            | performance_schema |
            | sys                |
            +--------------------+
            4 rows in set (0.01 sec)
    
  • 使用指定数据库 mysql>use mysql;(指定mysql库)

        mysql> use mysql;
        Reading table information for completion of table and column names
        You can turn off this feature to get a quicker startup with -A

        Database changed
    ```
- 显示库中的数据表 `mysql>show tables;`
    

    ```
        mysql> show tables
            -> ;
        +---------------------------+
        | Tables_in_mysql           |
        +---------------------------+
        | columns_priv              |
        | db                        |
        | engine_cost               |
        | event                     |
        | func                      |
        | general_log               |
        | gtid_executed             |
        | help_category             |
        | help_keyword              |
        | help_relation             |
        | help_topic                |
        | innodb_index_stats        |
        | innodb_table_stats        |
        | ndb_binlog_index          |
        | plugin                    |
        | proc                      |
        | procs_priv                |
        | proxies_priv              |
        | server_cost               |
        | servers                   |
        | slave_master_info         |
        | slave_relay_log_info      |
        | slave_worker_info         |
        | slow_log                  |
        | tables_priv               |
        | time_zone                 |
        | time_zone_leap_second     |
        | time_zone_name            |
        | time_zone_transition      |
        | time_zone_transition_type |
        | user                      |
        +---------------------------+
        31 rows in set (0.01 sec)
    ```
- 显示数据表结构  `mysql>describe yourtablename; `
    ```
        mysql> describe INNODB_SYS_TABLESTATS;
        +-------------------+---------------------+------+-----+---------+-------+
        | Field             | Type                | Null | Key | Default | Extra |
        +-------------------+---------------------+------+-----+---------+-------+
        | TABLE_ID          | bigint(21) unsigned | NO   |     | 0       |       |
        | NAME              | varchar(193)        | NO   |     |         |       |
        | STATS_INITIALIZED | varchar(193)        | NO   |     |         |       |
        | NUM_ROWS          | bigint(21) unsigned | NO   |     | 0       |       |
        | CLUST_INDEX_SIZE  | bigint(21) unsigned | NO   |     | 0       |       |
        | OTHER_INDEX_SIZE  | bigint(21) unsigned | NO   |     | 0       |       |
        | MODIFIED_COUNTER  | bigint(21) unsigned | NO   |     | 0       |       |
        | AUTOINC           | bigint(21) unsigned | NO   |     | 0       |       |
        | REF_COUNT         | int(11)             | NO   |     | 0       |       |
        +-------------------+---------------------+------+-----+---------+-------+
        9 rows in set (0.01 sec)
    ```
-  建库 `mysql>create database yourdbname;`
    ```
    mysql> create database binbinDatabases;
    Query OK, 1 row affected (0.00 sec)
    ```
- 建表 `mysql>create table yourtablename (columnname colunmtype, ...);`
    ```
    mysql> create table binbinFirstTable(Sno CHAR(9) PRIMARY KEY,
    -> Sname CHAR(20) UNIQUE,
    -> Ssex CHAR(2),
    -> Sage SMALLINT,
    -> Sdept CHAR(20));
    // 创建成功
    Query OK, 0 rows affected (0.01 sec)
    // 查看有没有我们创建的表 `binbinFirstTable`
    mysql> show tables
    -> ;
    +--------------------+
    | Tables_in_binbindb |
    +--------------------+
    | binbinFirstTable   |
    +--------------------+
    1 row in set (0.00 sec)

    ```
-  表中插入数据 `INSERT INTO yourtablename(属性型1, 属性型2, 属性型3) VALUES(属性值1, 属性值2, 属性值3) `
    ```
    // 注意: 这里的 `李华`, `Physics`
    mysql> INSERT INTO binbinFirstTable(Sno,Sname,Ssex,Sage,Sdept) VALUES(100,'李华',1,28,'Physics');
    Query OK, 1 row affected (0.01 sec)

    // 查看刚刚插入的表
    mysql> select *from binbinFirstTable;
    +-----+--------+------+------+---------+
    | Sno | Sname  | Ssex | Sage | Sdept   |
    +-----+--------+------+------+---------+
    | 100 | 李华   | 1    |   28 | Physics |
    +-----+--------+------+------+---------+

    //再次插入, 这里可以省略表属性型, 不过要按照建表的属性顺序输入值
    mysql> INSERT INTO binbinFirstTable VALUES(101,'韩梅梅',1,27,'Art');
           Query OK, 1 row affected (0.00 sec)
    //查看表内容
    mysql> select * from binbinFirstTable;  
    +-----+-----------+------+------+---------+
    | Sno | Sname     | Ssex | Sage | Sdept   |
    +-----+-----------+------+------+---------+
    | 100 | 李华      | 1    |   28 | Physics |
    | 101 | 韩梅梅    | 1    |   27 | Art     |
    +-----+-----------+------+------+---------+
    2 rows in set (0.00 sec)

    ```
----
    
// 创建一个课程 Course 表
mysql> CREATE TABLE Course(
-> Cno CHAR(4) PRIMARY KEY,
-> Cname CHAR(40),
-> Cpno CHAR(4),
-> Ceredit SMALLINT
-> );
// 创建成功
Query OK, 0 rows affected (0.01 sec)
// 查看数据库中的表 发现里面有 课程表 `Course` 但是没有学生表 
mysql> SHOW TABLES;
+--------------------+
| Tables_in_binbindb |
+--------------------+
| Course             |
| binbinFirstTable   |
+--------------------+
2 rows in set (0.00 sec)

- 改表名 `rename table table1 to table2`

mysql> rename table binbinFirstTable to Student;
Query OK, 0 rows affected (0.01 sec)
// 可以看到 表 binbinFirstTable 已经改名为 Student 了
mysql> show tables;
+--------------------+
| Tables_in_binbindb |
+--------------------+
| Course |
| Student |
+--------------------+
2 rows in set (0.00 sec)

- 添加列 `添加列:alter table 表名 add column 列名 varchar(30);`, column 可以省略
// 添加了S_score(分数)列
mysql> alter table Student add S_score SMALLINT;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0
// 查看添加结果
mysql> select * from Student;
+-----+-----------+------+------+---------+------------+---------+
| Sno | Sname     | Ssex | Sage | Sdept   | S_entrance | S_score |
+-----+-----------+------+------+---------+------------+---------+
| 100 | 李华      | 1    |   28 | Physics | NULL       |    NULL |
| 101 | 韩梅梅    | 1    |   27 | Art     | NULL       |    NULL |
+-----+-----------+------+------+---------+------------+---------+

2 rows in set (0.00 sec)


- 修改列 数据类型 `alter table 表名 modify 属性名 数据类型;`

// 可以看到 Sage 的数据类型是 samllint(6), 我们要改为 char(4) 
mysql> DESC Student;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Sno        | char(9)     | NO   | PRI | NULL    |       |
| Sname      | char(20)    | YES  | UNI | NULL    |       |
| Ssex       | char(2)     | YES  |     | NULL    |       |
| Sage       | smallint(6) | YES  |     | NULL    |       |
| Sdept      | char(20)    | YES  |     | NULL    |       |
| S_entrance | date        | YES  |     | NULL    |       |
| S_score    | smallint(6) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+

// 把 Sage 的数据类型改为 char(4)
mysql> alter table Student modify Sage varchar(4);
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

// 查看更改结果, 我们成功把 Sage 数据类型由 smallint(6) 改为了 char(4)
mysql> desc Student;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Sno        | char(9)     | NO   | PRI | NULL    |       |
| Sname      | char(20)    | YES  | UNI | NULL    |       |
| Ssex       | char(2)     | YES  |     | NULL    |       |
| Sage       | varchar(4)  | YES  |     | NULL    |       |
| Sdept      | char(20)    | YES  |     | NULL    |       |
| S_entrance | date        | YES  |     | NULL    |       |
| S_score    | smallint(6) | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

- 添加取唯一值约束 `alter table 表名 add UNIQUE(属性);`
// 我们给 表 Course 的 Cname (课程名) 添加唯一值约束

mysql> desc Course;
// 可以看到, 现在 Cname 是没有约束的
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Cno | char(4) | NO | PRI | NULL | |
| Cname | char(40) | YES | | NULL | |
| Ceredit | smallint(6) | YES | | NULL | |
| Cpno | char(4) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

// 添加唯一值约束

mysql> alter table Course add UNIQUE(Cname);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

// 查看结果, 添加成功

mysql> desc Course;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Cno | char(4) | NO | PRI | NULL | |
| Cname | char(40) | YES | UNI | NULL | |
| Ceredit | smallint(6) | YES | | NULL | |
| Cpno | char(4) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

Student 中忘记添加 下面的完整性约束条件, 百度下 给加上
FOREIGN KEY (Cno) REFERENCES

- 删除列  `alter 表名 drop column 属性名;`
// 我们要删除 列 Cpno
mysql> desc Course;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Cno     | char(4)     | NO   | PRI | NULL    |       |
| Cname   | char(40)    | YES  | UNI | NULL    |       |
| Ceredit | smallint(6) | YES  |     | NULL    |       |
| Cpno    | char(4)     | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

// 删除成功
mysql> alter table Course drop column Cpno;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

// Cpno 已经被删除
mysql> desc Course;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Cno     | char(4)     | NO   | PRI | NULL    |       |
| Cname   | char(40)    | YES  | UNI | NULL    |       |
| Ceredit | smallint(6) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)



- 修改列名字 `alter table 表名 change 旧属性名 新属性名字 数据类型;`
// 查看 Ceredit 状态
mysql> desc Course;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Cno     | char(4)     | NO   | PRI | NULL    |       |
| Cname   | char(40)    | YES  | UNI | NULL    |       |
| Ceredit | smallint(6) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

// 修改 Ceredit 
mysql> alter table Course change Ceredit New_Ceredit int;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

// 查看修改结果
mysql> desc Course;
+-------------+----------+------+-----+---------+-------+
| Field       | Type     | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| Cno         | char(4)  | NO   | PRI | NULL    |       |
| Cname       | char(40) | YES  | UNI | NULL    |       |
| New_Ceredit | int(11)  | YES  |     | NULL    |       |
+-------------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

- 表记录清空 `delete from 表名;`
// 删除 表 Student

mysql> delete from Student;
Query OK, 2 rows affected (0.00 sec)
// 查看 表 Student 发现表已经空了
mysql> select * from Student;
Empty set (0.00 sec)
// 查看表存不存在, 表 Student 还是存在的
mysql> desc Student;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Sno | char(9) | NO | PRI | NULL | |
| Sname | char(20) | YES | UNI | NULL | |
| Ssex | char(2) | YES | | NULL | |
| Sage | varchar(4) | YES | | NULL | |
| Sdept | char(20) | YES | | NULL | |
| S_entrance | date | YES | | NULL | |
| S_score | smallint(6) | YES | | NULL | |
+------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

> 表记录清空 `delete from 表名;` 和 删表 `DROP TABLE Student [RESTRICT | CASCADE]` 是有区别的.
表记录清空, 单纯的清除表的内容, 但是表还在.
但是 删表, 是整个表都被删除, 即 数据库中不再存在这个表
----
- 删表 `DROP TABLE Student [RESTRICT | CASCADE]`
    - RESTRICT 弱删除, 当表被其他表的约束引用(如: CHECK, FOREIGN KEY 等), 而且不能有(视图,触发器,存储过程, 函数), 在这些条件下, 才能被删除. 
    - CASCADE: 强删除, 删表的同时 会删除相关的依赖对象, 如 视图,会一起删除
    ```
    // 删除 Student 表
    mysql> Drop TALE Student CASCADE;
    // 这里报错了, 是因为 Student 是 选课表 SC 的参照表, 里面的Sno 属性是 SC 的外键, 所以无法删除
    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TALE Student CASCADE' at line 1
    ```
- 删库
`mysql>drop database yourdbname;`

----

- [MySQL 教程](http://www.runoob.com/mysql/mysql-tutorial.html)

你可能感兴趣的:(MySQL 基本使用教程)