zoukankan      html  css  js  c++  java
  • 数据库的增删改查

    CREATE TABLE lovostudebt(
    studentCode VARCHAR(20) PRIMARY KEY,
    studentName VARCHAR(20),
    age INT,
    sex VARCHAR(10),
    grade INT,
    clasddName VARCHAR (20)
    )DEFAULT CHARSET=utf8;

    INSERT INTO lovostudebt (studentCode,studentName,age,sex,grade,clasddName)
    VALUES('xh1001','jacky','20','男','90','T01'),
    ('xh1002','simth','30','男','75','T02'),
    ('xh1003','jay','18','男','80','T01'),
    ('xh1004','helen','19','女','75','T02'),
    ('xh1005','lily','22','女','90','T03'),
    ('xh1006','Green','23','男','85','T02'),
    ('xh1007','RedChar','18','男','60','T01'),
    ('xh1008','Kevin','17','女','45','T03');

    SELECT *FROM lovostudebt;
    DROP TABLE lovostudebt;


    -- 得到所有学生的总分 sum 表示求和
    SELECT SUM(grade) AS '总分' FROM lovostudebt;

    -- 等到所有学生的最高分 max 表示求最大值
    SELECT MAX(grade) AS '最高分' FROM lovostudebt;

    -- 得到所有学生的最低分,min表示求最小值
    SELECT MIN(grade) AS'最低分' FROM lovostudebt;

    -- 得到所有学生的平均分 ,avg表示求平均分
    SELECT AVG(grade) AS'平均分' FROM lovostudebt;


    -- 得到所有学生的总人数
    -- COUNT (geade)同级总人数,会忽略该列为null的记录。
    -- COUNT (*)不会忽略该列为null的记录。
    SELECT COUNT(grade)AS'人数' FROM lovostudebt;

    SELECT COUNT(*)AS'人数' FROM lovostudebt;




    -- 分组查询每个班级的总分
    -- GROUP BY 是以什么进行分组
    SELECT clasddName,SUM(grade) AS'总分' FROM lovostudebt GROUP BY clasddName;


    -- 分组查询每个班级的平均分 最高分 最低分 参考人数
    -- clasddName 班级名称
    -- 分组之后在select 中出现的字段,织女呢个是分组字段(group by 后面的字段)或者是聚合函数
    SELECT clasddName,SUM(grade) AS'总分',
    AVG(grade) AS'平均分',
    MAX(grade) AS '最高分',
    MIN(grade) AS'最低分',
    COUNT(grade) AS'参考人数'
    FROM lovostudebt GROUP BY clasddName;

    -- 查询平均分在85以上的学生
    -- where 后面不能跟聚合函数,因为where执行在GROUP by之前
    -- having 执行在分组之后,可以跟聚合函数
    SELECT clasddName FROM lovostudebt GROUP BY clasddName HAVING AVG(grade)>=75;


    -- 查询那个班有女生,并说出女生的人数
    SELECT clasddName AS'班级名称',COUNT(sex) AS '女生人数' FROM lovostudebt WHERE sex='女' GROUP BY clasddName;


    CREATE TABLE student(
    id INT PRIMARY KEY AUTO_INCREMENT,
    studentCode INT, -- 学号
    studentName VARCHAR(20), -- 学生成绩
    subjectName VARCHAR(20),
    grade INT, -- 成绩
    classID INT -- 班级编号

    )DEFAULT CHARSET=utf8;


    CREATE TABLE class(
    id INT PRIMARY KEY AUTO_INCREMENT,
    className VARCHAR (20) -- 班级

    )DEFAULT CHARSET=utf8;

    -- 增加外键约束
    ALTER TABLE student ADD CONSTRAINT fk_class FOREIGN KEY (classID) REFERENCES class(id);


    INSERT INTO class (className) VALUES('T01'),('T02'),('T03'),('T04');

    SELECT *FROM class;
    DROP TABLE class;


    INSERT INTO student(studentCode, studentName,subjectName,grade,classID )
    VALUES(1,'张三','计算机基础',60,1),
    (1,'张三','C语言',88,1),
    (1,'张三','关系型数据库基础',90,1),
    (2,'王五','计算机基础',80,2),
    (2,'王五','C语言',70,2),
    (2,'王五','关系型数据库基础',55,2),
    (3,'李四','C语言',66,4),
    (3,'李四','关系型数据库基础',90,4);
    SELECT* FROM student;
    DROP TABLE student;
    -- 显示C语言这门课程的最高分和得最高分的学生姓名
    SELECT MAX(grade),studentName FROM student WHERE subjectName='计算机基础'
    AND grade=(SELECT MAX(grade) FROM student WHERE subjectName='计算机基础');


    SELECT * FROM student WHERE subjectName='计算机基础' ORDER BY grade DESC LIMIT 0,1;

    -- 显示所有学生信息,要求将班级编号转化为班级名称
    SELECT s.*,(SELECT className FROM class AS c WHERE c.id = s.classID) AS'班级名称' FROM student AS s;


    -- 查询比王五c语言考得更好的学生
    SELECT * FROM student WHERE subjectName ='c语言' AND
    grade >(SELECT grade FROM student WHERE subjectName='c语言' AND studentName='王五');


    -- 查询关系型数据库和张三同分的学生信息
    SELECT *FROM student WHERE subjectName ='关系型数据库基础' AND
    grade=(SELECT grade FROM student WHERE subjectName ='关系型数据库基础'
    AND studentName='张三' )AND studentName!='张三';



    -- 查询那个班级有学生
    SELECT className FROM class WHERE id IN (SELECT DISTINCT classID FROM student);


    -- 查询那个班级没有学生
    SELECT className FROM class WHERE id NOT IN (SELECT DISTINCT classID FROM student);


    -- 找出每门课程的最高分和学生姓名
    SELECT * FROM student s1 WHERE grade IN(
    SELECT MAX(grade) FROM student s2
    WHERE s1.subjectName=s2.subjectName GROUP BY subjectName);

    -- 查询所有学生姓名、科目、成绩、所在班级
    SELECT s.studentName,s.subjectName,s.grade,c.className FROM
    student s JOIN class c ON s.classID=c.id;


    SELECT s.studentName,s.subjectName,s.grade,c.className FROM
    student s ,class c WHERE s.classID=c.id;



    CREATE TABLE t_student(
    id INT PRIMARY KEY AUTO_INCREMENT,
    StudentName VARCHAR (20),
    chinese INT,
    math INT,
    english INT,
    cardCode VARCHAR (20) -- 考试号
    )DEFAULT CHARSET=utf8;
    INSERT INTO t_student(StudentName,chinese,math,english,cardCode)
    VALUES('张无忌','90','80','70','12121525121'),
    ('王振天','44','88','74','12454545551'),
    ('胡凯','73','58','99','11245454545'),
    ('李金','70','90','84','52524645254');

    SELECT*FROM t_student;

    DROP TABLE t_student;


    -- 将张无忌的语文更改为95分,数学改为85分,
    UPDATE t_student SET chinese='95',math='85' WHERE StudentName= '张无忌'; -- 可以换成ID

    -- 将数学成绩都加5分
    UPDATE t_student SET math=math+5;

    -- 将姓张的人英语减10分
    -- 张%,表示通配符,匹配张开始的所有字符串(张三,张四,张五)
    -- %张%,表示(刘张云,杨张云)
    UPDATE t_student SET english=english-10 WHERE StudentName LIKE '张%'

    -- 增加学校列
    ALTER TABLE t_student ADD school VARCHAR(30);

    -- 修改张无忌的学校为成都十二中,胡凯学校为成都七中

    UPDATE t_student SET school='成都十二中' WHERE id=1;
    UPDATE t_student SET school='成都七中' WHERE id=3;



    -- 修改学校为null的记录,学校更改为成都四中
    UPDATE t_student SET school='成都四中' WHERE school IS NULL;


    -- 删除胡凯
    DELETE FROM t_student WHERE studentName='胡凯';

    -- 删除60-70之间的记录
    DELETE FROM t_student WHERE english>=60 AND english<=70;

    -- 也可以写成
    DELETE FROM t_student WHERE english BETWEEN 60 AND 70;

    -- 删除所有
    TRUNCATE TABLE t_student;


    -- 查询所有记录
    -- *表示该表中所有的字段
    SELECT * FROM t_student;

    -- 查询所有学生的姓名和语文成绩
    SELECT studentName,chinese FROM t_student;


    -- AS表示给该列取别名
    SELECT studentName AS'姓名',chinese AS '语文' FROM t_student;


    -- DISTINCT 去除重复的记录
    SELECT DISTINCT school FROM t_student;



  • 相关阅读:
    tar命令解压jdk.tar.gz包 报错 gzip: stdin: not in gzip format
    CentOS6.5安装完没有网络的解决办法
    UML在需求分析阶段的应用
    UML
    UML在软件开发中各个阶段的作用和意义
    Maven那点事儿(Eclipse版)
    eclipse和myeclipse的下载地址
    div的作用
    c:if标签数据回显判断是否选中
    《Hadoop》对于高级编程Hadoop实现构建企业级安全解决方案
  • 原文地址:https://www.cnblogs.com/liner730/p/4574370.html
Copyright © 2011-2022 走看看