数据库(黑马)

数据库

复制代码
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在语句最后

相关推荐
我会尽全力 乐观而坚强1 小时前
MySQL库与表的操作
linux·数据库·mysql
lilihuigz1 小时前
产品附加选项插件WooCommerce Product Add-Ons:9种字段类型与3种定价模式提升销售 - 易服客工作室
数据库·mysql·wordpress插件·woocommerce扩展·定制选项
360智汇云2 小时前
Valkey 9.1上线:从Redis兼容到AI数据能力探索
数据库
标致的自行车2 小时前
Entity Framework之问题收集
jvm·数据库·oracle
啵啵鱼爱吃小猫咪2 小时前
Franka机械臂安装实时内核
数据库·postgresql
今天也是元气满满的一天呢3 小时前
从单机架构到负载均衡的互联网架构全流程
数据库·系统架构
SelectDB3 小时前
快手 AB 场景提速 145 倍,从 Spark 到 Apache Doris 的加速实践
数据库·spark·开源
SelectDB3 小时前
阶跃星辰 Agent 可观测实践:为什么 Trace 数据底座选择 SelectDB?
大数据·数据库·agent
冷静的楼房3 小时前
使用ThreadPool发起同步的调用
数据库
一个天蝎座 白勺 程序猿3 小时前
自动SQL优化实战|吃透调优接口+报告配置+统计+索引全流程落地
数据库·sql·sql优化