MENU

我勒数据库笔记

September 29, 2024 • Read: 141 • 学习

请输入图片描述
9.20

insert into student VALUES('张三丰','男',109,'1130-10-03','13134533333');
insert into student VALUES('张无忌','男',18,'1200-6-5','1111122315');
insert into student(NAME,sex) VALUES('张三丰 ','男');
UPDATE student set age=age+1;
UPDATE student set age=age-5 where sex='女';
UPDATE student set age=55,tel='123456789' WHERE name='';

综合练习
1、先把你的数据表清空
DELETE from student;
2、在表中分别插入这几个记录
insert into student VALUES('令狐冲','男',22,'1256-03-12','1122335566');
insert into student(name,sex) VALUES('赵敏','女');
insert into student(name,sex,age,birthday) VALUES('乔峰','男',25,'1115-05-17');
insert into student(name,sex,age,birthday) VALUES('虚竹','男',66,'1015-06-17');
insert into student VALUES('王语嫣','女',18,'1015-06-17','123455555');
SELECT * FROM student;
3、为所有男生的年龄加上2岁
UPDATE student set age=age+2 where sex='男';
SELECT * FROM student;
4、把18岁的那个女生姓名改为“阿紫”
UPDATE student set name='阿紫' where sex='女';
SELECT * FROM student;
5、给赵敏完善年龄、出生日期、电话信息为“21,1246-05-24,133323232”
UPDATE student set age=21,birthday='1246-05-24',tel='133323232' WHERE name='赵敏';
6、删除年龄大于25岁的男人的信息
DELETE from student where sex='男' and age>25;
SELECT * FROM student;

9.25

INSERT into student(sno,name,sex,birthday) VALUES('2101003','王师傅','女',55,'1999-12-03','11122334455');
SELECT * from student;
DELETE from student;
-- 约束
-- 1、主键约束,能够阻止两条相同记录的插入
alter table student add PRIMARY key(sno);
-- 2、唯一性约束,能够阻止两条相同记录的插入
alter table student add constraint u_sfzh UNIQUE(sfzh);
-- 3、检查约束,规范插入的范围或符合实际
-- eg1:在age字段创建检查约束,使插入的值在0-200之间
alter TABLE student add constraint c_age check(age>=0 and age<=200);
-- eg2:在sex字段创建检查约束,使sex字段插入的值是男或女
alter table student add constraint c_sex check(sex='男' or sex='女');
-- 4、默认值约束,在某个字段上填充默认的数据
ALTER TABLE student modify tel char(11) DEFAULT'000000000';
-- 5、非空约束
ALTER TABLE student MODIFY age TINYINT not null;

9.27
-- 对字段操作
SELECT sname as 姓名,Ssex as 性别 from student;
-- 例1,查询表中所有女生的信息
SELECT * from student WHERE Ssex='女';
-- 练习1 查询班级编号是11010111班的所有学生信息
SELECT * from student WHERE Classno='11010111';
-- 练习2 查询班级编号是11010111班的学生的姓名和班级编号,字段名用中文显示
SELECT sname as 姓名,Classno as 班级编号 from student WHERE Classno='11010111';
-- 练习3 查询班级编号是11010111班的所有女生信息
SELECT * from student WHERE Classno='11010111' and ssex='女';
-- 练习4 查询1995年以后的所有女生的信息
SELECT * from student WHERE Sbirthday>'1995-12-31' and Ssex='女';
-- 例2 查询表中性王的学生的信息 %通配符 like模糊匹配
SELECT * from student WHERE Sname like '王%';
-- 练习5 查询表中性王的学生的信息
SELECT * from student WHERE Sname like '%萍%';
-- 练习6 查询表中江苏省的学生的信息
SELECT * from student WHERE address like '江苏省%';
-- 练习7 查询表中家中住镇江的学生的信息
SELECT * from student WHERE address like '%镇江市%';
-- 练习8 查询表中1995年出生学生的信息
SELECT * from student WHERE Sbirthday like '1995%';

9.29

聚合函数
cont() 计数
sum() 求和
avg() 求平均
max() 求最大值
min() 求最小值
-- SELECT * FROM result
-- 例1 查询student表中男生的人数
SELECT count(sno) as 男生人数 from student WHERE ssex='男';
-- 练习1 在result表中,查询学号1201011101的学生平均成绩
SELECT avg(score) as 平均成绩 from result WHERE sno='1201011101';
-- 练习2 在result表中,查询课程编号是0102001的课程的最高分和最低分
SELECT max(score) as 最高分,min(score) as 最低分 from result WHERE cno='0102001';

year() 年
month()月
day()日
SELECT CURDATE()当前时间日期函数 
DAYOFWEEK(date) 返回一周中的第几天
dayoyear(data) 返回一年中的第几天
DAYOFMONTH(date) 返回第几周
DAYNAME(date) 返回一周中的星期几
NOW() 获取当前的日期和时间
CURDATE() 获取当前的日期
例2
SELECT *,2024-year(sbirthday) as age from student;
练习3 在student表中,查询年龄最大的那个学生的信息,并且把年龄显示出来
SELECT *,max(year(CURDATE())- year(sbirthday)) as 最大年龄 from student;

10.9

范围、域查询
例1、查询出生日期是1995-4-1,1995-9-1,1995-5-24号出生的学生的信息
-- SELECT
--     *
-- FROM
--     student
-- WHERE
--     sbirthday = '1995-4-1'
-- OR sbirthday = '1995-9-1'
-- OR sbirthday = '1995-5-24';

SELECT
    *
FROM
    student
WHERE
    sbirthday IN (
        '1995-4-1',
        '1995-9-1',
        '1995-5-24'
    );
例2 查询年龄在30到34之间的学生信息
SELECT *,2024 - YEAR (sbirthday)
 
FROM
    student
WHERE
    2024 - YEAR (sbirthday) BETWEEN 30 and 34;

-- 当前日期 CURDATE()
-- 当前年份
select year(CURDATE()); 

SELECT * ,year(CURDATE())-year(sbirthday) as age from student;




concat(s1,s2...s) 把几个字符串连接起来
left(s,n) 获取字符串s左边的n个字符
right(s,n) 获取字符串s右边的n个字符
trim(s) 消除字符串s前后的空格
rtrim(s) 消除字符串s右面的空格
ltrim(s) 消除字符串s左面的空格
SUBSTR(str FROM pos FOR len) 从字符串str截取从pos开始的len长度的字符串
LOCATE(substr,str) 在str里查找substr第一次出现的位置
LOWER(str)
UPPER(str)
例3 查询年龄是35岁的学生,显示信息为'我叫'姓名',今年35岁'
SELECT
    CONCAT(
        '我叫',
        sname,
        ',今年35岁'
    ) AS 信息
FROM
    student
WHERE
    YEAR (CURDATE()) - YEAR (sbirthday) = 33;

例4 查询所有姓王学生的信息
SELECT * from student WHERE left(sname,1)='王'
练习1,查询所有1995年出生的学生的信息,使用left函数
SELECT * from student WHERE left(Sbirthday,4)=1995
Last Modified: October 9, 2024