数据库(黑马)

数据库

复制代码
use test2;
show tables;
create table student2(
    id int,
    name varchar(10),
    sex varchar(10)
);
ALTER TABLE student  ADD sex int;
INSERT INTO student (sex) VALUES ('1');
insert into student(id) values(1),(2),(3);
insert into student2  values(4,'呆呆1',19),(5,'hedfdshe',18),(6,'pdd',13);
delete from student where id=3;
update student  set name='yuanshen' where id=5;
select id,name,age from student ; 
select * from student where id=6 ; 
select sex,avg(id),sum(id) from student2 group by sex ;
select  * from student2 where sex=18 order by id desc ;
select  * from student2 limit 2,3;
select sex,count(*) from student2 where sex > 10 group by sex order by sex ;
​
DDL

层级关系 库>>表>>数据

注释 -- 我是注释 # 我是注释 /* 多行注释 */

库操作

显示有哪些数据库 SHOW DATABASES;

使用数据库 USE 数据库名称;

创建数据库 CREATE DATABASE 数据库名称 [CHARSET UTS-8];

删除数据库 DROP DATABASE 数据库名称;

查看当前使用的数据库SELECT DATABASE();

表操作

首先使用库,才能以此为基础操作表

use 库名;

显示表 show tables;

创建表

复制代码
create table student1(
​
    id int,
​
    name varchar(10),
​
    age int
​
);

删除表 drop table student;

DML数据操作

where 条件判断\]; 可写可不写,不写默认全部 字符用单引号引起来 插入 语法 INSERT 表\[列1,列2,。。。\] VALUES(值1,值2,。。。) insert **into** student (id,name,age) **values**(5,'呆呆',19),(6,'hehe',18) 删除 语法 DELETE FROM 表名称 \[WHERE 条件判断\]; = \< \> \<= \>= != **delete** **from** student **where** id=3; delete from student;删除整张表的内容 更新 语法 UPDATE 表名 SET 列=值 \[WHILE 条件判断\]; ##### DQL 数据查询 select 字段列表\|\* from 表 查询所有列 select id,name,age from student ; select \* from student ; 查询指定列 select \* **from** student **where** id=6 ; ##### 分组聚合 sum求和 avg求平均值 min最小值 max最大值 count求数量 select 字段\|聚合函数 from 表 \[where 条件\] group by 列; 可以写多个聚合函数 select gender,avg(age) from student group by gender; ##### 排序和分页 对查询到的结果进行排序(按照id,以asc升序排列) **select** \* **from** student2 **where** sex=18 **order** **by** id **asc** ; 分页 对查询的结果限制只输出从第2行以后开始,往后输出3行 **select** \* **from** student2 **limit** 2,3; limit在语句最后 #### ####

相关推荐
柠檬叶子C5 分钟前
PostgreSQL 忘记 postgres 密码怎么办?(已解决)
数据库·postgresql
864记忆35 分钟前
Qt创建连接注意事项
数据库·qt·nginx
毕设十刻1 小时前
基于Vue的迅读网上书城22f4d(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
薛定谔的猫19821 小时前
Langchain(十二)LangGraph 实战入门:用流程图思维构建 LLM 工作流
数据库·microsoft
坐吃山猪2 小时前
ChromaDB02-代码实战
数据库·向量数据库·chromadb
摇滚侠2 小时前
MySQL 中 utf8mb4 字符集,字母a占几个字节,一个汉字占几个字节 / MySQL 中 utf8mb3 字符集,字母a占几个字节,一个汉字占几个字节
数据库·mysql
ChineHe2 小时前
Redis数据类型篇001_数据类型梳理与选择指南
数据库·redis·缓存
Antoine-zxt2 小时前
MySQL CPU飙升至500%的深度排查与优化实践
数据库·mysql
齐 飞2 小时前
使用阿里云的MaxCompute查询sql时报错:DruidPooledPreparedStatement: getMaxFieldSize error
sql·阿里云·odps
Awkwardx2 小时前
MySQL数据库—MySQL基本查询
数据库·mysql