首页 > 其他分享 >建表,和练习题

建表,和练习题

时间:2023-10-08 15:56:50浏览次数:42  
标签:练习题 建表 sno score localhost student root select

目录

建库

create database linux

注意::整形和浮点型不用加''号,其他字符串型那部分需要加''

表一

字段 数据类型要求 是否为空 注释
sno 最多20位 学号(主键)
sname 可变长 学生姓名
sage 最小整数,非负数 学生年龄
ssex 0,1 学生性别(1是男,0是女)默认为男)
sbirthday 时间类型 默认为空 学生生日
class 可变长 学生班级
root@localhost [(none)] >create table `test1` (
	`son` char(20) not null primary key comment '学号',
	`sname` varchar(10) not null comment '学生姓名',
	`sage` tinyint not null comment '学生年龄',
	`ssex` enum('0','1') not null default '1' comment '学生性别(1是男,0是女,默认为男)',
	`sbirthday` date default null comment '学生生日',
	`class` varchar(10) not null comment '学生班级'
	);


# 查看库
root@localhost [nono] >show databases;

# 切换库
root@localhost [mysql] >use mysql

# 查看库里的表
root@localhost [mysql] >show tables;

## 查看表结构
root@localhost [mysql] >desc mysql.user;

# 查看表内内容
root@localhost [(none)] >select * from mysql.user\G

# 查看表内的列
root@localhost [(none)] >select user,host from mysql.user;

# 能查看注释的内容
root@localhost [linux] >show full columns from test;
root@localhost [linux] >show create table linux.test;

-------------------------------------------------------
## 向表里添加信息
root@localhost [linux] >INSERT INTO test (son, sname, sage, ssex, sbirthday, class) VALUES (1, '张三', 20, '1', '2023-07-24', '一班');


# 多行
insert into test(sname,sage,ssex,sbirthday,class) value
('徐导',20,'1',now(),'1'),
('曾导',18,'1',now(),'1'),
('李导',25,'1',now(),'2');

联合主键

primary key (sno,cno)

create table linux50.score(sno char(20) comment'学号(主键)',cno varchar(20) comment'课程号(主键)',mark float(4,1) comment'成绩',primary key (sno,cno));

001

## zerofill:零填充 加了这个选项后 写入1时 这里指定了3位 那么会保存为001 只能写整数类型

建表练习题

