跳过正文

数据库-c1&2作业

·251 字· loading · loading ·
Masterlong
作者
Masterlong
熬夜,但是世界之夜
目录
数据库系统 - 这篇文章属于一个选集。
§ 3: 本文

Chapter 1
#

  • List at least two reasons why DBMS use a declarative language such as SQL instead of just providing a library of functions in languages such as C, Python, or Java for data manipulation. Write some code using these languages for data manipulation and discuss their weaknesses.

    列出两个原因,为什么 DBMS 使用类似 SQL 这样的声明式语言,而不仅仅是提供一 组 C、Python 或 Java 函数库来进行数据操作。使用这些语言编写一些数据操作代码, 并讨论它们的缺点。

    • 答:

      • ① 简易清晰:SQL作为一种声明性语言,使得用户能专注于操作数据本身,而不用考虑语言实现的功能细节。对于非专业程序员更加友好,减轻了数据库使用者的操作负担;同时,这类语言的专门性也保证了数据库操作不与通用编程语言中的功能相耦合。

      • ② 专门优化:DBMS被尽量设计为以最有效的方式执行数据操作,不必由操作者自己参与。C、Python或Java等语言的函数库将要求用户编写代码来访问和操作数据,而这些代码不会对被访问的特定数据和访问模式进行优化。


      • SQLAlchemy就是这样的一个函数库。数据库操作是通过”声明式映射“进行的。对于关系表中出现的具体属性和数据实例,都需要映射到程序语言中的特定对象上来进行操作。这使得数据操作行为与程序运行过程中的具体数据结构和算法行为高度绑定。

      # 这是在python中使用SQLAlchemy提供的函数库操作sqlite数据库的例子
      from sqlalchemy import create_engine, Column, Integer, String
      from sqlalchemy.orm import sessionmaker
      from sqlalchemy.ext.declarative import declarative_base
      
      Base = declarative_base()
      
      class User(Base):
          __tablename__ = 'users'
      	# 创建表的三个属性
          id = Column(Integer, primary_key=True)
          name = Column(String)
          age = Column(Integer)
      
      engine = create_engine('sqlite:///example.db')
      Base.metadata.create_all(engine)
      
      Session = sessionmaker(bind=engine)
      session = Session()
      
      # 插入一个元组
      new_user = User(name='John', age=25)
      session.add(new_user)
      session.commit()
      

Chapter 2
#

image-20230301222447173
  1. Output the product name and price information for items in the shopping list with a quantity of 2 or more. (输出购物清单中购买数量大于等于 2 的商品名称和单价信息)

    答:(shopping_list和product对应题目两张表)

    $π_{product\_name, price}((σ_{quantity≥2}(shopping\_list))⨝product)$
    #

  2. Output the product name and price information for items in the shopping list with a quantity of 2 or more and a price of 1 or more.(输出 购物清单中购买数量大于等于 2 且商品单价大于等于 1 元的商品名称和单价信息)

    商品名字可以重复

    答:

    $π_{product\_name, price}(σ_{price≥1}((σ_{quantity≥2}(shopping\_list))⨝product))$
    #

  3. Use the division operation to find the name of products that are not eligible for any discounts. And show how to write the same query without using division 使用除法运算查找不符合任何折扣条件的产品名。并展示如何在不使用除法的情况下编写相同的查询语句(通过这样做,您将使用其他关系代数操作定义除法运算)。

    答:

    $a = π_{product\_name,product\_id}(product)$
    #

    $b = π_{product\_id}(discount)$
    #

    $c = π_{product\_id}(σ_{product\_id=discount\_id}(a⨝b))$
    #

    $(π_{product\_name}(product)) - (a÷c)$
    #


    $a = π_{product\_name,product\_id}(product)$
    #

    $b = π_{product\_id}(discount)$
    #

    $c = π_{product\_name, product\_id}(σ_{product\_id=discount\_id}(a⨝b))$
    #

    $π_{product\_name}(a - c)$
    #

数据库系统 - 这篇文章属于一个选集。
§ 3: 本文

相关文章

数据库-1
·125 字· loading · loading
数据库-2
·64 字· loading · loading
数据库-3
·46 字· loading · loading