首页 > 数据库 > MySQL > 正文

MYSQL基本操作命令

2024-07-24 12:36:00
字体:
来源:转载
供稿:网友
         MYSQL基本操作:
 
        RDBMS功能:
 
            1、数据库创建、删除除
 
            2、创建表、删除表、修改表
 
            3、索引的创建、删除
 
            4、用户和权限
 
            5、数据增、删、改
 
            6、查询
 
        Mysql登录命令:
 
            -u USERNAME # 指定用户名,不加默认为root
 
            -p # 密码,不加默认为空
 
            -h MYSER_SERVER # 指定服务器,不指定默认为localhost或者-h 127.0.0.1
 
# mysql -u root -p -h localhost # 回车会让输入密码,mysql默认root的密码为空
        客户端和服务器在同一台机器上的连接方式:
 
            Linux: socket
 
            Windows: memory
 
        Mysql客户端与服务器交互方式:
 
            1、交互式模式
 
            2、批处理模式:执行mysql脚本
 
                交互式模式中的命令类别:
 
                   2. 1、客户端命令
 
                   2. 2、 服务器端命令:都必须使用语句结束符,默认为分号;
 
        数据库数据类型:
 
            1、字符型:
 
                CHAR(n):固定长度n,最大256字符
 
                VARCHAR(n):小于等于n的可变长度,最大65536个字符
 
                BINARY(n):区分大小写的固定长度
 
                VARBINARY(n):区分大小写的可变长度
 
                TEXT(n):文本格式大对象,批量数据
 
                BLOB(n):二进制格式大对象
 
            2、数值型:
 
                2.1、整型:
 
                    TINYINT:1个字节的范围,即8位(范围在-128到128之间)
 
                    SMALLINT:2个字节
 
                    MEDIUMINT:3个字节
 
                    INT:4个字节
 
                    BIGINT:5个字节
 
                2.2、浮点型:
 
                    FLOAT
 
                    DOUBLE
 
            3、修饰符:
 
                UNSIGNED:正数或0
 
                NOT NULL:不能为空
 
            4、 日期时间:
 
                DATE
 
                TIME
 
                DATETIME
 
                STAMP:时间戳
 
        数据库语言,MYSQL常用命令:
 
            1、DDL:Data Defination Lanuage: 数据定义语言
 
                CREATE, ALTER, DROP
 
               1.1、 创建数据库:
 
                CREATE DATABASE db_name;
 
                CREATE DATABASE [IF NOT EXISTS] db_name;
 
mysql> CREATE DATABASE jmmj; # 创建一个数据库
mysql> SHOW DATABASES; # 查看所有数据库
mysql> CREATE DATABASE IF NOT EXISTS jmmj; # 假如不存在则创建该数据库
                1.2、删除数据库:
 
                DROP DATABASE [IF EXISTS] db_name;
 
mysql> DROP DATABASE IF EXISTS jmmj; # 假如存在则删除该数据库
                1.3、创建表:
 
                CREATE TABLE tb_name(col1,col2,...);
 
mysql> USE jmmj # 使用哪个数据库
mysql> CREATE TABLE test(name CHAR(20) NOT NULL,age TINYINT UNSIGNED,gender CHAR(1) NOT NULL); # 创建表test
                1.4、查看某数据库中的表:
 
                SHOW TABLES FROM db_name;
 
mysql> SHOW TABLES FROM jmmj; # 查看数据库jmmj中的表
                1.5、查看数据库中的表结构:
 
                DESC tb_name;
 
mysql> DESC test;
+--------+---------------------+------+-----+---------+-------+
| Field  | Type                | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| name   | char(20)            | NO   |     | NULL    |       |
| age    | tinyint(3) unsigned | YES  |     | NULL    |       |
| gender | char(1)             | NO   |     | NULL    |       |
+--------+---------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
                1.6、 删除表命令:
 
                DROP TABLE tb_name;
 
                1.7、修改表命令:
 
mysql> help ALTER TABLE; # 命令帮助
                    ALTER TABLE tb_name MODIFY|CHANGE|ADD|DROP ......
 
                    MODIFY:改字段属性
 
                    CHANGE:改字段名称
 
                    ADD:添加一个字段
 
                    DROP:删除一个字段
 
mysql> ALTER TABLE test ADD classes VARCHAR(100) NOT NULL; # 添加一个classes的字段
mysql> DESC test; # 看看效果
+---------+---------------------+------+-----+---------+-------+
| Field   | Type                | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+-------+
| name    | char(20)            | NO   |     | NULL    |       |
| age     | tinyint(3) unsigned | YES  |     | NULL    |       |
| gender  | char(1)             | NO   |     | NULL    |       |
| classes | varchar(100)        | NO   |     | NULL    |       |
+---------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
           2、 DML:Data Manapulate Language: 数据操作语言
 
                INSERT, REPLACE, UPDATE, DELETE
 
               2.1、 插入表中的行命令:
 
                INSERT INTO tb_name (col1,col2,...) VALUES|VALUE ('STRING', NUM,...);
 
                INSERT INTO tb_name (col1,col2,...) VALUES|VALUE ('STRING', NUM,...),('STRING',NUM,...);
 
