当前位置: 首页 > news >正文

Java全栈学习笔记31

create database testdb;

use testdb;

/*create table [if not exists] tableName(

    列名 数据类型(长度),

    列名2 数据类型(长度)

);

在同一个数据库中 表的名字不能重复

*/

-- orm

-- 字符类型

/*

  char[(长度)] ,长度可以不设置,默认为1

*/

-- 删除表 drop table [if exists] tableName

-- drop table t_student;

-- create table if not exists t_student(

--   studId char(5),

--   studName char(30),

--   description char(256)

-- );

-- select length(studId) from t_student;

-- varchar  0-16383  必须要指定长度,为变长字符串

-- drop table if exists t_student;

-- create table if not exists t_student(

--   studId char,

--   studName varchar(30)

-- );

select length(studId) from t_student;

-- text 大文本 blob 二进制文件   

drop table if exists t_student;

create table if not exists t_student(

  studId char,

  studName varchar(30),

  description LONGTEXT

);

-- enum 列举项,该字段内容只能是列举项当中的内容,底层存储的是数字

-- drop table if exists t_student;

-- create table if not exists t_student(

--   studId char,

--   studName varchar(30),

--   sex enum("男","女")

-- );

-- insert into t_student values(1,"张三","男");

-- insert into t_student values(2,"李四","女");

-- insert into t_student values(3,"王五",2);

-- insert into t_student values(4,"张四",1);

-- select * from t_student where sex = '男';

-- set (列表项1,....)

-- 允许为0,0 为空字符串

-- drop table if exists t_student;

-- create table if not exists t_student(

--   studId char,

--   studName varchar(30),

--   sex enum("男","女"),

--   hobbies set("运动","学习","睡觉")

-- );

-- insert into t_student values(1,"张三1",1,"运动");

-- insert into t_student values(2,"张三2",1,"运动,学习");

-- insert into t_student values(3,"张三3",1,2);

-- insert into t_student values(4,"张三4",2,3);

-- insert into t_student values(5,"张三5",2,4);

-- insert into t_student values(5,"张三6",1,6);

-- insert into t_student values(6,"张三7",1,0);

/*

  睡觉 学习 运动

   1   0   0   100  4

   0   1   0   010  2

   1   1   0   110  6

   

*/

select * from t_student where hobbies = 6;

select * from t_student where hobbies is null;

-- drop table if exists t_student;

-- create table if not exists t_student(

--   studId char,

--   studName varchar(30) binary

-- );

--

-- insert into t_student values(1,"Kobe");

-- insert into t_student values(2,"JAMES");

-- select * from t_student where studName = "JAMES";

-- drop table if exists t_student;

-- create table if not exists t_student(

--   studId int(5),

--   studName varchar(30) binary,

--   score dec(5,2),

--   birthdate date

-- );

-- insert into t_student values(1,"张三8",100,"2025-09-8");

-- 只有datetime有默认值。可以使用current_timestamp/now()作为日期时间的默认值

-- 默认值是在没有插入该列的时候使用。

-- drop table if exists t_student;

-- create table if not exists t_student(

--   studId int(5),

--   studName varchar(30) binary,

--   score dec(5,2),

--   birthdate datetime default now()

-- );

-- insert into t_student values(1,"张三8",100,null);

-- drop table if exists t_student;

-- create table if not exists t_student(

--   studId tinyint(5),

--   studName varchar(30) binary,

--   score dec(5,2),

--   birthdate datetime default now(),

--   isAdmin boolean

-- );

-- 在tinyint 最后转的也是bool

-- auto_increment 在int类型列可以设置为自增,自增列必须为主键

-- 主键:该列为数据表中的唯一列。通常用主键唯一表示一行  primary key

drop table if exists t_student;

create table if not exists t_student(

  studId int auto_increment primary key,

  studName varchar(30) binary,

  score dec(5,2),

  birthdate datetime default now(),

  isAdmin boolean

);

insert into t_student values(null,"李四",100,"2025-09-08 14:00:00",0)

insert into t_student(studName,score,birthdate,isAdmin) values("李四",100,"2025-09-08 14:00:00",0)

insert into t_student values(10,"李四",100,"2025-09-08 14:00:00",0)

insert into t_student values(null,"李四",100,"2025-09-08 14:00:00",0);

select LAST_INSERT_ID()

drop table t_student;

create table t_student(

  studId int auto_increment primary key,

  studName varchar(30),

  sex enum('男','女'),

  birthdate date,

  ingrade dec(3,2),

  hobbies set('运动',"学习","睡觉","刷痘印","玩儿文玩","打台球"),

  descr text

)

-- 修改表的结构

-- 新增列  alter table 表名 add [column] 列名 数据类型

