mysql数据库的最基本的操作

MySQL数据库的注释方式

  • 单行注释 –
  • 多行注释 /* … */

MySQL-操作数据库

数据库(database)、是用来进行数据存储的一个仓库(容器),内部由 表(table)、视图(view)、索引(index),
存储过程(procedure)、函数(function)、和触发器(trigger)等组成

数据库Database的基本操作

  • 创建一个数据库
create database 数据库名字 ;

create database if not exists 数据库名;

  • 删除数据库
drop database 数据库名 ;

drop database if exists 数据库名 ;
  • 查看当前数据库管理系统下有哪些数据库
show databases ;

  • 切换数据库
use 数据库名 ;

MySQL数据库中常见的数据类型

  • 数字类型

    • 整数类型: tinyint , smallint , int/integer
    • 浮点类型: float , double
    • decimal
  • 字符串类型(必须设置字符串的允许的最大长度)

    • char : 定长字符串
    • varchar: 可变长度字符串
    • text/longtext : 适合存储大字符串,在使用的时候不需要指定长度
  • 日期类型

    • date : 日期,用来存储年月日
    • datetime : 用来存储年月日,时分秒,微妙
    • time : 时间 , 用来存储时分秒
  • 布尔类型

    • boolean : 使用 1 和 0 代表 true/false
  • Blob类型(流的存储)

    • blob
    • longblob

表的基本操作

表(table)使用进行数据存储的容器、表由字段(column) 和 记录 组成

表的创建

create table 表名(
   字段名 字段类型 [约束] [注释] ,
   字段名 字段类型 [约束] [注释] ,
   字段名 字段类型 [约束] [注释] ,
   ...
);

表的删除

drop table 表名 ;

修改表的结构 alter

  • 新增字段 add
alter table 表名 add [column] 字段名 字段类型 [约束] [注释] ;

  • 修改字段类型 modify

alter table 表名 modify  字段名 字段类型 [注释] ;

  • 修改字段名称 change
alter table 表名 change oldColumn newColumn 类型 【注释】

  • 删除字段名 drop column
alter table 表名 drop column 字段名 ;

  • 修改表名
alter table 表名 rename to 新表名 ;

rename table 表名 to 新表名 ;

  • 查看表结构
desc 表名 ;

describe 表名 ;

查看当前库下所有的表

show tables ;

show full tables;

-- 查看 建表语句
show create table 表名 ;

猜你喜欢

转载自blog.csdn.net/qq_40679091/article/details/109129526