mysql 安装 & 基本操作 & 数据类型

时间:2021-06-25 17:12:30   收藏:0   阅读:0

windows10系统mysql5.7 安装

  1. 下载mysql-5.7.34-winx64 https://dev.mysql.com/downloads/file/?id=502363

  2. 解压mysql-5.7.34-winx64.zip

  3. 进入解压文件,创建my.ini文件

  4. my.ini文件编写

    [mysql]
    # 设置mysql客户端默认字符集
    default-character-set=utf8
    [mysqld]
    # 设置3306端口
    port=3306
    # 设置mysql的安装目录
    basedir=C:\\ly\mysql-5.7.34-winx64
    # 设置mysql数据库的数据的存放目录
    datadir=C:\\ly\mysql-5.7.34-winx64\\data
    # 允许最大连接数
    max_connections=200
    # 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
    max_connect_errors=10
    # 服务端使用的字符集默认为UTF8
    character-set-server=utf8
    # 创建新表时将使用的默认存储引擎
    default-storage-engine=INNODB
    
  5. 以管理员身份运行cmd,进入bin目录执行:

    1. 初始化,创建mysql默认的root账户

      # mysqld --initialize-insecure --user=mysql
      
    2. 安装

      # mysqld install
      
    3. 启动MySql服务

      # net start mysql
      
    4. 修改环境变量,添加C:\ly\mysql-5.7.34-winx64\bin

    5. cmd窗口中执行mysql -uroot -p进入;默认没有密码

    6. 若要卸载先停止服务,再删除即可

      #启动mysql服务
      net start mysql
      #停止mysql服务
      net stop mysql
      

Mysql基础操作

SQL语句中的快捷键

