首页 > 代码库 > MySQL基础操作

MySQL基础操作

MySQL数据库是一个C/S(客户端/服务端)架构应用,要访问MySQL数据库要使用专门的客户端软件

  • 登陆到MySQL服务器
[root@www /]# mysql -u rootWelcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 5Server version: 5.5.22-log Source distributionCopyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type help; or \h for help. Type \c to clear the current input statement.mysql> 

 

  • SHOW MASTER LOGS 语句可以查看当前数据库服务的日志文件信息
mysql> show master logs;+------------------+-----------+| Log_name         | File_size |+------------------+-----------+| mysql-bin.000001 |     27284 || mysql-bin.000002 |   1031892 || mysql-bin.000003 |       107 |+------------------+-----------+3 rows in set (0.01 sec)

 

  • 退出mysql>操作环境

执行"quit" "exit" 可以退出mysql命令工具,返回原来的Shell环境

mysql> quitBye

 

  • 查看当前服务器中有哪些库
mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               |+--------------------+4 rows in set (0.00 sec)

 

  • 查看当前使用的库中有哪些表
mysql> use mysql;   #切换到所使用的库Database changedmysql> show tables;   #查看当前所在的库中包含的表+---------------------------+| Tables_in_mysql           |+---------------------------+| columns_priv              || db                        || event                     || func                      || general_log               || help_category             || help_keyword              || help_relation             || help_topic                || host                      || ndb_binlog_index          || plugin                    || proc                      || procs_priv                || proxies_priv              || servers                   || slow_log                  || tables_priv               || time_zone                 || time_zone_leap_second     || time_zone_name            || time_zone_transition      || time_zone_transition_type || user                      |+---------------------------+24 rows in set (0.00 sec)mysql数据库的文件存放在/usr/lcoal/mysql/data目录下,每个数据库对应一个子目录,用于存储数据表文件。每个数据表对应为三个文件,后缀名分别为".frm"".myd"".myi"

 

  • 查看表的结构

describe语句:用于显示表的结构,即组成表的字段(列)的信息。

mysql> use mysql;Database changedmysql> describe user;+------------------------+-----------------------------------+------+-----+---------+-------+| Field                  | Type                              | Null | Key | Default | Extra |+------------------------+-----------------------------------+------+-----+---------+-------+| Host                   | char(60)                          | NO   | PRI |         |       || User                   | char(16)                          | NO   | PRI |         |       || Password               | char(41)                          | NO   |     |         |       || Select_priv            | enum(N,Y)                     | NO   |     | N       |       || Insert_priv            | enum(N,Y)                     | NO   |     | N       |       || Update_priv            | enum(N,Y)                     | NO   |     | N       |       || Delete_priv            | enum(N,Y)                     | NO   |     | N       |       || Create_priv            | enum(N,Y)                     | NO   |     | N       |       || Drop_priv              | enum(N,Y)                     | NO   |     | N       |       || Reload_priv            | enum(N,Y)                     | NO   |     | N       |       || Shutdown_priv          | enum(N,Y)                     | NO   |     | N       |       || Process_priv           | enum(N,Y)                     | NO   |     | N       |       || File_priv              | enum(N,Y)                     | NO   |     | N       |       || Grant_priv             | enum(N,Y)                     | NO   |     | N       |       || References_priv        | enum(N,Y)                     | NO   |     | N       |       || Index_priv             | enum(N,Y)                     | NO   |     | N       |       || Alter_priv             | enum(N,Y)                     | NO   |     | N       |       || Show_db_priv           | enum(N,Y)                     | NO   |     | N       |       || Super_priv             | enum(N,Y)                     | NO   |     | N       |       || Create_tmp_table_priv  | enum(N,Y)                     | NO   |     | N       |       || Lock_tables_priv       | enum(N,Y)                     | NO   |     | N       |       || Execute_priv           | enum(N,Y)                     | NO   |     | N       |       || Repl_slave_priv        | enum(N,Y)                     | NO   |     | N       |       || Repl_client_priv       | enum(N,Y)                     | NO   |     | N       |       || Create_view_priv       | enum(N,Y)                     | NO   |     | N       |       || Show_view_priv         | enum(N,Y)                     | NO   |     | N       |       || Create_routine_priv    | enum(N,Y)                     | NO   |     | N       |       || Alter_routine_priv     | enum(N,Y)                     | NO   |     | N       |       || Create_user_priv       | enum(N,Y)                     | NO   |     | N       |       || Event_priv             | enum(N,Y)                     | NO   |     | N       |       || Trigger_priv           | enum(N,Y)                     | NO   |     | N       |       || Create_tablespace_priv | enum(N,Y)                     | NO   |     | N       |       || ssl_type               | enum(‘‘,ANY,X509,SPECIFIED) | NO   |     |         |       || ssl_cipher             | blob                              | NO   |     | NULL    |       || x509_issuer            | blob                              | NO   |     | NULL    |       || x509_subject           | blob                              | NO   |     | NULL    |       || max_questions          | int(11) unsigned                  | NO   |     | 0       |       || max_updates            | int(11) unsigned                  | NO   |     | 0       |       || max_connections        | int(11) unsigned                  | NO   |     | 0       |       || max_user_connections   | int(11) unsigned                  | NO   |     | 0       |       || plugin                 | char(64)                          | YES  |     |         |       || authentication_string  | text                              | YES  |     | NULL    |       |+------------------------+-----------------------------------+------+-----+---------+-------+42 rows in set (0.00 sec)

 

  • 创建新的库

create database 语句:用于创建一个新的库,需要指定数据库名称作为参数。

mysql> create database auth;Query OK, 1 row affected (0.00 sec)
刚创建的数据库是空的,其中不包含任何表,在/usr/lcoal/mysql/data目录下会自动生成一个与新建的库名相同的空文件夹

 

  • 创建新的表

create table 语句:用于在当前库中穿件新的表,需指定数据库表名称作为参数,并定义该表格所使用的各字段

mysql> use auth;Database changedmysql> create table users (user_name char(16) not null, user_passwd char(48) default ‘‘, primary key (user_name));Query OK, 0 rows affected (0.11 sec)

 

  • drop table语句:用于删除库中的表,需要指定"库名.表名" 作为参数;若只指定表明参数,则需先通过执行"use"语句切换到目标库。
mysql> drop table auth.users;Query OK, 0 rows affected (0.00 sec)

 

  • 删除一个数据库

drop database语句:用于删除指定的库,需要指定库名作为参数。

mysql> drop database auth;Query OK, 0 rows affected (0.00 sec)

 

MySQL基础操作