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;
相关推荐
Elastic 中国社区官方博客8 分钟前
Elasticsearch percolator 用于电商搜索治理:将模糊查询转换为可控的检索策略
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
zxrhhm12 分钟前
PostgreSQL 中的层级查询 Oracle CONNECT BY 替代方案
数据库·postgresql·oracle
万事大吉CC1 小时前
【3】深入剖析 Django 之 MTV:路径引用与资源加载机制
数据库·django·sqlite
Hical_W1 小时前
用 Hical + MySQL 5 分钟搭建 CRUD API(C++20 协程版)
数据库·mysql·c++20
AIMath~1 小时前
agent上下文和模型的上下文区别
数据库
与遨游于天地1 小时前
分布式锁从Redis到Redisson的演进
数据库·redis·分布式
想唱rap1 小时前
传输层协议之UDP
java·linux·网络·c++·网络协议·mysql·udp
山峰哥2 小时前
SQL性能提升20倍的秘密:这些优化技巧让DBA都惊叹
开发语言·数据库·sql·编辑器·深度优先·宽度优先
猪脚踏浪2 小时前
mysql 用户授权
mysql