问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

MySQL数据表基本操作

创作时间:
作者:
@小白创作中心

MySQL数据表基本操作

引用
1
来源
1.
https://www.bilibili.com/read/mobile?id=34333512

其实在数据库中创建数据库 和 创建Excel非常类似,需要指定: 表名、 列名称、 类类型(整型、字符串或其他)。

进入数据库day20,新建成绩表score

表结构图

列名 也叫字段名 包括 :id ,name,python

创建表命令

create table 表名(  
列名 类型,  
列名 类型,  
列名 类型  
)default charset=utf8;  

create table score(  
id int,  
name varchar(16),  
python decimal  
)default charset=utf8;  

查看表是否建成功

show tables;  

+-----------------+  
| Tables_in_day20 |  
+-----------------+  
| score |  
+-----------------+  

查看表结构 ----看表的列名(字段名)

desc score;  

修改列类型

alter table 表名 modify column 列名 类型;  

将id修改为主键  

alter table score modify column id int not null primary key;  

查看表结构 是否修改成功

desc score;  

表结构规划好后,就可以添加数据

insert into 表名(列名1,列名2,列名3) values(数值1,数值2,数值3);  

insert into score (id,name,python) values(1,'alex',70);  

查看数据是否添加成功

select * from score;  

多行数据添加方法

insert into score(id,name,python) values(2,'kelly',80),  
(3,'李明',50),  
(4,'张洁',90),  
(5,'李林',65);  

再次查看数据表,检查数据是否添加成功

select * from score;  

查看表里不及格的人的id,name 分数

select * from score where python<60;  

根据分数从高到低排序

select * from score order by python desc;  

查看成绩最高的同学的id ,name, 成绩 不考虑并列第一;

select * from score order by python desc limit 1;  

求平均分

select avg(python) from score;  

求平均分 并将列名重命名为

avg_py select avg(python) as avg_py from score;  

求总分重命名为

sum_py select sum(python) as sum_py from score;  

最高分 ,最低分

select max(python) as max_py ,min(python) as min_py from score;  

最高分的同学的id,名字,分数

select * from score where python=(select max(python) from score);  

姓李同学的id,name,分数

select * from score where name like '李%';  
© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号