持续学习,持续更新
本题目要求编写 Insert语句, 在stu表中添加一条学生记录:学号:S012,姓名:,周强,性别:1,其它属性为NULL.
表结构:
CREATE TABLE `stu` (
`sno` char(4) NOT NULL,
`sname` char(8) NOT NULL,
`sex` tinyint(1) DEFAULT NULL,
`mno` char(2) DEFAULT NULL,
`birdate` datetime DEFAULT NULL,
`memo` text,
PRIMARY KEY (`sno`)
);
INSERT INTO stu (sno,sname,sex) VALUES ('S012','周强',1);
本题目要求编写SQL语句, 检索C002号课程的成绩最高的二人学号,姓名与成绩
表结构:
CREATE TABLE `stu` (
`sno` char(4) NOT NULL,
`sname` char(8) NOT NULL,
`sex` tinyint(1) DEFAULT NULL,
`mno` char(2) DEFAULT NULL,
`birdate` datetime DEFAULT NULL,
`memo` text,
PRIMARY KEY (`sno`),
);
CREATE TABLE `sc` (
`sno` char(4) NOT NULL,
`cno` char(4) NOT NULL,
`grade` decimal(6,1) DEFAULT NULL,
PRIMARY KEY (`sno`,`cno`),
CONSTRAINT `fk_sc_sno` FOREIGN KEY (`sno`) REFERENCES `stu` (`sno`)
);
SELECT stu.sno,stu.sname,sc.grade
FROM stu LEFT JOIN sc
ON stu.sno=sc.sno
WHERE sc.cno='C002'
ORDER BY sc.grade
DESC LIMIT 2;
本题目要求编写DELETE语句, 删除学生姓名为’周强’的所有数据信息。
表结构:
CREATE TABLE `stu` (
`sno` char(4) NOT NULL,
`sname` char(8) NOT NULL,
`sex` tinyint(1) DEFAULT NULL,
`mno` char(2) DEFAULT NULL,
`birdate` datetime DEFAULT NULL,
`memo` text,
PRIMARY KEY (`sno`)
);
CREATE TABLE `sc` (
`sno` char(4) NOT NULL,
`cno` char(4) NOT NULL,
`grade` decimal(6,1) DEFAULT NULL,
PRIMARY KEY (`sno`,`cno`),
CONSTRAINT `fk_sc_sno` FOREIGN KEY (`sno`) REFERENCES `stu` (`sno`)
);
DELETE t1,t2
FROM stu AS t1 LEFT JOIN sc AS t2
ON t1.sno = t2.sno
WHERE t1.sname = '周强';
在员工表中查询所有男性员工的编号,姓名和入职日期,结果按员工编号升序排列
表结构:
create table 员工(员工编号 char(3) primary key,姓名 char(5),性别 char(1),出生日期 date,入职日期 date,电话 char(11))
SELECT 员工编号,姓名,入职日期
FROM 员工
WHERE 性别='男'
ORDER BY 员工编号 ASC;
本题目要求编写SQL语句, 检索出students表中所有年龄小于等于25岁的女生的学号和姓名的记录。
表结构:
CREATE TABLE students (
sno char(7) ,
sname char(8) NOT NULL,
class char(10),
ssex char(2),
bday date ,
bplace char(10) ,
IDNum char(18) ,
sdept char(16),
phone char(11),
PRIMARY KEY (sno)
) ;
select sno as "学号",sname as "姓名",ssex as "性别"
from students
where ROUND(DATEDIFF(CURDATE(), bday)/365.2422) <=25
and
ssex="女";