MySQL数据库基础

1、数据库的操作

1.1 显示当前的数据库

show databases;

 1.2 创建数据库   

CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,

create_specification] ...]

create_specification:

[DEFAULT] CHARACTER SET charset_name

[DEFAULT] COLLATE collation_name

说明:

大写的表示关键字

[] 是可选项

CHARACTER SET: 指定数据库采用的字符集

COLLATE: 指定数据库字符集的校验规则

 示例:

创建名为test的数据库

create database test;

如果没有test2,则创建,有则不创建

create database if not exists test2;

如果没有test的数据库,则创建一个使用utf8mb4字符集的test数据库,如果有则不创建

create database if not exists character set utf8mb4

1.3 使用数据库

注意:只有在使用指定数据库后,才能操作对应数据库中的表

use 数据库名;

1.4 删除数据库

语法:

DROP DATABASE [IF EXISTS] db_name; 

示例:

drop database test1;

drop database if exists test2;

 本次删除是测试的数据库,大家在工作中谨记,不要随意进行drop操作,如果要进行,也要及时备份数据库,防止出现不可逆的操作。

2、常用的数据类型

2.1 数值类型:分为整型和浮点型

2.2 字符串类型

2.3 日期类型

3、表的操作

操作表时要先使用数据库

3.1 查看表结构

desc 表名;

 3.2 创建表

语法:

CREATE TABLE table_name (

field1 datatype,

field2 datatype,

field3 datatype

);

后面可以加comment增加字段的注释

 示例:

create table test1(id int,

    -> name varchar(20),

    -> password varchar(50) comment '密码',

    -> age int,

    -> sex varchar(1),

    -> birthday timestamp,

    -> amout decimal(13,2),

    -> resume text

    -> );

 3.3 删除表

语法格式:

DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...

示例:

-- 删除 test1 表

drop table test1;

-- 如果存在test1 表,则删除 test1 表

drop table if exists test1 ;

相关推荐

  1. 数据库MySQL数据库基础

    2024-03-25 03:16:03       43 阅读
  2. MySQL数据库基础

    2024-03-25 03:16:03       34 阅读
  3. MySQL数据库基础

    2024-03-25 03:16:03       56 阅读
  4. MySQL数据库基础

    2024-03-25 03:16:03       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-25 03:16:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-25 03:16:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-25 03:16:03       87 阅读
  4. Python语言-面向对象

    2024-03-25 03:16:03       96 阅读

热门阅读

  1. LeetCode的使用方法

    2024-03-25 03:16:03       41 阅读
  2. Vue修饰符总结

    2024-03-25 03:16:03       39 阅读
  3. AcWing 3417.砝码称重

    2024-03-25 03:16:03       45 阅读
  4. qinakun实现公共依赖的加载

    2024-03-25 03:16:03       50 阅读
  5. Git tag总结

    2024-03-25 03:16:03       39 阅读
  6. vscode集成git管理项目

    2024-03-25 03:16:03       37 阅读
  7. PiflowX-Faker组件

    2024-03-25 03:16:03       47 阅读
  8. bind更改this指向问题

    2024-03-25 03:16:03       44 阅读
  9. 三维重建-单目相机标定

    2024-03-25 03:16:03       38 阅读
  10. c语言如何颠倒字符串顺序

    2024-03-25 03:16:03       36 阅读