全栈开发知识库

MySQL简介(一)

MySQL是最流行的关系型数据库管理系统,在Web应用方面MySQL是最好的RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。

RDBMS即关系数据库管理系统(Relational Database Management System)的特点:
- 数据以表格的形式出现 - 每行为各种记录名称 - 每列为记录名称所对应的数据域 - 许多的行和列组成一张表单 - 若干的表单组成database

MySQL由瑞典MySQL AB公司开发,目前属于Oracle公司:
- MySQL是开源的,所以不需要支付额外的费用。 - MySQL支持大型的数据库。可以处理拥有上千万条记录的大型数据库。 - MySQL使用标准的SQL数据语言形式。 - MySQL可以运行于多个系统上,并且支持多种语言。 - MySQL支持大型数据库,支持5000万条记录的数据仓库,32位系统表文件最大可支持4G,64位系统支持最大的表文件为8T。

MySQL下载地址 选择MySQL Community Server下载dmg安装包,然后一路安装,安装的过程中会提示让输入一个密码, 要记住自己设置的密码。

安装完成后,进入MacOS的系统偏好设置,最后面就有MySQL的选项了。

然后在Path文件中加上mysql的路径

PATH=$PATH:/usr/local/mysql/bin

登陆MySQL

在命令行执行

mysql -u root -p

然后输入刚才安装时设置的密码

B0000000134553m:~ xxx$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.13 MySQL Community Server - GPL

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

如果要登陆远程服务器的MySQL服务器,需要加上-h参数指定主机名

mysql -h 主机名 -u 用户名 -p

然后就继续执行命令就可以了

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> create database test;
Query OK, 1 row affected (0.01 sec)

mysql> show databases;
mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table Student(
    ->   SNO char(7) primary key,
    ->   SNAME varchar(20) not null,
    ->   SDEPT varchar(20) default '计算机'
    -> )comment='学生表';
Query OK, 0 rows affected (0.10 sec)
mysql> desc test.Student;
+-------+-------------+------+-----+-----------+-------+
| Field | Type        | Null | Key | Default   | Extra |
+-------+-------------+------+-----+-----------+-------+
| SNO   | char(7)     | NO   | PRI | NULL      |       |
| SNAME | varchar(20) | NO   |     | NULL      |       |
| SDEPT | varchar(20) | YES  |     | 计算机    |       |
+-------+-------------+------+-----+-----------+-------+
3 rows in set (0.00 sec)

mysql> insert into Student values ('9513101','罗大力','教育数学');
Query OK, 1 row affected (0.05 sec)
mysql> select * from Student;
+---------+-----------+--------------+
| SNO     | SNAME     | SDEPT        |
+---------+-----------+--------------+
| 9513101 | 罗大力    | 教育数学     |
+---------+-----------+--------------+
1 row in set (0.00 sec)
mysql> update Student set SDEPT='物联网' where SNAME='罗大力';
Query OK, 1 row affected (0.09 sec)
Rows matched: 1  Changed: 1  Warnings: 0
mysql> delete from Student where SNAME='罗大力';
Query OK, 1 row affected (0.06 sec)
mysql> select * from Student;
Empty set (0.00 sec)
mysql> drop table Student;
Query OK, 0 rows affected (0.06 sec)
mysql> drop database test;
Query OK, 0 rows affected (0.07 sec)
mysql> quit
Bye

我们本地还是用上面的语句创建库和表,增加一条数据,然后通过go去查询。

package main
import (
    "database/sql"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
)

func main()  {
    db, e := sql.Open("mysql", "root:ZUOWOZIJI@tcp(localhost:3306)/test?charset=utf8")
    checkErr(e)

    stmt, err := db.Prepare("INSERT Student SET SNO = ?, SNAME = ?, SDEPT = ?")
    checkErr(err)
    result, err := stmt.Exec("9513102", "出奇迹", "大数据")
    checkErr(err)
    id, err := result.LastInsertId()
    checkErr(err)
    fmt.Println("insert data : ", id)

    //更新数据
    stmt, err = db.Prepare("UPDATE Student SET SNAME = ? WHERE SNAME = ?")
    checkErr(err)

    exec, err := stmt.Exec("大力", "出奇迹")
    checkErr(err)

    affect, err := exec.RowsAffected()
    checkErr(err)

    fmt.Println(affect)

    rows, queryError := db.Query("SELECT * FROM Student")
    if queryError != nil {
        fmt.Println("database query error : ", queryError)
        return
    }

    var SNO string
    var SNAME string
    var SDEPT string
    for rows.Next()  {
        rows.Scan(&SNO, &SNAME, &SDEPT)
        fmt.Println(SNO, SNAME, SDEPT)
    }
}
func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

执行结果:

insert data :  0
3
123434 大力 大数据
1234567 大力 大数据
9513101 罗大力 物联网
9513102 大力 大数据

  • 邮箱 :charon.chui@gmail.com
  • Good Luck!