美文网首页
mac下安装和使用mysql

mac下安装和使用mysql

作者: runner123 | 来源:发表于2018-06-12 11:56 被阅读0次

    一、MySQL数据库的安装和配置
    进入官网,选择mysql的Community版本即可
    https://dev.mysql.com/downloads/mysql/

    image.png
    下载完成后点击进行安装,根据提示点下一步,默认会生成数据库,记住自己设置的默认密码

    官网下载的数据库包安装目录默认为

    /usr/local/mysql/bin
    

    如果不想每次都进入该目录执行命令,可配置到环境变量中

     open .bash_profile
    

    将mysql的bin目录配置到环境变量中

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

    执行source使配置生效

    source .bash_profile
    

    二、mysql的基本命令操作
    1、数据库的连接(首先自行检查数据库的服务是开启的)

    mysql -h localhost -u root -p
    

    看到下面的标示标示连接成功,可以进行正常的数据库操作

    mysql>
    

    mysql -h 主机名 -u 用户名 -p密码
    ◆-h:后面接的是主机名,表示你要连接到哪台主机的MySQL数据库。

    ◆-u:后面接的是用户名,MySQL默认的就是root。

    ◆-p:指定需要使用密码登陆MySQL数据库,如果密码为空,该参数可以省略。

    例如:

    mysql -h 192.168.188.90 -u root -p123456
    

    连接IP:192.168.188.90 用户名:root 密码:123456的数据库

    2、查看已有的数据库
    mysql作为数据仓库,里面可以创建多个数据库,每个数据库里面可以创建多个数据库表,查看当前仓库的数据库,命令:

     show databases;
    

    效果如下

    mysql> show databases;
    +---------------------+
    | Database            |
    +---------------------+
    | information_schema  |
    | asset               |
    | asset_database      |
    | assets_database     |
    | boss_database       |
    

    注意该命令后边的;号一定要有
    3、创建自己的数据库

    create database 数据库名;
    
    mysql> create database user_info;
    Query OK, 1 row affected (0.01 sec)
    

    4、选择操作的数据库

    use 数据库名  
    

    5、在指定数据库中创建数据库表

    首先执行 use 数据库名  指定要执行的数据库
    然后使用 create table 数据库表名; 命令创建表
    使用 describe  数据库表名; 命令检查创建的数据库表
    
    mysql> use user_info
    Database changed
    mysql> create table user_info
        -> (
        -> id char(10) not null primary key,
        -> name char(20) not null,
        -> sex char(6) not null,
        -> age int not null,
        -> address char(36) not null
        -> );
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> describe user_info;
    +---------+----------+------+-----+---------+-------+
    | Field   | Type     | Null | Key | Default | Extra |
    +---------+----------+------+-----+---------+-------+
    | id      | char(10) | NO   | PRI | NULL    |       |
    | name    | char(20) | NO   |     | NULL    |       |
    | sex     | char(6)  | NO   |     | NULL    |       |
    | age     | int(11)  | NO   |     | NULL    |       |
    | address | char(36) | NO   |     | NULL    |       |
    +---------+----------+------+-----+---------+-------+
    5 rows in set (0.02 sec)
    
    mysql>
    

    三、数据库表的增删改查操作
    1、向数据库表中添加数据

    insert into 数据库表名 values(value值1,value值2,.......);
    
    mysql> insert into user_info value("1000023","liz","male",21,"xian");
    Query OK, 1 row affected (0.01 sec)
    mysql> insert into user_info value("1000024","liy","male",25,"beijing");
    Query OK, 1 row affected (0.00 sec)
    

    2、查询数据库表中的数据

    select  列名称 from 数据库表名称  where 查询条件 and 查询条件;
    
    mysql> select * from user_info;
    +---------+------+------+-----+---------+
    | id      | name | sex  | age | address |
    +---------+------+------+-----+---------+
    | 1000023 | liz  | male |  21 | xian    |
    | 1000024 | liy  | male |  25 | beijing |
    +---------+------+------+-----+---------+
    2 rows in set (0.00 sec)
    
    mysql> select name from user_info where name="liy";
    +------+
    | name |
    +------+
    | liy  |
    +------+
    1 row in set (0.00 sec)
    
    mysql> select name,id from user_info where name="liz"
        -> ^C
    mysql> select name,id from user_info where name="liz";
    +------+---------+
    | name | id      |
    +------+---------+
    | liz  | 1000023 |
    +------+---------+
    1 row in set (0.00 sec)
    mysql> select name,id from user_info where name="liz" and id="1000023";
    +------+---------+
    | name | id      |
    +------+---------+
    | liz  | 1000023 |
    +------+---------+
    1 row in set (0.00 sec)
    
    mysql> select name,id from user_info where name="liz" and id="1000024";
    Empty set (0.00 sec)
    
    mysql>
    

    记得数据库表操作的命令一定要分号结束
    3、修改数据库表中数据

    update 数据库表名 set 列名=赋值 where 修改条件
    
    mysql> select * from user_info;
    +---------+------+------+-----+---------+
    | id      | name | sex  | age | address |
    +---------+------+------+-----+---------+
    | 1000023 | lisi | male |  21 | xian    |
    | 1000024 | liy  | male |  25 | beijing |
    +---------+------+------+-----+---------+
    2 rows in set (0.00 sec)
    
    mysql> update user_info set age=age+1;
    Query OK, 2 rows affected (0.00 sec)
    Rows matched: 2  Changed: 2  Warnings: 0
    
    mysql> select * from user_info;
    +---------+------+------+-----+---------+
    | id      | name | sex  | age | address |
    +---------+------+------+-----+---------+
    | 1000023 | lisi | male |  22 | xian    |
    | 1000024 | liy  | male |  26 | beijing |
    +---------+------+------+-----+---------+
    2 rows in set (0.00 sec)
    
    mysql> update user_info set name="wangwu",age=12 where name="lisi";
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    
    mysql> select * from user_info;
    +---------+--------+------+-----+---------+
    | id      | name   | sex  | age | address |
    +---------+--------+------+-----+---------+
    | 1000023 | wangwu | male |  12 | xian    |
    | 1000024 | liy    | male |  26 | beijing |
    +---------+--------+------+-----+---------+
    2 rows in set (0.01 sec)
    

    4、删除数据库表中内容

    delete from 数据库表名 where 删除条件;
    
    mysql> delete from user_info where name="wangwu";
    Query OK, 1 row affected (0.06 sec)
    mysql> select * from user_info;
    +---------+------+------+-----+---------+
    | id      | name | sex  | age | address |
    +---------+------+------+-----+---------+
    | 1000024 | liy  | male |  26 | beijing |
    +---------+------+------+-----+---------+
    1 row in set (0.01 sec)
    

    5、修改数据库表
    上面是关于数据库表中的数据的增删改查的操作,接下来是关于修改数据库表的操作,针对数据库表的操作,主要是删除数据库表、数据库表重命名、对数据库表的列进行修改、删除操作

    (1)修改数据库表中的列:

    alter table 数据库表名 change 列名称 新数据;
    

    修改前,先查看之前的数据库表的详细信息

    mysql> describe user_info;
    +---------+----------+------+-----+---------+-------+
    | Field   | Type     | Null | Key | Default | Extra |
    +---------+----------+------+-----+---------+-------+
    | id      | char(10) | NO   | PRI | NULL    |       |
    | name    | char(20) | NO   |     | NULL    |       |
    | sex     | char(6)  | NO   |     | NULL    |       |
    | age     | int(11)  | NO   |     | NULL    |       |
    | address | char(36) | NO   |     | NULL    |       |
    +---------+----------+------+-----+---------+-------+
    5 rows in set (0.01 sec)
    
    mysql> alter table user_info change address addr char(40) not null;
    Query OK, 1 row affected (0.03 sec)
    Records: 1  Duplicates: 0  Warnings: 0
    
    mysql> alter table user_info change name userName char(20) not null;
    Query OK, 0 rows affected (0.01 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> describe user_info;
    +----------+----------+------+-----+---------+-------+
    | Field    | Type     | Null | Key | Default | Extra |
    +----------+----------+------+-----+---------+-------+
    | id       | char(10) | NO   | PRI | NULL    |       |
    | userName | char(20) | NO   |     | NULL    |       |
    | sex      | char(6)  | NO   |     | NULL    |       |
    | age      | int(11)  | NO   |     | NULL    |       |
    | addr     | char(40) | NO   |     | NULL    |       |
    +----------+----------+------+-----+---------+-------+
    5 rows in set (0.00 sec)
    

    (2)删除数据库表中列

    alter table 数据库表名 drop 列名称;
    
    mysql> alter table user_info drop addr;
    Query OK, 0 rows affected (0.03 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> describe user_info;
    +----------+----------+------+-----+---------+-------+
    | Field    | Type     | Null | Key | Default | Extra |
    +----------+----------+------+-----+---------+-------+
    | id       | char(10) | NO   | PRI | NULL    |       |
    | userName | char(20) | NO   |     | NULL    |       |
    | sex      | char(6)  | NO   |     | NULL    |       |
    | age      | int(11)  | NO   |     | NULL    |       |
    +----------+----------+------+-----+---------+-------+
    4 rows in set (0.00 sec)
    

    (3) 增加数据库表中的列

    alter table 数据库表名 add 添加的列信息;
    
    mysql> alter table user_info add address char(40) not null;
    Query OK, 0 rows affected (0.02 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> describe user_info;
    +----------+----------+------+-----+---------+-------+
    | Field    | Type     | Null | Key | Default | Extra |
    +----------+----------+------+-----+---------+-------+
    | id       | char(10) | NO   | PRI | NULL    |       |
    | userName | char(20) | NO   |     | NULL    |       |
    | sex      | char(6)  | NO   |     | NULL    |       |
    | age      | int(11)  | NO   |     | NULL    |       |
    | address  | char(40) | NO   |     | NULL    |       |
    +----------+----------+------+-----+---------+-------+
    5 rows in set (0.00 sec)
    

    (4) 重命名数据库表

    alter table 数据库表 rename 新表名;
    
    mysql> alter table user_info rename user;
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> show tables;
    +---------------------+
    | Tables_in_user_info |
    +---------------------+
    | user                |
    +---------------------+
    1 row in set (0.00 sec)
    

    (5) 删除数据库表
    当该数据库表的信息已经是不需要的信息时,可以执行删表操作:

    drop table 数据库表名;
    
    mysql> drop table user;
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> show tables;
    Empty set (0.00 sec)
    

    6、删除数据库

    drop database 数据库名;
    
    mysql> drop database user_info;
    Query OK, 0 rows affected (0.01 sec)
    

    以上就是关于mysql安装,数据库连接、数据库的基本操作,数据库表的操作的简单事例。

    相关文章

      网友评论

          本文标题:mac下安装和使用mysql

          本文链接:https://www.haomeiwen.com/subject/zpbheftx.html