296 lines
12 KiB
TeX
296 lines
12 KiB
TeX
|
\documentclass[全部作业]{subfiles}
|
|||
|
\input{mysubpreamble}
|
|||
|
\begin{document}
|
|||
|
\setcounter{chapter}{1}
|
|||
|
\chapter{第二周作业}
|
|||
|
\begin{enumerate}
|
|||
|
\item 以下四个整数哪个不在Int类型的范围内?
|
|||
|
\begin{tasks}[](4)
|
|||
|
\task \fbox{$2^{31}$}
|
|||
|
\task $2^{31}-1$
|
|||
|
\task $-2^{31}$
|
|||
|
\task $-2^{31}+1$
|
|||
|
\end{tasks}
|
|||
|
\item 以下四个整数哪个不在Unsigned Int类型的范围内?
|
|||
|
\begin{tasks}[](4)
|
|||
|
\task $2^{31}$
|
|||
|
\task $2^{31}-1$
|
|||
|
\task \fbox{$2^{32}$}
|
|||
|
\task $2^{32}-1$
|
|||
|
\end{tasks}
|
|||
|
\item 对于默认的Decimal与Double类型,分别输入1234.56与9876.54后,将其字段类型分别修改为Decimal(5,1)与Double(5,1),最终显示的内容为
|
|||
|
\begin{tasks}[](4)
|
|||
|
\task 1235.0、9877.0
|
|||
|
\task 1234.6、9876.5
|
|||
|
\task \fbox{1235.0、9876.5}
|
|||
|
\task 1235.6、9876.0
|
|||
|
\end{tasks}
|
|||
|
\begin{verification}
|
|||
|
\begin{minted}{sql}
|
|||
|
drop table if exists test1;
|
|||
|
create table if not exists test1(decimal_ decimal, double_ double);
|
|||
|
\end{minted}
|
|||
|
\includegraphics[width=0.6\linewidth]{imgs/2024-09-27-08-44-19.png}
|
|||
|
|
|||
|
\begin{minted}{sql}
|
|||
|
insert into test1 values (1234.56, 9876.54);
|
|||
|
\end{minted}
|
|||
|
\includegraphics[width=0.6\linewidth]{imgs/2024-09-27-08-47-00.png}
|
|||
|
|
|||
|
\begin{minted}{sql}
|
|||
|
alter table test1 modify column decimal_ decimal(5, 1);
|
|||
|
alter table test1 modify column double_ double(5, 1);
|
|||
|
\end{minted}
|
|||
|
\includegraphics[width=0.6\linewidth]{imgs/2024-09-27-08-51-00.png}
|
|||
|
|
|||
|
\end{verification}
|
|||
|
\item 对于两种类型,分别设置为ENUM(‘a’, ‘b’, ‘c’)与SET(‘a’, ‘b’, ‘c’),之后均输入数据’a’、’a,c’、’a,d’,最终显示的内容为
|
|||
|
\begin{tasks}[](2)
|
|||
|
\task (‘a’)、(‘a’)
|
|||
|
\task (‘a’)、(‘a’, ‘a,c’)
|
|||
|
\task \fbox{(‘a’)、(‘a’, ‘a,c’, ‘a’)}
|
|||
|
\task (‘a’, ‘a,c’)、(‘a’, ‘a,c’, ‘a’)
|
|||
|
\end{tasks}
|
|||
|
\begin{verification}
|
|||
|
\begin{minted}{sql}
|
|||
|
DROP TABLE if EXISTS test2;
|
|||
|
CREATE TABLE if NOT EXISTS test2(
|
|||
|
enum_ enum('a', 'b', 'c'),
|
|||
|
set_ set('a', 'b', 'c')
|
|||
|
);
|
|||
|
INSERT INTO test2 VALUES ('a', 'a');
|
|||
|
INSERT INTO test2 VALUES ('a,c', 'a,c');
|
|||
|
INSERT INTO test2 VALUES ('a,d', 'a,d');
|
|||
|
\end{minted}
|
|||
|
\includegraphics[width=0.6\linewidth]{imgs/2024-09-27-09-09-53.png}
|
|||
|
\end{verification}
|
|||
|
\item 假设表A中有3个数据,其NAME项分别为\mycircle{1}‘Li Ming’, \mycircle{2}‘Fang Li’, \mycircle{3}‘Tang Li Zhi’, 对于命令SELECT NAME FROM A WHERE NAME LIKE ‘\%Li’, 其返回内容为
|
|||
|
\begin{tasks}[](4)
|
|||
|
\task \mycircle{1}
|
|||
|
\task \fbox{\mycircle{2}}
|
|||
|
\task \mycircle{3}
|
|||
|
\task \mycircle{1}\mycircle{2}\mycircle{3}
|
|||
|
\end{tasks}
|
|||
|
\begin{verification}
|
|||
|
\begin{minted}{sql}
|
|||
|
DROP TABLE if EXISTS test3;
|
|||
|
CREATE TABLE IF NOT EXISTS test3(name varchar(255));
|
|||
|
INSERT INTO test3 VALUES ('Li Ming'), ('Fang Li'), ('Tang Li Zhi');
|
|||
|
SELECT NAME FROM test3 WHERE NAME like '%Li';
|
|||
|
\end{minted}
|
|||
|
\includegraphics[width=0.3\linewidth]{imgs/2024-09-27-09-24-05.png}
|
|||
|
\end{verification}
|
|||
|
\item 对于以下4个查询,有哪些语句返回值为NULL
|
|||
|
\begin{enumerate}[label=\mycircle{\arabic{enumii}}]
|
|||
|
\item SELECT (!NULL)
|
|||
|
\item SELECT (NULL \&\& 1)
|
|||
|
\item SELECT (NULL || 1)
|
|||
|
\item SELECT (NULL XOR 1)
|
|||
|
\end{enumerate}
|
|||
|
\begin{tasks}[](4)
|
|||
|
\task \mycircle{1}\mycircle{4}
|
|||
|
\task \mycircle{2}\mycircle{3}
|
|||
|
\task \fbox{\mycircle{1}\mycircle{2}\mycircle{4}}
|
|||
|
\task \mycircle{1}\mycircle{2}\mycircle{3}\mycircle{4}
|
|||
|
\end{tasks}
|
|||
|
\item 以下哪条删除记录正确的
|
|||
|
\begin{tasks}[](2)
|
|||
|
\task \fbox{delete from emp where name='dony';}
|
|||
|
\task delete * from emp where name='dony';
|
|||
|
\task drop from emp where name='dony';
|
|||
|
\task drop * from emp where name='dony';
|
|||
|
\end{tasks}
|
|||
|
\questionandanswer[]{
|
|||
|
上机题:
|
|||
|
|
|||
|
写出向员工表(employee)插入如下记录的sql:
|
|||
|
|
|||
|
姓名:李四
|
|||
|
|
|||
|
员工编号:19
|
|||
|
|
|||
|
入职日期:2022-9-22
|
|||
|
|
|||
|
离职日期:无
|
|||
|
|
|||
|
职位名称:出纳员
|
|||
|
|
|||
|
分支机构编号:1
|
|||
|
|
|||
|
部门编号:2
|
|||
|
|
|||
|
上级领导编号:4
|
|||
|
}{}
|
|||
|
\begin{minted}{sql}
|
|||
|
insert into employee (LAST_NAME, FIRST_NAME, EMP_ID, START_DATE, END_DATE,
|
|||
|
TITLE, ASSIGNED_BRANCH_ID, DEPT_ID, SUPERIOR_EMP_ID)
|
|||
|
values ("李", "四", 19, "2022-9-22", null, "出纳员", 1, 2, 4);
|
|||
|
\end{minted}
|
|||
|
\questionandanswer[]{
|
|||
|
上机题:
|
|||
|
|
|||
|
写出向上一条记录职位修改为’出纳主任’的sql
|
|||
|
}{}
|
|||
|
\begin{minted}{sql}
|
|||
|
update employee set TITLE = "出纳主任" where EMP_ID = 19;
|
|||
|
\end{minted}
|
|||
|
\questionandanswer[]{
|
|||
|
上机题:
|
|||
|
|
|||
|
写出删除交易记录表(acc\_transaction)中交易时间(TXN\_DATE)在2012-01-01之前的交易记录的SQL
|
|||
|
}{}
|
|||
|
\begin{minted}{sql}
|
|||
|
delete from acc_transaction where TXN_DATE < "2012-01-01";
|
|||
|
\end{minted}
|
|||
|
|
|||
|
\questionandanswer[]{
|
|||
|
上机题:
|
|||
|
|
|||
|
officer 单位联系人信息表中用到了哪几种基本数据类型,这几种数据类型的特性是什么?
|
|||
|
}{
|
|||
|
int, date, varchar。
|
|||
|
|
|||
|
\begin{itemize}
|
|||
|
\item int的特性是用来存储整数,一般占用4个字节,可以存储从 -$2^{31}$ 到 $2^{31}-1$ 的整数值;
|
|||
|
\item date的特性是用来存储日期,可以进行日期相关的运算;
|
|||
|
\item varchar的特性是用来存储可变长度的字符串,其参数用来指定最大长度。
|
|||
|
\end{itemize}
|
|||
|
}
|
|||
|
|
|||
|
\questionandanswer[]{
|
|||
|
日期时间类型中,DATETIME和TIMESTAMP的最大值是一样的,并且这两个类型值都与时区有关。上述说法是否正确,请指正。
|
|||
|
}{
|
|||
|
不正确。最大值不同,datetime的最大值为9999-12-31 23:59:59,timestamp的最大值是2037-12-31 23:59:59;且datetime一般是无时区的,而timestamp一般是有时区的。
|
|||
|
}
|
|||
|
\questionandanswer[2.7]{
|
|||
|
请考虑图 \ref{fig:2-18} 的银行数据库。假设支行名称和客户ID分别能够唯一标识支行(branch)和客户(customer),但贷款(loan)和账户(account)可以与多位客户相关联。
|
|||
|
}{}
|
|||
|
\begin{figure}[H]
|
|||
|
\begin{minted}[frame=single]{text}
|
|||
|
branch(branch_name, branch_city, assets)
|
|||
|
customer (ID, customer_name, customer_street, customer_city)
|
|||
|
loan (loan_number, branch_name, amount)
|
|||
|
borrower (ID, loan_number)
|
|||
|
account (account_number, branch_name, balance)
|
|||
|
depositor (ID, account_number)
|
|||
|
\end{minted}
|
|||
|
\caption{银行数据库}\label{fig:2-18}
|
|||
|
\end{figure}
|
|||
|
\begin{enumerate}
|
|||
|
\questionandanswer[]{
|
|||
|
每个关系适当的主码属性是什么?
|
|||
|
}{}
|
|||
|
{
|
|||
|
\begin{minted}{text}
|
|||
|
branch 的主码为 branch_name
|
|||
|
customer 的主码为 ID
|
|||
|
loan 的主码为 (loan_number, branch_name)
|
|||
|
borrower 的主码为 (ID, loan_number)
|
|||
|
account 的主码为 (account_number, branch_name)
|
|||
|
depositor 的主码为 (ID, account_number)
|
|||
|
\end{minted}
|
|||
|
}
|
|||
|
\questionandanswer[]{
|
|||
|
确定每个关系适当的外码属性。
|
|||
|
}{}
|
|||
|
{
|
|||
|
\begin{minted}[fontsize=\small]{text}
|
|||
|
branch 无外码
|
|||
|
customer 无外码
|
|||
|
loan 的外码为 loan_number -> borrower.loan_number, branch_name -> branch.branch_name
|
|||
|
borrower 的外码为 ID -> customer.ID, loan_number -> loan.loan_number
|
|||
|
account 的外码为 account_number -> depositor.account_number, branch_name -> branch.branch_name
|
|||
|
depositor 的外码为 ID -> customer.ID, account_number -> account.account_number
|
|||
|
\end{minted}
|
|||
|
}
|
|||
|
\end{enumerate}
|
|||
|
|
|||
|
\questionandanswer[2.10]{
|
|||
|
请讨论命令式、函数式与声明式语言的相对优点。
|
|||
|
}{
|
|||
|
\begin{itemize}
|
|||
|
\item 命令式语言的优点是可定制化强,由于每一步计算都可以修改,所以可以自己实现某些复杂功能;
|
|||
|
\item 函数式语言的优点是不会产生副作用,即不会意外更改数据库的内容,比较便于实现原子操作;
|
|||
|
\item 声明式语言的优点是上手容易,只需要把所需的信息准确描述出来,就可以获取到信息。
|
|||
|
\end{itemize}
|
|||
|
}
|
|||
|
|
|||
|
\questionandanswer[6.5]{
|
|||
|
关系代数的\textbf{除法算子}“$\div$”定义如下:令$r(R)$和$r(S)$代表关系,且$S \subseteq R$;也就是说,模式$S$的每个属性也在模式$R$中。给定一个元组$t$,令$t[S]$代表元组$t$在$S$中属性上的投影。那么,$r \div s$是在$R-S$模式上的一个关系(也就是说,该模式包含所有在模式$R$中但不在模式$S$中的属性)。元组$t$在$r \div s$中的充分必要条件是满足以下两个条件:
|
|||
|
\begin{itemize}
|
|||
|
\item $t$在$\Pi_{R-S}(r)$中。
|
|||
|
\item 对于$s$中的每个元组$t_s$,在$r$中存在一个元组$t_r$同时满足如下条件:
|
|||
|
\begin{enumerate}
|
|||
|
\item $t_r[S] = t_s[S]$。
|
|||
|
\item $t_r[R-S] = t$。
|
|||
|
\end{enumerate}
|
|||
|
\end{itemize}
|
|||
|
给定上述定义:
|
|||
|
}{}
|
|||
|
\begin{enumerate}
|
|||
|
\questionandanswer[]{
|
|||
|
请使用除法算子写出一个关系代数表达式,来找出选修过全部计算机科学课程的所有学生的ID。(提示:在进行除法运算之前,将 takes 投影到只有ID和course\_id,并用选择表达式来生成全部计算机科学课程的course\_id集合。)
|
|||
|
}{
|
|||
|
$$
|
|||
|
\begin{aligned}
|
|||
|
&a \leftarrow \Pi_{(ID, course\_id)}takes\\
|
|||
|
&ids \leftarrow \Pi_{course\_id} (\sigma_{dept\_name="\text{Comp. Sci.}"}(course\_dept)) \\
|
|||
|
&a \div ids \\
|
|||
|
\end{aligned}
|
|||
|
$$
|
|||
|
}
|
|||
|
\questionandanswer[]{
|
|||
|
请展示如何在不使用除法的情况下用关系代数编写上述查询。(通过这样做,你也展示了如何使用其他的关系代数运算来定义除法运算。)
|
|||
|
}{
|
|||
|
|
|||
|
$$
|
|||
|
\begin{aligned}
|
|||
|
&a \leftarrow \Pi_{(ID, course\_id)}takes\\
|
|||
|
&ids \leftarrow \Pi_{course\_id} (\sigma_{dept\_name="\text{Comp. Sci.}"}(course\_dept)) \\
|
|||
|
&\Pi_{ID} (\sigma_{course\_id \in ids}(a)) \\
|
|||
|
\end{aligned}
|
|||
|
$$
|
|||
|
}
|
|||
|
\end{enumerate}
|
|||
|
\questionandanswer[6.12]{
|
|||
|
对于大学模式,请用关系代数编写如下查询:
|
|||
|
}{}
|
|||
|
\begin{figure}
|
|||
|
\centering
|
|||
|
\includegraphics[width=0.5\linewidth]{imgs/2024-09-25-21-03-29.png}
|
|||
|
\caption{由图6-15的E-R图中的实体集导出的模式}\label{fig:6-16}
|
|||
|
\end{figure}
|
|||
|
\begin{figure}
|
|||
|
\centering
|
|||
|
\includegraphics[width=0.7\linewidth]{imgs/2024-09-25-21-04-08.png}
|
|||
|
\caption{由图6-15的E-R图中的联系集导出的模式}\label{fig:6-17}
|
|||
|
\end{figure}
|
|||
|
\begin{enumerate}
|
|||
|
\questionandanswer[]{
|
|||
|
请找出物理系中每位教师的ID和姓名。
|
|||
|
}{
|
|||
|
$\Pi_{(ID, name)} (\sigma_{dept\_name="\text{Physics}"}( instructor \bowtie inst\_dept))$
|
|||
|
}
|
|||
|
\questionandanswer[]{
|
|||
|
请找出位于“Watson”教学楼的系里的每位教师的ID和姓名。
|
|||
|
}{
|
|||
|
$\Pi_{(ID, name)} (\sigma_{building="\text{Watson}"}(department\_name) \bowtie inst\_dept \bowtie instructor)$
|
|||
|
}
|
|||
|
\questionandanswer[]{
|
|||
|
请找出至少选修过“Comp. Sci.”系的一门课程的每名学生的ID和姓名。
|
|||
|
}{
|
|||
|
$\Pi_{(ID, name)} (\sigma_{dept\_name="\text{Comp. Sci.}"}(course\_dept) \bowtie takes \bowtie student)$
|
|||
|
}
|
|||
|
\questionandanswer[]{
|
|||
|
请找出在2018年至少上过一个课程段的每名学生的ID和姓名。
|
|||
|
}{
|
|||
|
$\Pi_{(ID, name)} (\sigma_{year="\text{2018}"}(sec\_course) \bowtie takes \bowtie student)$
|
|||
|
}
|
|||
|
\questionandanswer[]{
|
|||
|
请找出在2018年未上过任何一个课程段的每名学生的ID和姓名。
|
|||
|
}{
|
|||
|
$\Pi_{(ID, name)}(student) - \Pi_{(ID, name)} (\sigma_{year="\text{2018}"}(sec\_course) \bowtie takes \bowtie student)$
|
|||
|
}
|
|||
|
\end{enumerate}
|
|||
|
\end{enumerate}
|
|||
|
\end{document}
|