JavaWeb开发-06-SpringBootWeb-MySQL

一.MySQL概述

1.安装、配置

官网下载地址:https://dev.mysql.com/downloads/mysql/

2.数据模型

3.SQL简介

二.数据库设计-DDL

1.数据库

官网:http:// https://www.jetbrains.com/zh-cn/datagrip/

2.表(创建、查询、修改、删除)

sql 复制代码
# 查询所有数据库
show databases;

# 创建数据库
create database db02;

# 删除数据库
drop database db02;

# DDL : 表结构
# 创建 : 基础语法
-- auto_increment 自增
create table tb_user(
    id int primary key auto_increment comment 'ID 唯一标识',
    username varchar(20) not null unique comment '用户名',
    name varchar(10) not null comment '姓名',
    age int comment '年龄',
    gander char(1) default '男' comment '性别'
) comment '用户表';
sql 复制代码
-- DDL: 查看表结构
-- 查看: 当前数据库下的表
show tables;

-- 查看: 查看指定表结构
desc table_emp;

-- 查看: 数据库的建表语句
show create table table_emp;
sql 复制代码
# DDl : 修改表结构

# 修改 : 为表 tb_emp 添加字段 qq varchar(11)
alter table table_emp add qq varchar(11) comment 'QQ';

# 修改 : 修改 tb_emp 字段类型 qq varchar(13)
alter table table_emp modify qq varchar(13) comment 'QQ';

# 修改 : 修改 tb_emp 字段名 qq 为 qq_num varchar(13)
alter table table_emp change qq qq_num varchar(13) comment 'QQ';

# 修改 : 删除 tb_emp 的 qq 字段
alter table table_emp drop qq_num;

# 修改 : 将 tb_emp 表名修改为 emp
rename table table_emp to emp;

# 修改 : 将 emp 表名修改为 tb_emp
rename table emp to table_emp;
sql 复制代码
-- DDL : 删除表结构
-- 删除 : 删除 table_emp 表
drop table if exists table_emp;

-- 表备份
-- auto-generated definition
create table table_emp
(
    id          int auto_increment comment '主键ID' primary key,
    username    varchar(20)                  not null comment '用户名',
    password    varchar(32) default '123456' null comment '密码',
    name        varchar(10)                  not null comment '姓名',
    gander      tinyint unsigned             not null comment '性别:1-男,2-女',
    image       varchar(300)                 null comment '图像url',
    job         tinyint unsigned             null comment '职位:1 班主任, 2 讲师, 3 学工主管, 4 教研',
    entryDate   date                         null comment '入职日期',
    create_time datetime                     not null comment '创建时间',
    update_time datetime                     not null comment '修改时间',
    qq_num      varchar(13)                  null comment 'QQ',
    constraint table_emp_username_uindex
        unique (username)
)
    comment '员工表';

三.数据库操作-DML

sql 复制代码
-- DML : 数据操作语言

-- DML : 插入数据 - insert
-- 1.为 tb_emp 表的username, name, gander 字段插入值
insert into tb_emp(username, name, gander, create_time, update_time) values ('ikun', '坤坤', '1', now(), now());

-- 2.为 tb_emp 表的 所有字段插入新的值
insert into tb_emp(id, username, password, name, gander, image, job, entryDate, create_time, update_time, qq_num)
values (null, 'hei', '1234', '黑子', 2, '1.jpg', 1, now(), now(), now(), '1123456789');

-- 简化
insert into tb_emp values (null, 'ganMa', '12345', '黑子', 2, '1.jpg', 2, now(), now(), now(), '1123456780');

-- 3.批量为 tb_emp 表的 username, name, gander 字段插入数据
insert into tb_emp(username, name, gander, create_time, update_time) values ('xiaoMing',  '小明', 2, now(), now()), ('xiaoHong', '小红', 2, now(), now());
sql 复制代码
-- DML : 更新数据 - update
-- 1.将 tb_emp 表的 ID 为 1 的员工姓名name 字段更新为 '张三'
update tb_emp set name = '张三', update_time = now() where id = '1';

-- 2.将 tb_emp 表的所有员工的入职日期跟新为 '2010-01-01'
update tb_emp set entryDate = '2023-01-02', update_time = now() where 1 = 1;
sql 复制代码
-- DML : 删除数据 - delete
-- 1. 删除 tb_emp 表中 ID 为1的员工
delete from  tb_emp where id = '1';

-- 2. 删除 tb_emp 表中的所有员工
delete from  tb_emp;
相关推荐
katasea8 小时前
第05章:信创技术栈选型:服务器、操作系统、数据库、中间件适配对比
服务器·数据库·中间件
梓沂8 小时前
记一次 Oracle 测试库自动刷新脚本的踩坑实录
数据库·oracle
2501_931803758 小时前
MySQL 索引核心原理
数据库·mysql
浪潮IT馆8 小时前
Windows 10 安装 PostgreSQL 9.6.24 完整教程
数据库·windows·postgresql
谢亮_vipxieliang9 小时前
一套 Compose 搭起完整开发环境:MySQL + Redis + Nginx
redis·mysql·nginx·docker
Elastic 中国社区官方博客9 小时前
14 个 alerts,1 个 incident:使用 Elasticsearch 中的 ES|QL 衡量 alerting rule 噪声
大数据·运维·数据库·elasticsearch·搜索引擎·全文检索
资深技术分享员9 小时前
遗留系统——把“改不动的老系统“接过来
java·服务器·数据库
AllData公司负责人9 小时前
AllData数据中台物联网实时平台|集成 Apache StreamPipes,MQTT工业物联网数据实时预警实践案例
大数据·数据库·人工智能·物联网·apache·工业物联网·streampipes
天衍四九-10 小时前
Docker Compose企业实战系列(一):LNMP环境一键部署(Nginx\+MySQL\+PHP)
mysql·nginx·docker