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 表名;

相关推荐
长征coder5 分钟前
AWS MySQL 读写分离配置指南
mysql·云计算·aws
醇醛酸醚酮酯20 分钟前
Qt项目锻炼——TODO清单(二)
开发语言·数据库·qt
ladymorgana40 分钟前
【docker】修改 MySQL 密码后 Navicat 仍能用原密码连接
mysql·adb·docker
PanZonghui44 分钟前
Centos项目部署之安装数据库MySQL8
linux·后端·mysql
GreatSQL社区1 小时前
用systemd管理GreatSQL服务详解
数据库·mysql·greatsql
掘根1 小时前
【MySQL进阶】错误日志,二进制日志,mysql系统库
数据库·mysql
weixin_438335401 小时前
基础知识:mysql-connector-j依赖
数据库·mysql
小明铭同学1 小时前
MySQL 八股文【持续更新ing】
数据库·mysql
Mr_Xuhhh1 小时前
信号与槽的总结
java·开发语言·数据库·c++·qt·系统架构
Fireworkitte2 小时前
Redis 源码 tar 包安装 Redis 哨兵模式(Sentinel)
数据库·redis·sentinel