## student
create table linux50.student(
sno bigint(20) not null primary key auto_increment comment '学号(主键)',
sname varchar(10) not null comment '学生姓名',
sage tinyint unsigned not null comment '学生年龄',
ssex enum('0','1') not null default '1' comment '学生性别(1是男,0是女)默认为男)',
sbirthday datetime null comment '学生生日',
class varchar(5) not null comment '学生班级');
## course
CREATE TABLE `course` (
`cno` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '课程号(主键)',
`cname` varchar(10) NOT NULL COMMENT '课程名称',
`tno` varchar(10) NOT NULL COMMENT '教师编号',
PRIMARY KEY (`cno`,`cname`,`tno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
## score
CREATE TABLE score(
sno BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '学号(主键)',
cno BIGINT(20) NOT NULL COMMENT '课程号(主键)',
mark FLOAT(4,1) NOT NULL COMMENT '成绩',
PRIMARY KEY(sno,cno));
## teacher
CREATE TABLE `teacher` (
`tno` bigint(3) unsigned zerofill NOT NULL AUTO_INCREMENT COMMENT '教师编号(主键)',
`tname` varchar(10) NOT NULL COMMENT '教师姓名',
`tage` tinyint(3) unsigned NOT NULL COMMENT '教师年龄',
`tsex` enum('1','0') NOT NULL DEFAULT '1' COMMENT '教师性别(1是男,0是女)默认为男)',
`prof` varchar(10) NOT NULL COMMENT '教师职称',
`depart` varchar(10) NOT NULL COMMENT '教师部门',
PRIMARY KEY (`tno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

查询练习

1.查询student表中的所有记录的sname、ssex和class列。
root@localhost [linux50] >select sname,ssex,class from student;
+-------+------+-------+
| sname | ssex | class |
+-------+------+-------+
| 旭    | 1    | L5    |
| 东    | 1    | L5    |
+-------+------+-------+
2 rows in set (0.00 sec)

2.查询教师所有的单位即不重复的depart列。
root@localhost [linux50] >select distinct depart from teacher;
+-----------+
| depart    |
+-----------+
| 语言系    |
| 文学系    |
| 科学系    |
+-----------+
3 rows in set (0.00 sec)

3.查询student表的所有记录。
root@localhost [linux50] >select * from student;
+-----+-------+------+------+---------------------+-------+
| sno | sname | sage | ssex | sbirthday           | class |
+-----+-------+------+------+---------------------+-------+
|   4 | 旭    |   20 | 1    | 2023-07-25 18:18:30 | 5     |
|   5 | 东    |   21 | 1    | 2023-07-25 18:18:28 | 5     |
|   6 | 李    |   18 | 0    | 2023-07-25 19:38:13 | 9     |
|   7 | 明    |   22 | 1    | 2023-07-25 19:39:16 | 7     |
|   8 | 王    |   11 | 1    | 2023-07-25 20:06:23 | 2     |
|   9 | 徐    |   23 | 1    | 2023-07-25 20:06:42 | 2     |
+-----+-------+------+------+---------------------+-------+
6 rows in set (0.00 sec)


4.查询score表中成绩在60到80之间的所有记录。
root@localhost [linux50] >select * from score;
+-----+-----+------+
| sno | cno | mark |
+-----+-----+------+
|   1 |   1 | 90.0 |
|   2 |   1 | 10.0 |
|   3 |   1 | 60.0 |
|   1 |   2 | 90.0 |
|   2 |   2 | 99.5 |
|   3 |   2 | 80.0 |
|   1 |   3 | 80.5 |
|   2 |   3 | 60.0 |
|   3 |   3 | 88.0 |
+-----+-----+------+
9 rows in set (0.00 sec)

root@localhost [linux50] >select * from score where mark>=60 and mark<=80;
+-----+-----+------+
| sno | cno | mark |
+-----+-----+------+
|   3 |   1 | 60.0 |
|   3 |   2 | 80.0 |
|   2 |   3 | 60.0 |
+-----+-----+------+
3 rows in set (0.00 sec)

5.查询score表中成绩为85,86或88的记录。
root@localhost [linux50] >select * from score where mark in ('85','86','88');
+-----+-----+------+
| sno | cno | mark |
+-----+-----+------+
|   3 |   3 | 88.0 |
+-----+-----+------+
1 row in set (0.00 sec)

6.查询student表中2班或性别为“女”的同学记录。
## 查询没加''报错
root@localhost [linux50] >select * from student where class=2 or ssex=0;
ERROR 1054 (42S22): Unknown column '2' in 'where clause'
root@localhost [linux50] >select * from student where class='2' or ssex='0';
+-----+-------+------+------+---------------------+-------+
| sno | sname | sage | ssex | sbirthday           | class |
+-----+-------+------+------+---------------------+-------+
|   6 | 李    |   18 | 0    | 2023-07-25 19:38:13 | 9     |
|   8 | 王    |   11 | 1    | 2023-07-25 20:06:23 | 2     |
|   9 | 徐    |   23 | 1    | 2023-07-25 20:06:42 | 2     |
+-----+-------+------+------+---------------------+-------+
3 rows in set (0.00 sec)


7.以class降序查询Student表的所有记录。
root@localhost [linux50] >select * from student order by class;
+-----+-------+------+------+---------------------+-------+
| sno | sname | sage | ssex | sbirthday           | class |
+-----+-------+------+------+---------------------+-------+
|   8 | 王    |   11 | 1    | 2023-07-25 20:06:23 | 2     |
|   9 | 徐    |   23 | 1    | 2023-07-25 20:06:42 | 2     |
|   4 | 旭    |   20 | 1    | 2023-07-25 18:18:30 | 5     |
|   5 | 东    |   21 | 1    | 2023-07-25 18:18:28 | 5     |
|   7 | 明    |   22 | 1    | 2023-07-25 19:39:16 | 7     |
|   6 | 李    |   18 | 0    | 2023-07-25 19:38:13 | 9     |
+-----+-------+------+------+---------------------+-------+
6 rows in set (0.00 sec)


8.以cno升序、mark降序查询Score表的所有记录
root@localhost [linux50] >select * from score order by cno asc, mark desc;
+-----+-----+------+
| sno | cno | mark |
+-----+-----+------+
|   1 |   1 | 90.0 |
|   3 |   1 | 60.0 |
|   2 |   1 | 10.0 |
|   2 |   2 | 99.5 |
|   1 |   2 | 90.0 |
|   3 |   2 | 80.0 |
|   3 |   3 | 88.0 |
|   1 |   3 | 80.5 |
|   2 |   3 | 60.0 |
+-----+-----+------+
9 rows in set (0.00 sec)

9.查询2班的学生人数。
# count == wc -l

root@localhost [linux50] >select count(*) as student_count from student where class='2';
+---------------+
| student_count |
+---------------+
|             2 |
+---------------+
1 row in set (0.00 sec)

10.查询”曾志高翔“教师任课的学生成绩。
root@localhost [linux50] >select * from course;
+-----+--------+-----+
| cno | cname  | tno |
+-----+--------+-----+
|   1 | 数学   | 001 |
|   2 | 语文   | 002 |
|   3 | 英语   | 003 |
+-----+--------+-----+
3 rows in set (0.00 sec)

root@localhost [linux50] >select * from score;
+-----+-----+------+
| sno | cno | mark |
+-----+-----+------+
|   1 |   1 | 90.0 |
|   2 |   1 | 10.0 |
|   3 |   1 | 60.0 |
|   1 |   2 | 90.0 |
|   2 |   2 | 99.5 |
|   3 |   2 | 80.0 |
|   1 |   3 | 80.5 |
|   2 |   3 | 60.0 |
|   3 |   3 | 88.0 |
+-----+-----+------+
9 rows in set (0.00 sec)

root@localhost [linux50] >select * from student;
+-----+-------+------+------+---------------------+-------+
| sno | sname | sage | ssex | sbirthday           | class |
+-----+-------+------+------+---------------------+-------+
|   1 | 旭    |   20 | 1    | 2023-07-26 17:33:14 | 5     |
|   2 | 东    |   21 | 1    | 2023-07-26 17:33:27 | 5     |
|   3 | 徐    |   22 | 1    | 2023-07-26 17:33:39 | 5     |
+-----+-------+------+------+---------------------+-------+
3 rows in set (0.00 sec)

root@localhost [linux50] >select * from teacher;
+-----+--------+------+------+------------+-----------+
| tno | tname  | tage | tsex | prof       | depart    |
+-----+--------+------+------+------------+-----------+
| 001 | 曾导   |   18 | 1    | 教学总垠   | 语言系    |
| 002 | 徐导   |   50 | 1    | 讲师       | 文学系    |
| 003 | 李导   |   80 | 1    | 助教       | 科学系    |
+-----+--------+------+------+------------+-----------+
3 rows in set (0.00 sec)

root@localhost [linux50] >select teacher.tname,course.cname,student.sname,score.mark from teacher  join
course on teacher.tno=course.tno join score on course.cno=score.cno join student on score.sno=student.sno where teacher.tname='曾导';

root@localhost [linux50] >select teacher.tname,course.cname,student.sname,score.mark
from teacher 
join course on teacher.tno=course.tno
join score on course.cno=score.cno
join student on score.sno=student.sno
where teacher.tname='曾导';
+--------+--------+-------+------+
| tname  | cname  | sname | mark |
+--------+--------+-------+------+
| 曾导   | 数学   | 旭    | 90.0 |
| 曾导   | 数学   | 东    | 10.0 |
| 曾导   | 数学   | 徐    | 60.0 |
+--------+--------+-------+------+
3 rows in set (0.00 sec)

11.查询语文课程所有男生的成绩并且查出对应课程的教师名,职称,及所在部门。
root@localhost [linux50] >select teacher.tname,teacher.prof,teacher.depart,student.sname,course.cname,score.mark 
from teacher 
join course on teacher.tno=course.tno 
join score on score.cno=course.cno 
join student on student.sno=score.sno 
where student.ssex='1' and course.cname='语文';
+--------+--------+-----------+-------+--------+------+
| tname  | prof   | depart    | sname | cname  | mark |
+--------+--------+-----------+-------+--------+------+
| 徐导   | 讲师   | 文学系    | 旭    | 语文   | 90.0 |
| 徐导   | 讲师   | 文学系    | 东    | 语文   | 99.5 |
| 徐导   | 讲师   | 文学系    | 徐    | 语文   | 80.0 |
+--------+--------+-----------+-------+--------+------+
3 rows in set (0.00 sec)

12.把11题查出的成绩按照降序排序。

sql练习

1.查询表里所有的数据
select * from city;
# 查询   所有字段  从  表;

2.查询部分字段的数据
select name from city limit 10;
# 查询name和CountryCode的列,的前10行。
select name,CountryCode from city limit 10;

3.查询city表中,所有中国的城市信息CHN
root@localhost [world] >select * from city where CountryCode='CHN';

4.查询city表中,城市名称叫北京的信息
root@localhost [world] >select * from city where District='peking';

5.查询人口数小于100人城市信息
root@localhost [world] >select * from city where Population<100;

6.查询中国,人口数超过500w的所有城市信息
root@localhost [world] >select * from city where CountryCode='CHN' and Population>5000000; 

7.查询中国或美国的城市信息
root@localhost [world] >select * from city where CountryCode='CHN' or CountryCode='USA';
root@localhost [world] >select * from city where CountryCode in ('CHN','USA');

8.查询人口数为100w-200w (包括两头)城市信息
root@localhost [world] >select * from city where Population>=1000000 and Population<=2000000;
root@localhost [world] >select * from city where Population between 1000000 AND 2000000;

9.查询中国或美国,人口数大于500w的城市
root@localhost [world] >select * from city where CountryCode in ('CHN','USA') and Population>5000000;

10.查询CHN城市名为qing开头的城市信息
root@localhost [world] >select * from city where CountryCode='CHN' and name like 'qing%';

11.查询 城市名为jing结尾的城市信息,只显示name和Population列
root@localhost [world] >select name,Population from city where CountryCode='CHN' and name like '%jing';

12.查询mysql里都有哪些用户和主机地址

13.查询表的字段都有哪些
desc city;

标签:练习题,建表,sno,score,localhost,student,root,select
From: https://www.cnblogs.com/xiutai/p/17749286.html

相关文章

  • oj练习题 数字 eval 整数 int ???
      s=input()if'helloworld!'==s.casefold():print("Yes")else:print("No")    A+B问题II描述亲爱的小朋友们,大家好!今天我们来探讨一下大家都会做的A+B的问题,给你两个数A和B,请你输出这两个数的和。输入输入两个数字,a和b输出输出一个......
  • Oracle 12c创建表空间、用户
    目录一、前言二、创建表空间三、创建用户四、删除表空间、用户五、CDB与PDB相关文章一、前言Oracle12c中新增加了可插入数据库的概念,即PDB(PluggableDatabase),允许一个容器数据库,即CDB(ContainerDatabase)承载多个可插入数据库PDB。CDB相当于操作系统,调用并管理各个PDB,P......
  • python基础操作练习题
    使用版本:python3.6.8IDE:pycharm前言这些练习题是在神经网络与深度学习课程上老师提供的,原因是有些同学没学过python,作为简单的练手习题。题目都很简单,加上python本身也比较简单,有些题目的作答可以一行代码实现(虽然可读性就下降了)。练习题2.1数位之和编写程序,输入一个正......
  • C# efcode 新建表格数据 增删改查
    usingTestDbContextctx=newTestDbContext();varb1=newBook{AuthorName="杨中科",Title="零基础趣学C语言",Price=59.8,PubTime=newDateTime()};varb2=newBook{AuthorName="RobertSedgewick",......
  • go语言ent教程:使用ent定义数据库的表schema、创建表
    背景:在go语言里,常用的与数据库进行交互的包有:gorm、xorm,今天我们讲解的是:ent,本ent系列的教程基于Mac开发环境,归属于go-study项目。 一、准备工作1阅读ent官方的上手教程 https://entgo.io/docs/getting-started2创建测试数据库:test 二、跟着ent官方的上手教程动手......
  • 攻防世界MISC【3-1】练习题WriteUp
    下载附件是一个没有后缀的文件,直接扔到010Editor看看观察了一下发现应该是rar压缩包,去给它加上后缀试试。加上后缀解压出来的又是一个不知道是什么的文件。直接丢到010Editor看了看发现是个流量包既然知道了是个流量包,试着给它加上pcap后缀试试看BinGo用Wireshark可以打......
  • for循环以及练习题
    循环结构for循环一种特殊的方式int[]numbers={10,20,30,4,50};for(intx:numbers){System.out.println(x);}打印三角形5行for(inti=0;i<5;i++){for(intj=0;j<5-i;j++){System.out.print("");//第一行打五个空格第二行四个。......
  • hive string, map, struct类型的建表和导入数据语句
    本文转载于 https://blog.51cto.com/u_14405/6419362,https://blog.csdn.net/tototuzuoquan/article/details/115493697和 https://blog.csdn.net/weixin_43597208/article/details/117450579。今天要用到hive的string相关的数据类型和数据,直接附链接和sql语句Hive的String类......
  • 函数(函数的分类及声明和定义,练习题,作用域和生命周期的介绍,static和extern的详细介绍)
    1.函数的概念是一个完成某项任务的一小段代码,包括库函数和自定义函数1.1库函数库函数相关头文件点击查看库函数需要包含头文件1.2自定义函数函数的语法形式形参只有在函数在被调用的过程中为了存放实参传递过来的值,才向内存申请空间,这个过程叫形参的实例化VS中调试时F10,当进入形......
  • 攻防世界MISC练习题[中等] QR1
    下载附件得到一张空白的图片直接打开放大后发现有很多黑点,观察其的分布位置看起来像是二维码因为全是小黑点的分布也不能直接扫描出来,拿去KALI看一下。虚拟鸡启动!binwalk没内容zstegnothing。现在想起来题目是QR,在想会不会是和二维码有关,决定再去看看图片。放大图片后......