Linux下安装和使用MySQL(四)

八、MySQL的常用操作

  注意:MySQL中每个命令后都要以分号;结尾。

  1、显示数据库
    mysql> show databases;
    +-----------------------+
    | Database              |
    +-----------------------+
    | information_schema    | 
    | mysql                 | 
    | test                  | 
    | #mysql50#{mysql,test} | 
    +-----------------------+
    4 rows in set (0.00 sec)

  Mysql刚安装完有两个数据库:mysql和test。mysql库非常重要,它里面有MySQL的系统信息,我们改密码和新增用户,实际上就是用这个库中的相关表进行操作。

  2、显示数据库中的表
    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> 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                | 
    | servers                   | 
    | slow_log                  | 
    | tables_priv               | 
    | time_zone                 | 
    | time_zone_leap_second     | 
    | time_zone_name            | 
    | time_zone_transition      | 
    | time_zone_transition_type | 
    | user                      | 
    +---------------------------+
    23 rows in set (0.00 sec)
    
    mysql> 

  3、显示数据表的结构:
  describe 表名;

  4、显示表中的记录:
  select * from 表名;
  例如:显示mysql库中user表中的纪录。所有能对MySQL用户操作的用户都在此表中。
  Select * from user;

  5、建库:
  create database 库名;(小心不要把database打成databases了);
  例如:创建一个名字位aaa的库
  mysql> create database cvskill;

6、建表:
  use 库名;
  create table 表名 (字段设定列表);
  例如:在刚创建的cvskill库中建立表name,表中有id(序号,自动增长),xm(姓名),xb(性别),csny(出身年月)四个字段
  use cvskill;
  mysql> create table name (id int(3) auto_increment not null primary key, xm char(8),xb char(2),csny date);
  可以用describe命令察看刚建立的表结构。
    mysql> describe name;
    +-------+---------+------+-----+---------+----------------+
    | Field | Type    | Null | Key | Default | Extra          |
    +-------+---------+------+-----+---------+----------------+
    | id    | int(3)  | NO   | PRI | NULL    | auto_increment | 
    | xm    | char(8) | YES  |     | NULL    |                | 
    | xb    | char(2) | YES  |     | NULL    |                | 
    | csny  | date    | YES  |     | NULL    |                | 
    +-------+---------+------+-----+---------+----------------+
    4 rows in set (0.00 sec)


  7、增加记录
  例如:增加几条相关纪录。
  mysql> insert into name values('','Tom','m','1971-10-01');
  Query OK, 1 row affected, 1 warning (0.00 sec)
  
  mysql> insert into name values('','Cat','f','1973-10-01');
  Query OK, 1 row affected, 1 warning (0.00 sec)
  
    可用select命令来验证结果。

  mysql> select * from name;
  +----+------+------+------------+
  | id | xm   | xb   | csny       |
  +----+------+------+------------+
  |  1 | Tom  | m    | 1971-10-01 | 
  |  2 | Cat  | f    | 1973-10-01 | 
  +----+------+------+------------+
  2 rows in set (0.00 sec)


  8、修改纪录
  例如:将Tom的出生年月改为1971-01-10
  mysql> update name set csny='1971-01-10' where xm='Tom';
    或者:
  mysql> update name set csny='1971-01-10' where id=1;

  9、删除纪录
  例如:删除Tom的纪录。
  mysql> delete from name where xm='Tom';

  10、删库和删表
  drop database 库名;
  drop table 表名;

原文地址:https://www.cnblogs.com/deogao/p/3422046.html