alter table t_student add column createTime datetime default current_timestamp;

alter table t_student add age int;

-- 修改列的数据类型  alter table 表名 modify 列名 新数据类型

-- 如果表中该列存在数据,那么在修改时要注意 修改有的数据类型和表中存的数据的类型要兼容

alter table t_student modify ingrade dec(5,2);

alter table t_student modify age double;

alter table t_student modify studName varchar(30);

alter table t_student modify createTime datetime default CURRENT_TIMESTAMP;

-- 删除列  alter table 表名 drop 列名

alter table t_student drop descr;

-- 修改列的名字 alter table t_student change 旧列名 新列名 数据类型

alter table t_student change studage studAge int;

-- 修改表的名字 alter table 表名 rename 新表名

alter table t_student rename t_stu;

show tables from mysql; -- 查看当前有哪些表

alter table t_stu modify createTime datetime default CURRENT_TIMESTAMP;

-- DML crud

-- c create insert into 表名[(列名...)] values(值...);

insert into t_stu values(null,"李四",11,"女","2014-09-08",500,"学习");

insert into t_stu(studAge,sex,studName,birthdate,ingrade,hobbies)

values(13,'女',"王五","2012-09-08",300,"睡觉");

insert into t_stu(studAge,sex,studName,birthdate,ingrade,hobbies)

value(13,'女',"赵六","2012-09-08",300,"睡觉")

insert into t_stu(studId,studAge,sex,studName,birthdate,ingrade,hobbies)

values(1,13,'女',"李一","2012-09-08",300,"睡觉"),

(2,13,'女',"王一","2012-09-08",300,"睡觉"),

(3,13,'男',"赵二","2012-09-08",300,"睡觉"),

(4,13,'男',"叶三","2012-09-08",300,"睡觉");

-- r  read select *(列名,....) from 表名

-- * 查询表中的所有列

select * from t_stu;

select studAge,studName,sex,hobbies from t_stu;

-- u update  更新  update 表名 set 列名 = 值... where

update t_stu set studAge = 11;

update t_stu set studAge = 13 where sex = "男";

update t_stu set studAge = 12,ingrade = 400 where studId = 1

-- d delete 删除  删除表中数据  delete from 表名 where

delete from t_stu where studId = 14;

delete from t_stu where studId in(2,3);

-- 清空表

truncate t_stu

create table t_admin(

  id int auto_increment primary key,

  admin_account varchar(10),

  admin_pwd varchar(50),

  create_time datetime default now()

);

-- 批量数据的生成。检索数据。定时任务

delimiter //

create PROCEDURE add_admin()

BEGIN

  DECLARE i int default 1;

  while i < 1001 do

    insert into t_admin(admin_account,admin_pwd) values(concat('admin_',i),'123456');

    

  end while;

end //

delimiter ;

-- 调用存储过程

call add_admin();

-- 删除存储过程

drop PROCEDURE add_admin;

-- 实现完整性的方式,利用为表中的列添加约束

-- not null 非空约束.该列在没有默认值的情况下,且没有设置值是不允许的。

-- 添加约束。约束的删除

-- 创建表的过程添加非空约束

drop table t_stu;

create table t_stu(

  studId int not null,

  studName varchar(30) not null,

  sex char(1)

);

insert into t_stu values(1,"张三",'男');

-- 修改表的数据类型方式添加非空约束,注意表中的数据是否合理(兼容性,约束)

alter table t_stu modify sex char(1) not null

insert into t_stu values(2,"李四","女");

alter table t_stu add age int;

alter table t_stu modify age int not null;

-- default  默认值约束,当不为该列设置值时,可以存在一个默认值。如果设置为null,则不使用默认值

drop table t_stu;

create table t_stu(

  studId int not null,

  studName varchar(30) not null,

  hobbies varchar(40) default '运动',

  studAge int,

  birthdate date,

  sex char(1),

  createTime datetime default now(),

  updateTime datetime default now() on update now()

);

insert into t_stu(studId,studName,hobbies,studAge,sex) values(1,"张三",null,12,"男");

insert into t_stu(studId,studName,hobbies,studAge,sex) values(1,"李四",null,12,"男");

alter table t_stu drop sex;

-- 修改表的数据类型的方式添加默认值约束

alter table t_stu add sex char(1) not null default '男'

alter table t_stu drop ingrade;

-- 如果添加列的时候,没有设置默认值,但是设置了非空,那么会默认赋予一个默认值。

alter table t_stu add ingrade varchar(30) not null;

-- on update 更新级联  可以在修改该行数据时,时间随之更改

alter table t_stu modify birthdate datetime default now() on update current_timestamp;

update t_stu set hobbies = "睡觉" where studId = 1

-- drop table t_stu

