MySQL数据库基础(数据库的基本操作、常用的数据类型、表的相关操作)

前言

今天我们将介绍数据库的基本操作、常用的数据类型、表的相关操作


一、数据库的基本操作

1.1 显示当前的数据库

操作代码

bash 复制代码
show databases;

1.2 创建数据库

基本语法:

bash 复制代码
1.
//创建数据库
create database examble;
bash 复制代码
2.
create database if not exists exist examble2;
//如果系统没有 examble2 的数据库,则创建一个名叫 db_test2 的数据库,如果有则不创建
bash 复制代码
3.
create database if not exists exist examble2 character set utf8mb4;
//如果系统没有 db_test 的数据库,则创建一个使用utf8mb4字符集的 db_test 数据库,如果有则不创建

create表示关键字

character set :指定数据库采用的字符集collate:指定数据库字符集的校验规则

MySQL的utf8编码不是真正的utf8,没有包含某些复杂的中文字符。MySQL真正的utf8是使用utf8mb4,建议大家都使用utf8mb4。

1.3 使用数据库

语法:

bash 复制代码
use 数据库名

1.4删除数据库

基本语法:

bash 复制代码
drop database [if exists]   数据库名;

注意:

数据库删除以后,内部看不到对应的数据库,里边的表和数据全部被删除

二、常用数据类型

2.1 数值类型

2.2字符串类型和日期类型

三、表的操作

需要操作数据库中的表时,需要先使用该数据库

bash 复制代码
use 数据库名称

3.1查看表结构

bash 复制代码
desc 表名称

比如查询到的内容

3.2 创建表

bash 复制代码
create table table_name(
	field1 datatype,
	field2 datatype,
	field3 datatype
);

比如我们创建一个学生表

bash 复制代码
create table student_test(
	id int,
	name varchar(20) comment'姓名' //可以使用comment增加字段说明。
	password varchar(50) comment '密码'
	age int,
	sex varchar(1)
	birthday timestampamout decimal(13,2)
	resume test
	);

3.2 删除表

bash 复制代码
//语法
drop table 表名;

参考代码

bash 复制代码
-- 删除 stu_test 表
	drop table stu_test;
-- 如果存在 stu_test 表,则删除 stu_test 表
	drop table if exists stu_test;

总结

今天我们将介绍数据库的基本操作、常用的数据类型、表的相关操作,相关重点如下:
1.操作数据库:

-- 显示

show databases;

-- 创建

create database xxx;

-- 使用

use xxx;

-- 删除

drop database xxx;

2.常用数据类型:

INT:整型

DECIMAL(M, D):浮点数类型

VARCHAR(SIZE): 字符串类型

TIMESTAMP:日期类型

3.表的相关操作

-- 查看 show 表;

-- 创建 create table 表名(字段1 类型1, 字段2 类型2,... );

--删除 drop talbe 表名;

相关推荐
嘟嘟w1 天前
DROP DELETE 和TRUNCATE的区别?
数据库·mysql·oracle
Navicat中国1 天前
Navicat x 达梦技术指引 | 数据字典
数据库·达梦·可视化·navicat·数据字典
running up1 天前
Spring核心深度解析:AOP与事务管理(TX)全指南
java·数据库·spring
一水鉴天1 天前
整体设计 定稿 之6 完整设计文档讨论及定稿 之1(豆包周助手)
java·前端·数据库
倔强的石头_1 天前
金仓数据库(KingbaseES) 开发实战:常见迁移挑战与技术解析
数据库
编程小白gogogo1 天前
苍穹外卖后端环境搭建-数据库环境搭建
数据库
星哥说事1 天前
基于 MySQL Monitoring and Management 的性能保障实践
数据库·mysql
此生只爱蛋1 天前
【Redis】列表List类型
数据库·redis·缓存
Jaising6661 天前
Mybatis Plus 主键生成器实现思路分析
数据库·spring boot·mybatis
程序喵大人1 天前
SQLITE问题整理
开发语言·数据库·c++·sqlite