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

2.27-1笔记1

一、新建表

图片

二、建表语句

create table student( id int primary key , name char(20), sex char(10), age int(3), mobile char(20), class char(10), english int(10), chinese int(10), math int(10) )engine=innodb default charset=utf8; insert into student values (1,'小红','女',23,'13813828824','1719',77,88,98), (2,'小明','男',23,'13713713711','1720',56,66,55), (3,'小李','男',23,'15915913911','1719',78,64,87), (4,'小张','男',23,'15915913912','1720',77,76,77), (5,'小白','女',24,'15915913913','1719',90,89,98), (6,'小陈','女',19,'15915913914','1719',84,100,81), (7,'小钱','女',20,'15915913915',null,45,99,93);

三、操作语句

1、表结构的语句

(1)add  添加字段

格式:ALTER  table   表名  add   字段名  字符类型(字符长度);

案例:ALTER  table   student  add   dcs  int(10);

图片

(2)change 修改字段

格式:ALTER  table   表名  change  源字段名   新字段名  字符类型(字符长度);

案例:ALTER  table   student  change  dcs   hzdcs  int(10);

图片

(3)drop 删除字段

格式:ALTER  table    表名  drop   字段名;

案例:ALTER  table    student  drop   hzdcs ;

图片

(4)rename 修改表名

格式:ALTER  table     表名  rename  新表名 ;

案例:ALTER  table     student  rename  student2 ;

图片

(5)modify     after   字段的调换

格式:

alter  table   表名  modify  源字段名  字符类型(字符长度)  after  表中字段名 ; 

案例:

alter  table   student2  modify  math int(10)  after  name ; 

图片

(6)first  添加字段到第一位

格式:

ALTER  table   表名    add  新字段名  字符类型(字符长度)  first ; 

案例:

ALTER  table   student2    add  no  int(10)  first ; 

图片

2.27-1课堂笔记:

-- select * from student;#查询内容

-- ALTER table student add dcs int(10);#最后添加字段dcs

-- alter table student change dcs hzdcs int(10);#字段改名

-- alter table student drop dsc;#删除字段

-- alter table student rename stu;#改表名
-- select * from stu;
-- alter table stu rename student;
-- alter table student rename s;

-- desc student;#查看字段
-- alter table student modify math int(10) after name;
-- alter table student modify math int(10) after class;
#把字段math移到某字段后面


-- alter table student add num int(10) first;
#在最前面加字段

-- select * from student;
-- select * from s;#查看全部

-- select name as "姓名",math "数学" from s;
#显示某些字段,as可省略


#####查询
-- select name,math,english,chinese from s 
--      where math>80 and english>80 and chinese>80;
#条件查询where

-- alter table s change math m int(10);#改字段名
-- alter table s change english e int(10);
-- alter table s change chinese c int(10);

-- select * from s where id between 2 and 5;
#查id在2到5之间的between

-- select * from s where id in (2,6,8);
#查id包含在2或6或8的

-- select * from s where id not in (2,6,8);
#查id不包含在2或6或8的

-- select * from s where class is null;
#查含空值的

######排序
-- select * from s order by m asc;#升序
-- select * from s order by m desc;#降序

-- select * from s order by age asc,m desc,c desc,e desc;
#多次排序

-- select * from s where m like "8%";#8开头
-- select * from s where m like "%8%";#含8
-- select * from s where m like "%8";#8结尾
-- select * from s where m like "8_";#查看80几
-- select * from s where m like "8__";#查看800多

-- select * from s limit 0,3;#从索引第一个开始取3个数据
-- select * from s limit 4;#索引前4行
#索引=行数-1

-- select max(m) from s;#数学最大值
-- select min(m) from s;#数学最小值
-- select sum(m) from s;#数学求和
-- select avg(m) from s;#数学平均值
-- select count(m) from s;#数学统计个数
-- select DISTINCT(m) from s;#数学去重

-- select class,avg(m) from s group by class;
-- select class,avg(m) from s group by class HAVING m<80;
#求每个班平均数小于80

-- UPDATE s set name="小红帽" where id=1;
-- UPDATE s set name="小红" where id=1;
#将id=1的人name字段改成小红帽
-- select * from s;

######删除
#方法1
-- delete from s where id=100;#删除id=100的数据
#方法2
-- -- truncate sb;#连表带数据全部删掉
#方法3
#drop(前面已讲)
#drop>truncate>delete


#######注释
#ctrl+/注释
#shift+ctrl+/取消注释


#备份
-- create table sb like s;#备份表结构,不复制数据
#备份部分数
#insert into sb(id,age) select id,age from s;
#备份全部数据
#truncate sb;
-- select * from sb;


 

相关文章:

  • 【Vue3 Teleport 技术解析:破解弹窗吸附与滚动列表的布局困局】
  • 初阶数据结构(C语言实现)——3顺序表和链表(2)
  • linux--多进程开发(6)IPC之内存映射
  • thinkphp下的Job队列处理
  • 网络运维学习笔记(DeepSeek优化版)006网工初级(HCIA-Datacom与CCNA-EI)VLAN间路由
  • Android手机部署DeepSeek
  • C# Json序列化的常用几种方式
  • 教你通过腾讯云AI代码助手,免费使用满血版deepseek r1,还可以自定义知识库!
  • AF3 pair_sequences函数解读
  • 2月27(信息差)
  • Spock框架:让单元测试更优雅的高效武器
  • 【nextjs官方demo】Chapter 6连接数据库报错
  • docker 运行claude 的computer use
  • Linux驱动学习(四)--字符设备注册
  • MySQL练习
  • AI人工智能机器学习之降维和数据压缩
  • 基于W2605C语音识别合成芯片的智能语音交互闹钟方案-AI对话享受智能生活
  • lvgl运行机制分析
  • 车载以太网-基于linux的ICMP协议
  • 决策树(Decision Tree)详细解释(带示例)
  • erp系统界面/首页关键词优化公司
  • 本地拖拽网站建设/公司管理培训课程大全
  • 河南新蔡有做网站建设的吗/百度快速提交入口
  • 丹阳做网站/外贸seo优化公司
  • 免费模版网站/网络开发
  • 个人可以注册商标吗/十堰seo排名公司