-- 体现实体完整性

-- 主键  primary key  唯一的标识一行。

/*

  主键列不允许为null,不允许重复。

  每个表只能有一个主键(每个表中只能出现一次primary key关键字)

  可以作用在一列或者多列上

*/

-- 可以主键约束设置在列上

-- create table t_stu(

--   studId int primary key,

--   studName varchar(30),

--   studSex char(1)

-- )

-- insert into t_stu values(1,"张三","男");

-- insert into t_stu values(null,"李四","男");

-- 声明注解约束在列的最后

-- drop table t_stu;

-- create table t_stu(

--   studId int,

--   studName varchar(30),

--   studSex char(1),

--   primary key(studId)

-- )

-- insert into t_stu values(1,"张三","男");

-- insert into t_stu values(1,"李四","男");

-- 修改表的方式添加注解约束

drop table t_stu;

-- create table t_stu(

--   studId int,

--   studName varchar(30),

--   studSex char(1),

--   constraint `pk_studId` primary key(studId)

-- )

create table t_stu(

  studId int,

  studName varchar(30),

  studSex char(1)

)

alter table t_stu add constraint `pk_studId` primary key(studId)

insert into t_stu values(1,"张三","男");

insert into t_stu values(1,"李四","男");

insert into t_stu values(1,"张三","女");

alter table t_stu add constraint primary key(studName,studSex);

alter table t_stu modify studId int primary key;


文章转载自:

http://1PP1ZoFY.qzfjL.cn
http://QgjzESmS.qzfjL.cn
http://I0DnOnow.qzfjL.cn
http://McNcXVLj.qzfjL.cn
http://ehwbzppn.qzfjL.cn
http://NZB5OWGw.qzfjL.cn
http://cCrfeRvg.qzfjL.cn
http://rFON1ErH.qzfjL.cn
http://opIRruv6.qzfjL.cn
http://OxkM84RY.qzfjL.cn
http://N562b7f6.qzfjL.cn
http://vtG0xJrt.qzfjL.cn
http://lua0jWlR.qzfjL.cn
http://OHtuTBkE.qzfjL.cn
http://zUuLmfXY.qzfjL.cn
http://Rp2jir5k.qzfjL.cn
http://Xz0BKRpt.qzfjL.cn
http://0DaVepeI.qzfjL.cn
http://iowcLVS9.qzfjL.cn
http://jafxe4Zr.qzfjL.cn
http://wZ5yoUBe.qzfjL.cn
http://DTpbfutn.qzfjL.cn
http://RGbpgSDP.qzfjL.cn
http://op67taNB.qzfjL.cn
http://ruDtyXbu.qzfjL.cn
http://MDyDVw11.qzfjL.cn
http://QjVzAHPQ.qzfjL.cn
http://NaYItrG5.qzfjL.cn
http://WJNHTOJE.qzfjL.cn
http://F9BRE5Hj.qzfjL.cn
http://www.dtcms.com/a/374284.html

相关文章:

  • 算法之双指针
  • js定义变量时let和cons的使用场景
  • DataLens:一款现代化的开源数据分析和可视化工具
  • 人工智能-python-深度学习-神经网络-MobileNet V1V2
  • TDengine 选择函数 Last() 用户手册
  • MySQL的数据模型
  • vulnhub:Kioptrix level 2
  • C++ Int128 —— 128位有符号整数类实现剖析
  • 前端部署,又有新花样?
  • Neural Jacobian Field学习笔记 - omegaconf
  • C++(day8)
  • 设计模式:模板方法模式
  • 英发睿能闯关上市:业绩波动明显,毅达创投退出,临场“移民”
  • 华清远见25072班网络编程day1
  • 深入理解 AbstractQueuedSynchronizer (AQS):Java 并发的排队管家
  • 32位CPU架构是如何完成两数(32位)相加的指令的?
  • 深度学习中的损失函数都有哪些,大模型时代主要用的损失函数有哪些,中间有什么区别?
  • java:io流相关类的继承关系梳理
  • PAT 1004 Counting Leaves
  • Linux操作系统shell脚本语言-第六章
  • 基于Springboot + vue3实现的小区物业管理系统
  • 自动化测试DroidRun
  • 把一段 JSON 字符串还原成一个实体对象
  • YOLO系列论文梳理(AI版)
  • ARM内核知识概念
  • 图论相关经典题目练习及详解
  • 深圳比斯特|多维度分选:圆柱电池品质管控的自动化解决方案
  • MySQL 日志全解析:Binlog/Redo/Undo 等 5 类关键日志的配置、作用与最佳实践
  • 龙虎榜——20250908
  • 自噬机制解析(二):一文厘清 LC3/Atg8 概念及实验应用要点