mysql增删改查

  1. 操作【数据库】(文件夹)

    • #创建数据库
      create database db0616 charset utf8;
      
    • #删除数据库db0616
      mysql> drop database db0616;
      Query OK, 0 rows affected (0.05 sec)
      
      mysql> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | abc                |
      | db0616_2           |
      | mysql              |
      | performance_schema |
      | sys                |
      +--------------------+
      6 rows in set (0.00 sec)
      
    • #alter 更改数据库的字符集
      mysql> alter database db0616 charset gbk;
      Query OK, 1 row affected (0.00 sec)
      
      mysql> show create database db0616;
      +----------+----------------------------------------------------------------+
      | Database | Create Database                                                |
      +----------+----------------------------------------------------------------+
      | db0616   | CREATE DATABASE `db0616` /*!40100 DEFAULT CHARACTER SET gbk */ |
      +----------+----------------------------------------------------------------+
      1 row in set (0.00 sec)
      
    • #查看所有数据库
      show databases;
      #查看数据库的建表语句
      mysql> show create database db0616;
      +----------+-----------------------------------------------------------------+
      | Database | Create Database                                                 |
      +----------+-----------------------------------------------------------------+
      | db0616   | CREATE DATABASE `db0616` /*!40100 DEFAULT CHARACTER SET utf8 */ |
      +----------+-----------------------------------------------------------------+
      1 row in set (0.00 sec)
      
      mysql> CREATE DATABASE `db0616_2` /*!40100 DEFAULT CHARACTER SET utf8 */;
      Query OK, 1 row affected (0.00 sec)
      
  2. 操作【数据表】(文件)

    选择数据库

    ? use 数据库名

    • #int 整型  char 字符
      #字段名1 类型1,字段名2 类型2 ...
      mysql> create table t1(id int ,name char);
      Query OK, 0 rows affected (0.03 sec)
      
    • #删除表t1
      drop table t1;
      
    • #modify 只能改变数据类型
      mysql> alter table t1 modify name char(5);
      Query OK, 0 rows affected (0.06 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      
      mysql> desc t1;
      +-------+---------+------+-----+---------+-------+
      | Field | Type    | Null | Key | Default | Extra |
      +-------+---------+------+-----+---------+-------+
      | id    | int(11) | YES  |     | NULL    |       |
      | name  | char(5) | YES  |     | NULL    |       |
      +-------+---------+------+-----+---------+-------+
      2 rows in set (0.01 sec)
      
      #change 改字段名 + 数据类型一起改变
      mysql> alter table t1 change name NAME char(4);
      Query OK, 0 rows affected (0.04 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      mysql> desc t1;
      +-------+---------+------+-----+---------+-------+
      | Field | Type    | Null | Key | Default | Extra |
      +-------+---------+------+-----+---------+-------+
      | id    | int(11) | YES  |     | NULL    |       |
      | NAME  | char(4) | YES  |     | NULL    |       |
      +-------+---------+------+-----+---------+-------+
      2 rows in set (0.00 sec)
      
      mysql> alter table t1 change NAME name char(5);
      Query OK, 0 rows affected (0.04 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      
      mysql> desc t1;
      +-------+---------+------+-----+---------+-------+
      | Field | Type    | Null | Key | Default | Extra |
      +-------+---------+------+-----+---------+-------+
      | id    | int(11) | YES  |     | NULL    |       |
      | name  | char(5) | YES  |     | NULL    |       |
      +-------+---------+------+-----+---------+-------+
      2 rows in set (0.00 sec)
      
      #add 添加字段
      mysql> alter table t1 add age int;
      Query OK, 0 rows affected (0.04 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      
      mysql> desc t1;
      +-------+---------+------+-----+---------+-------+
      | Field | Type    | Null | Key | Default | Extra |
      +-------+---------+------+-----+---------+-------+
      | id    | int(11) | YES  |     | NULL    |       |
      | name  | char(5) | YES  |     | NULL    |       |
      | age   | int(11) | YES  |     | NULL    |       |
      +-------+---------+------+-----+---------+-------+
      3 rows in set (0.00 sec)
      
      #drop 删除字段 column列(字段,关键字)
      mysql> alter table t1 drop column age;
      Query OK, 0 rows affected (0.04 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      
      mysql> desc t1;
      +-------+---------+------+-----+---------+-------+
      | Field | Type    | Null | Key | Default | Extra |
      +-------+---------+------+-----+---------+-------+
      | id    | int(11) | YES  |     | NULL    |       |
      | name  | char(5) | YES  |     | NULL    |       |
      +-------+---------+------+-----+---------+-------+
      2 rows in set (0.00 sec)
      
      # rename 更改表名
      mysql> alter table t1 rename t1_1;
      Query OK, 0 rows affected (0.01 sec)
      
      mysql> show tables;
      +------------------+
      | Tables_in_db0616 |
      +------------------+
      | t1_1             |
      +------------------+
      1 row in set (0.00 sec)
      
      
    • #查询所有数据表
      mysql> show tables;
      +------------------+
      | Tables_in_db0616 |
      +------------------+
      | t1               |
      +------------------+
      1 row in set (0.00 sec)
      
      #查看表的建表语句
      mysql> show create table t1;
      +-------+---------------------------------------------------------------------------------------------------------------------+
      | Table | Create Table                                                                                                        |
      +-------+---------------------------------------------------------------------------------------------------------------------+
      | t1    | CREATE TABLE `t1` (
        `id` int(11) DEFAULT NULL,
        `name` char(1) DEFAULT NULL
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
      +-------+---------------------------------------------------------------------------------------------------------------------+
      1 row in set (0.00 sec)
      
      #查看表结构
      mysql> desc t1;
      +-------+---------+------+-----+---------+-------+
      | Field | Type    | Null | Key | Default | Extra |
      +-------+---------+------+-----+---------+-------+
      | id    | int(11) | YES  |     | NULL    |       |
      | name  | char(1) | YES  |     | NULL    |       |
      +-------+---------+------+-----+---------+-------+
      2 rows in set (0.00 sec)
      
      
      mysql> show create table t1\G;(加上\G垂直显示,默认是横向显示)
      *************************** 1. row ***************************
             Table: t1
      Create Table: CREATE TABLE `t1` (
        `id` int(11) DEFAULT NULL,
        `name` char(1) DEFAULT NULL
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8
      1 row in set (0.00 sec)
      
      ERROR:
      No query specified
      
      mysql> \s
      --------------
      mysql  Ver 14.14 Distrib 5.7.34, for Win64 (x86_64)
      
      Connection id:          15
      Current database:       db0616
      Current user:           root@localhost
      SSL:                    Cipher in use is ECDHE-RSA-AES128-GCM-SHA256
      Using delimiter:        ;
      Server version:         5.7.34 MySQL Community Server (GPL)
      Protocol version:       10
      Connection:             localhost via TCP/IP
      Server characterset:    utf8
      Db     characterset:    utf8
      Client characterset:    utf8
      Conn.  characterset:    utf8
      TCP port:               3306
      Uptime:                 16 hours 46 min 11 sec
      
      Threads: 1  Questions: 74  Slow queries: 0  Opens: 127  Flush tables: 1  Open tables: 120  Queries per second avg: 0.001
      --------------
      
      mysql> \h
      
      For information about MySQL products and services, visit:
         http://www.mysql.com/
      For developer information, including the MySQL Reference Manual, visit:
         http://dev.mysql.com/
      To buy MySQL Enterprise support, training, or other products, visit:
         https://shop.mysql.com/
      
      List of all MySQL commands:
      Note that all text commands must be first on line and end with ‘;‘
      ?         (\?) Synonym for `help‘.
      clear     (\c) Clear the current input statement.
      connect   (\r) Reconnect to the server. Optional arguments are db and host.
      delimiter (\d) Set statement delimiter.
      ego       (\G) Send command to mysql server, display result vertically.
      exit      (\q) Exit mysql. Same as quit.
      go        (\g) Send command to mysql server.
      help      (\h) Display this help.
      notee     (\t) Don‘t write into outfile.
      print     (\p) Print current command.
      prompt    (\R) Change your mysql prompt.
      quit      (\q) Quit mysql.
      rehash    (\#) Rebuild completion hash.
      source    (\.) Execute an SQL script file. Takes a file name as an argument.
      status    (\s) Get status information from the server.
      tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
      use       (\u) Use another database. Takes database name as argument.
      charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
      warnings  (\W) Show warnings after every statement.
      nowarning (\w) Don‘t show warnings after every statement.
      resetconnection(\x) Clean session context.
      
      For server side help, type ‘help contents‘
      
  3. 操作记录(文件里面的数据)

    • mysql null 相当于python None是一样的 代表空的 什么也没有

    .frm文件 表结构

    .ibd文件 表数据

    • #一次插入一条数据
      #insert into 表名(字段1,字段2) values(值1,值2)
      mysql> insert into t1(id,name) values(1,‘xboy1‘);
      Query OK, 1 row affected (0.01 sec)
      
      #一次插入多条数据
      mysql> insert into t1(id,name) values(2,‘xboy2‘),(3,‘xboy3‘),(4,‘xboy4‘);
      Query OK, 3 rows affected (0.00 sec)
      Records: 3  Duplicates: 0  Warnings: 0
                  
      #不指定具体字段,默认把所有字段插一遍
      mysql> insert into t1 value(5,‘xboy5‘);
      Query OK, 1 row affected (0.00 sec)
      
      #可以具体指定某个字段进行插入
      mysql> insert into t1(name) values(‘xboy6‘);
      Query OK, 1 row affected (0.00 sec)
      
      mysql> select * from t1;
      +------+-------+
      | id   | name  |
      +------+-------+
      |    1 | xboy1 |
      |    2 | xboy2 |
      |    3 | xboy3 |
      |    4 | xboy4 |
      |    5 | xboy5 |
      | NULL | xboy6 |
      +------+-------+
      6 rows in set (0.00 sec)
      
    • # * 代表所有
      mysql> select * from t1;
      +------+-------+
      | id   | name  |
      +------+-------+
      |    1 | xboy1 |
      +------+-------+
      1 row in set (0.00 sec)
      
      #把要搜索的字段写在select后面
      select id,name from t1;
      
    • #update 表名 set 名字=值 where 条件
      mysql> update t1 set name=‘王文‘ where id=1;
      Query OK, 1 row affected (0.01 sec)
      Rows matched: 1  Changed: 1  Warnings: 0
      
      mysql> select * from t1;
      +------+--------+
      | id   | name   |
      +------+--------+
      |    1 | 王文   |
      |    2 | xboy2  |
      |    3 | xboy3  |
      |    4 | xboy4  |
      |    5 | xboy5  |
      | NULL | xboy6  |
      +------+--------+
      6 rows in set (0.00 sec)
      
      #如果不加条件,所有的数据都会改掉
      mysql> update t1 set name=‘王文‘;
      Query OK, 5 rows affected (0.00 sec)
      Rows matched: 6  Changed: 5  Warnings: 0
      
      mysql> select * from t1;
      +------+--------+
      | id   | name   |
      +------+--------+
      |    1 | 王文   |
      |    2 | 王文   |
      |    3 | 王文   |
      |    4 | 王文   |
      |    5 | 王文   |
      | NULL | 王文   |
      +------+--------+
      6 rows in set (0.00 sec)
      
    • #删除id为2的这条数据
      mysql> delete from t1 where id = 2;
      Query OK, 1 row affected (0.00 sec)
      
      mysql> select * from t1;
      +------+--------+
      | id   | name   |
      +------+--------+
      |    1 | 王文   |
      |    3 | 王文   |
      |    4 | 王文   |
      |    5 | 王文   |
      | NULL | 王文   |
      +------+--------+
      5 rows in set (0.00 sec)
      
      #不加条件删除所有数据
      mysql> delete from t1 ;
      Query OK, 5 rows affected (0.00 sec)
      
      mysql> select * from t1;
      Empty set (0.00 sec)
      
      #重置数据表(重置id)
      truncate table t1;
      

常用数据类型

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!