MySQL数据库篇之库的增删改查

主要内容:

  一、系统数据库介绍

  二、创建数据库

  三、数据库增删改查

   四、MySQL添加注释

1️⃣ 系统数据库介绍

  1、初识sql语句

    有了mysql这个数据库软件,就可以将程序员从对数据的管理中解脱出来,专注于对程序逻辑的编写

  mysql服务端软件即mysqld帮我们管理好文件夹以及文件,前提是作为使用者的我们,需要下载mysql的客户端,

  或者其他模块来连接到mysqld,然后使用mysql软件规定的语法格式去提交自己命令,实现对文件夹或文件的管理。

  该语法即sql(Structured Query Language 即结构化查询语言)

SQL语言主要用于存取数据、查询数据、更新数据和管理关系数据库系统,SQL语言由IBM开发。SQL语言分为3种类型:
1、DDL语句 数据库定义语言: 数据库、表、视图、索引、存储过程,例如CREATE DROP ALTER 2、DML语句 数据库操纵语言: 插入数据INSERT、删除数据DELETE、更新数据UPDATE、查询数据SELECT 3、DCL语句 数据库控制语言: 例如控制用户的访问权限GRANT、REVOKE

  2、MySQL数据库登录后,查看数据库的初始数据库(show databases; ),一般会有以下几个库文件:

information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息、列信息、权限信息、字符信息等
performance_schema: MySQL 5.5开始新增一个数据库:主要用于收集数据库服务器性能参数,记录处理查询请求时发生的各种事件、锁等现象
mysql: 授权库,主要存储系统用户的权限信息
test: MySQL数据库系统自动创建的测试数据库

2️⃣ 创建数据库

  1、语法   

create database 数据库 charset utf8;

  2、数据库命名规则

可以由字母、数字、下划线、@、#、$
区分大小写
唯一性
不能使用关键字如 create select
不能单独使用数字
最长128位

3️⃣ 数据库相关操作 

SQL语句:
操作文件夹(库)
    增  create database db1 charset utf8;

    查  show create database db1;
        查看所有数据库(show databases;)
    改
        改数据库:alter database db1 charset gbk;
    删
        删数据库:drop database db1;

操作文件
    切换文件夹:use db1;

    增 :create table t1(id int,name char);

    查:
        查看当前所在文件夹:select database();
        查看当前库中所有表 :show tables;
        查看当前表:show create t3G 或者:desc t1;(更直观)

    改:
        alter table t1 modify name char(6);
        alter table t1 change name Name char(7);
    删:
        drop table t1;
操作文件内容(记录): 增:insert t1(id,name) value(
1,'cc'),(2,'cc2'),(3,'cc3'); 查:select id,name from db1.t1; select * from db1.t1; 改:update db1.t1 set name='CC';(表中内容全改) update db1.t1 set name='hyt' where id=2; 删:delete from t1; delete from t1 where id=2;

4️⃣  MySQL添加注释 

  在MySQL数据库中, 字段或列的注释是用属性comment来添加。

  创建新表的脚本中, 可在字段定义脚本中添加comment属性来添加注释。

  1、在新建表的时候添加注释

mysql> create table test(
    -> id int primary key comment '添加注释测试',
    -> name varchar(20)
    -> );
Query OK, 0 rows affected (0.39 sec)

  查看添加的注释的表信息,可使用语句 “show full columns from test;” 

mysql> show full columns from test;
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+--------------------+
| Field | Type        | Collation       | Null | Key | Default | Extra | Privileges                      | Comment            |
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+--------------------+
| id    | int(11)     | NULL            | NO   | PRI | NULL    |       | select,insert,update,references | 添加注释测试       |
| name  | varchar(20) | utf8_general_ci | YES  |     | NULL    |       | select,insert,update,references |                    |
+-------+-------------+-----------------+------+-----+---------+-------+---------------------------------+--------------------+
2 rows in set (0.00 sec)

  

  2、为建好的表添加或修改注释

mysql> alter table test comment'这是测试表';
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

  查看表的的注释(两种方法)

  # 方法一:在生成的SQL语句中看 (show create table 表名G;),实例如下:

mysql> show create table testG;    # G 表示格式化显示
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE `test` (
  `id` int(11) NOT NULL COMMENT '添加注释测试',
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='这是测试表'
1 row in set (0.00 sec)

ERROR:
No query specified
View Code

  # 方法二:在元数据的表里面看(复杂一点)

  过程:

use information_schema; 
select * from tables where table_schema='数据库名' and table_name='表名' G

  实例:

mysql> select * from tables where table_schema='db1' and table_name='test'G;
*************************** 1. row ***************************
  TABLE_CATALOG: def
   TABLE_SCHEMA: db1
     TABLE_NAME: test
     TABLE_TYPE: BASE TABLE
         ENGINE: InnoDB
        VERSION: 10
     ROW_FORMAT: Compact
     TABLE_ROWS: 0
 AVG_ROW_LENGTH: 0
    DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
   INDEX_LENGTH: 0
      DATA_FREE: 0
 AUTO_INCREMENT: NULL
    CREATE_TIME: 2018-06-02 12:15:31
    UPDATE_TIME: NULL
     CHECK_TIME: NULL
TABLE_COLLATION: gbk_chinese_ci
       CHECKSUM: NULL
 CREATE_OPTIONS:
  TABLE_COMMENT: 这是测试表
1 row in set (0.00 sec)

ERROR:
No query specified
View Code

  

  3、为建好的表里的字段添加或修改注释,并查看效果

mysql> alter table test change column id id int not null default 0 comment '测试表id';
Query OK, 0 rows affected (0.17 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show full columns from test;
+-------+-------------+----------------+------+-----+---------+-------+---------------------------------+-------------+
| Field | Type        | Collation      | Null | Key | Default | Extra | Privileges                      | Comment     |
+-------+-------------+----------------+------+-----+---------+-------+---------------------------------+-------------+
| id    | int(11)     | NULL           | NO   | PRI | 0       |       | select,insert,update,references | 测试表id    |
| name  | varchar(20) | gbk_chinese_ci | YES  |     | NULL    |       | select,insert,update,references |             |
+-------+-------------+----------------+------+-----+---------+-------+---------------------------------+-------------+
2 rows in set (0.00 sec)

mysql> alter table test change column id id int not null default 1 comment '修改后的测试表id';
Query OK, 0 rows affected (0.13 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show full columns from test;
+-------+-------------+----------------+------+-----+---------+-------+---------------------------------+-------------------------+
| Field | Type        | Collation      | Null | Key | Default | Extra | Privileges                      | Comment                 |
+-------+-------------+----------------+------+-----+---------+-------+---------------------------------+-------------------------+
| id    | int(11)     | NULL           | NO   | PRI | 1       |       | select,insert,update,references | 修改后的测试表id        |
| name  | varchar(20) | gbk_chinese_ci | YES  |     | NULL    |       | select,insert,update,references |                         |
+-------+-------------+----------------+------+-----+---------+-------+---------------------------------+-------------------------+
2 rows in set (0.02 sec)

  

  

原文地址:https://www.cnblogs.com/schut/p/9055740.html