• MySQL基础语句实际演练


    MySQL基础 语句实际演练

    在一个运行MySQL的服务器上,实际上可以创建多个数据库(Database)。要列出所有数据库,使用命令:

    mysql> SHOW DATABASES;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | shici              |
    | sys                |
    | test               |
    | school             |
    +--------------------+
    

    其中,information_schemamysqlperformance_schemasys是系统库,不要去改动它们。其他的是用户创建的数据库。

    要创建一个新数据库,使用命令:

    mysql> CREATE DATABASE test;
    Query OK, 1 row affected (0.01 sec)
    

    要删除一个数据库,使用命令:

    mysql> DROP DATABASE test;
    Query OK, 0 rows affected (0.01 sec)
    

    注意:删除一个数据库将导致该数据库的所有表全部被删除。

    对一个数据库进行操作时,要首先将其切换为当前数据库:

    mysql> USE test;
    Database changed
    

    列出当前数据库的所有表,使用命令:

    mysql> SHOW TABLES;
    +---------------------+
    | Tables_in_test      |
    +---------------------+
    | classes             |
    | statistics          |
    | students            |
    | students_of_class1  |
    +---------------------+
    

    要查看一个表的结构,使用命令:

    mysql> DESC students;
    +----------+--------------+------+-----+---------+----------------+
    | Field    | Type         | Null | Key | Default | Extra          |
    +----------+--------------+------+-----+---------+----------------+
    | id       | bigint(20)   | NO   | PRI | NULL    | auto_increment |
    | class_id | bigint(20)   | NO   |     | NULL    |                |
    | name     | varchar(100) | NO   |     | NULL    |                |
    | gender   | varchar(1)   | NO   |     | NULL    |                |
    | score    | int(11)      | NO   |     | NULL    |                |
    +----------+--------------+------+-----+---------+----------------+
    5 rows in set (0.00 sec)
    

    还可以使用以下命令查看创建表的SQL语句:

    mysql> SHOW CREATE TABLE students;
    +----------+-------------------------------------------------------+
    | students | CREATE TABLE `students` (                             |
    |          |   `id` bigint(20) NOT NULL AUTO_INCREMENT,            |
    |          |   `class_id` bigint(20) NOT NULL,                     |
    |          |   `name` varchar(100) NOT NULL,                       |
    |          |   `gender` varchar(1) NOT NULL,                       |
    |          |   `score` int(11) NOT NULL,                           |
    |          |   PRIMARY KEY (`id`)                                  |
    |          | ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 |
    +----------+-------------------------------------------------------+
    1 row in set (0.00 sec)
    

    创建表使用CREATE TABLE语句,而删除表使用DROP TABLE语句:

    mysql> DROP TABLE students;
    Query OK, 0 rows affected (0.01 sec)
    

    修改表就比较复杂。如果要给students表新增一列birth,使用:

    ALTER TABLE students ADD COLUMN birth VARCHAR(10) NOT NULL;
    

    要修改birth列,例如把列名改为birthday,类型改为VARCHAR(20)

    ALTER TABLE students CHANGE COLUMN birth birthday VARCHAR(20) NOT NULL;
    

    要删除列,使用:

    ALTER TABLE students DROP COLUMN birthday;
    

    退出MySQL

    使用EXIT命令退出MySQL:

    mysql> EXIT
    Bye
    

    注意EXIT仅仅断开了客户端和服务器的连接,MySQL服务器仍然继续运行。

  • 相关阅读:
    MSSQL2005和Access在SQL的某一种写法上的区别。update的一种写法不一致。
    博客园 记录 了解多一点
    马克斯4.0 采集规则的编写
    谷歌代码托管 GoogleCode中 关于 版本的一个写法
    晒晒名企大公司的工资收入
    Asp.net中DataBinder.Eval用法的总结
    Mastering Debugging in Visual Studio 2010 A Beginner's Guide
    Solution Configuration but not Platform in VS2010 Toolbar
    window.showdialog完全手册,解决模态窗口,传值和返回值问题
    从此不再惧怕URI编码:JavaScript及C# URI编码详解
  • 原文地址:https://www.cnblogs.com/tangbohu2008/p/11165711.html
Copyright © 2020-2023  润新知