mysql> INSERT INTO test (name,age,gender,classes) VALUE ('liming','19','m','one'); # 插入一行
mysql> INSERT INTO test VALUE ('han×××','18','m','one'); # 如果所有字段都插入则前面字段名可以省略
mysql> INSERT INTO test (name,age,gender,classes) VALUE ('lintao','19','m','one'),('tom','','m','two'); # 还可以批量插入
mysql> SELECT * FROM test; # 查看下效果
+-----------+------+--------+---------+
| name      | age  | gender | classes |
+-----------+------+--------+---------+
| liming    |   19 | m      | one     |
| han×××    |   18 | m      | one     |
| lintao    |   19 | m      | one     |
| tom       |    0 | m      | two     |
+-----------+------+--------+---------+
5 rows in set (0.00 sec)
               2.2、 更新表中的行命令:
 
                UPDATE tb_name SET column=value WHERE
 
mysql> UPDATE test SET gender='f' WHERE name='han×××'; # 修改字段name为han×××的行的gender值为f
mysql> SELECT * FROM test; # 查看下效果
+-----------+------+--------+---------+
| name      | age  | gender | classes |
+-----------+------+--------+---------+
| liming    |   19 | m      | one     |
| han×××    |   18 | f      | one     |
| lintao    |   19 | m      | one     |
| tom       |    0 | m      | two     |
+-----------+------+--------+---------+
5 rows in set (0.00 sec)
               2.3、  删除表中的行命令:
 
                DELETE FROM tb_name WHERE CONDITION;
 
mysql> DELETE FROM test WHERE name='liming';
           3、SELECT:数据库查询语言
 
                查询表命令:
 
                SELECT (col1,col2,...) FROM tb_name WHERE CONDITION
 
                *: 所有字段
 
                WHERE:没有条件表示显示所有行
 
mysql> SELECT * FROM test; # 查询test表中的所有字段
+-----------+------+--------+---------+
| name      | age  | gender | classes |
+-----------+------+--------+---------+
| han×××    |   18 | f      | one     |
| lintao    |   19 | m      | one     |
| tom       |    0 | m      | two     |
| liming    |   20 | m      | one     |
+-----------+------+--------+---------+
4 rows in set (0.00 sec)
           4、DCL:Data Control Language: 数据控制语言
 
                GRANT, REVOKE
 
               4.1、授予权限命令:
 
                GRANT pri1,pri2,... ON DB_NAME.TB_NAME TO 'USERNAME'@'HOST' [IDENTIFIED BY 'PASSWORD'];
 
mysql> GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '666'; # 创建(GRANT若没有则自动创建用户)一个可以在所有终端登录的用户root( root@'%'),设置密码为666,授予对所有库中的所(*.*)有表所有的权限(ALL PRIVILEGES)
mysql> FLUSH PRIVILEGES; # 重读权限
                4.2、撤销权限命令:
 
                REVOKE pri1,pri2,... ON DB_NAME.TB_NAME FROM 'USERNAME'@'HOST';
 
                4.3、查看授权命令:
 
                SHOW GRANTS FOR 'USERNAME'@'HOST';
 
mysql> SHOW GRANTS FOR 'lee'@'%';
           5、MYSQL用户管理:
 
               5.1、用户存放位置:
 
mysql> SELECT User,Host,Password FROM mysql.user;
+------+-----------+-------------------------------------------+
| User | Host      | Password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *007D50CA06F69776D307B1BEC71CD73D0EA0999C |
| root | 127.0.0.1 |                                           |
| root | ::1       |                                           |
|      | localhost |                                           |
| lee  | %         | *007D50CA06F69776D307B1BEC71CD73D0EA0999C |
| root | %         | *007D50CA06F69776D307B1BEC71CD73D0EA0999C |
+------+-----------+-------------------------------------------+
                5.2、创建用户命令:
 
                CREATE USER 'USERNAME'@'HOST' [IDENTIFIED BY 'PASSWORD'];
 
                    HOST表示方式:注意这里的host指定的是能以此用户登录服务器的客户端的范围,%表示所有主机
 
                        IP:
 
                        HOSTNAME:
 
                        NETWORK:
 
                        通配符:
 
                        _:匹配任意单个字符, 172.16.0._
 
                        %:匹配任意字符;
 
mysql> CREATE USER 'lee'@'%' IDENTIFIED BY '666'; # 创建用户lee密码为666,可以从任何客户端登录服务器
                5.3、删除用户命令:
 
                DROP USER 'USERNAME'@'HOST';
 
mysql> DROP USER lee@'%';
                 5.4、设定、修改用户密码:
 
                    1、mysql>SET PASSWORD FOR 'USERNAME'@'HOST'=PASSWORD('password');
 
mysql> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('666');
mysql> FLUSH PRIVILEGES; # 重读权限
                    2、# mysqladmin-uUSERNAME -hHOST -p password 'password'
 
                    3、mysql> UPDATE user SET Password=PASSWORD('password')WHERE USER='root' AND Host='127.0.0.1';
 
           6、MYSQL图形客户端工具:
 
        1、phpMyAdmin
 
# wget https://files.phpmyadmin.net/phpMyAdmin/4.7.7/phpMyAdmin-4.7.7-all-languages.zip
# unzip phpMyAdmin-4.7.7-all-languages.zip
# cp phpMyAdmin-4.7.7-all-languages /usr/local/apache/htdocs/ -rf
# mv phpMyAdmin-4.7.7-all-languages/ phpmyadmin
        http://192.168.8.8/phpmyadmin/  # 输入对应地址即可以管理MYSQL了
 
        2、Workbench
 
        3、MySQL Front
 
        4、Navicat for MySQL
 
        5、Toad

(编辑:武林网)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表