SQL Server数据库操作
通过SQL Server建库语句等,采用纯代码方式创建数据库,创建数据表,以及进行相应数据库操作,包括检索,插入,删除,修改。 以下通过一个例题说明数据库操作。 某仓储超市采用POS(PointofSale)收银机负责前台的销售收款,为及时掌握销售信息,并依此指导
通过SQL Server建库语句等,采用纯代码方式创建数据库,创建数据表,以及进行相应数据库操作,包括检索,插入,删除,修改。
以下通过一个例题说明数据库操作。
某仓储超市采用POS(Point of Sale)收银机负责前台的销售收款,为及时掌握销售信息,并依此指导进货,拟建立商品进、销、存数据库信息管理系统。经过系统需求分析、概念结构设计和逻辑结构设计,可以简化得到如下一组关系模式(其中 表示主键, 表示外键):
积分卡(用户编号,用户名,累积消费金额,积分点)
销售详单(销售流水号,商品编码,数量,金额,用户编号,收银员,时间)
销售日汇总(日期,商品编码,数量)
存货表(商品编码,数量)
进货表(送货号码,商品编码,数量,日期)
商品(商品编码,商品名称,单价)
请在SQL Server的查询分析器中按要求完成如下各题:
1、 创建名为Supermarket的数据库,数据文件名取为:Supermarket_data.mdf,日志文件名取为:Supermarket_log.ldf。
2、 按表1-6要求创建6张数据表,并为每张表设置主键码和外键码(若有的话)。
表1 Integralcard积分卡信息表
列名 |
数据类型 |
可否为空 |
说明 |
User_id |
char(10) |
Not null |
用户编号 |
User_name |
varchar(20) |
Not null |
用户名 |
Cumulative_consumption |
numeric(8,2) |
Not null |
累计消费金额 |
Integral_point |
numeric(5,0) |
Not null |
积分点 |
表2 Salesdetails 销售详单信息表
列名 |
数据类型 |
可否为空 |
说明 |
sales_id |
char(10) |
Not null |
销售流水号 |
commodity_code |
char(10) |
Not null |
商品编码 |
number |
numeric(4,0) |
null |
数量 |
amount |
numeric(9,2) |
null |
金额 |
User_id |
char(10) |
Not null |
用户编号 |
cashier |
varchar(20) |
null |
收银员 |
sd_time |
datetime |
null |
时间 |
表3 Salesdatesummary 销售日汇总信息表
列名 |
数据类型 |
可否为空 |
说明 |
sds_date |
datetime |
Not null |
日期 |
commodity_code |
char(10) |
Not null |
商品编码 |
number |
numeric(4,0) |
null |
数量 |
表4 Inventorylist存货信息表
列名 |
数据类型 |
可否为空 |
说明 |
commodity_code |
char(10) |
Not null |
商品编码 |
number |
numeric(4,0) |
null |
数量 |
表5 Purchasetable进货信息表
列名 |
数据类型 |
可否为空 |
说明 |
delivery_number |
char(10) |
Not null |
送货号码 |
commodity_code |
char(10) |
Not null |
商品编码 |
number |
numeric(4,0) |
null |
数量 |
pt_date |
datetime |
Not null |
日期 |
表6 Commodity商品信息表
列名 |
数据类型 |
可否为空 |
说明 |
commodity_code |
char(10) |
Not null |
商品编码 |
commodity_name |
varchar(10) |
Not null |
商品名称 |
commodity_price |
numeric(7,2) |
Not null |
商品单价 |
3、在建好的6张表中,利用对象资源管理器分别输入和更新若干条记录,要求主键码不能为空和重复,外键码只能取另一张表的主键码之一。
4、针对该数据库的6张表,完成如下10个查询请求:
(1)查询用户编号为’yh23001011’的用户的用户名、累积消费金额和积分点;
(2)查询’张三’用户所购的全部商品的商品编码、商品名称、单价、数量和金额;
(3)查询2016年4月各类商品销售数量的排行榜,要求显示商品编号、商品名称和数量(按降序排列);
(4)根据销售详单中的销售流水号’xs80020001’和商品编码’sp03004561’,对存货表中的数量进行更新;
(5)根据进货表中的送货号码’sh00012288’和商品编码’sp03006677’, 对存货表中的数量进行更新;
(6)统计2016年4月中每一天的销售金额,要求显示日期、销售金额(按降序排列)。
5、针对该数据库的6张表,定义如下2个视图:
(1)定义一个商品存货的视图Commodity_Inventorylist,属性包括商品编码、商品名称、单价和数量;
(2)定义一个用户购买商品的详细清单User_Purchase_Details, 属性包括用户编号、用户名、商品编码、商品名称、单价和数量。
--创建数据库 --创建名为Supermarket的数据库,数据文件名取为:Supermarket_data.mdf,日志文件名取为:Supermarket_log.ldf。 USE master--使用系统 GO CREATE DATABASE Supermarket --创建数据库 ON PRIMARY --主文件 ( NAME='Supermarket_data', --文件名 FILENAME='D:\SQLProject\Supermarket_data.mdf',--路径 SIZE=5MB,--初始大小 MAXSIZE=100MB,--最大容量 FILEGROWTH=10%--增长速度 ) LOG ON--日志文件 ( NAME='Supermarket_log', FILENAME='D:\SQLProject\Supermarket_log.ldf', SIZE=5MB, FILEGROWTH=0 ) GO --创建你数据库表 --模式(其中 表示主键, 表示外键): --积分卡(用户编号,用户名,累积消费金额,积分点) --销售详单(销售流水号,商品编码,数量,金额,用户编号,收银员,时间) --销售日汇总(日期,商品编码,数量) --存货表(商品编码,数量) --进货表(送货号码,商品编码,数量,日期) --商品(商品编码,商品名称,单价) use Supermarket go --积分卡(用户编号,用户名,累积消费金额,积分点) create table Integralcard ( User_id char(10) primary key not null, User_name varchar(20) not null, Cumulative_consumption numeric(8,2) not null, Integral_point numeric(5,0) not null ) go --销售详单(销售流水号,商品编码,数量,金额,用户编号,收银员,时间) use Supermarket go create table Salesdetails ( sales_id char(10) not null, commodity_code char(10) not null foreign key(commodity_code) references Commodity(commodity_code) on delete cascade, number numeric(4,0) null, amount numeric(9,2) null, User_id char(10) not null foreign key(User_id) references Integralcard(User_id) on delete cascade, cashier varchar(20) null, sd_time datetime null ) go --外键 ALTER TABLE Salesdetails ADD CONSTRAINT Salesdetails_KEY PRIMARY KEY(sales_id,commodity_code,User_id) Go --销售日汇总(日期,商品编码,数量) use Supermarket go create table Salesdatesummary ( sds_date datetime not null, commodity_code char(10) not null foreign key(commodity_code) references Commodity(commodity_code) on delete cascade, number numeric(4,0) null ) go ALTER TABLE Salesdatesummary ADD CONSTRAINT Salesdatesummary_KEY PRIMARY KEY(commodity_code) Go --存货表(商品编码,数量) use Supermarket go create table Inventorylist ( commodity_code char(10) not null foreign key(commodity_code) references Commodity(commodity_code) on delete cascade, number numeric(4,0) null ) go ALTER TABLE Inventorylist ADD CONSTRAINT Inventorylist_KEY PRIMARY KEY(commodity_code) Go --进货表(送货号码,商品编码,数量,日期) use Supermarket go create table Purchasetable ( delivery_number char(10) not null, commodity_code char(10) not null foreign key(commodity_code) references Commodity(commodity_code) on delete cascade, number numeric(4,0) null, pt_date datetime not null ) go --外键 ALTER TABLE Purchasetable ADD CONSTRAINT Purchasetable_KEY PRIMARY KEY(delivery_number,commodity_code) Go --商品(商品编码,商品名称,单价) use Supermarket go create table Commodity ( commodity_code char(10) primary key not null, commodity_name varchar(10) not null, commodity_price numeric(7,2) not null ) go ----------------------------------------------- --查询用户编号为’yh23001011’的用户的用户名、累积消费金额和积分点 use Supermarket select User_name as 姓名,Cumulative_consumption as 累计消费金额,Integral_point as 积分点 from Integralcard where User_id='yh23001011' go --查询’张三’用户所购的全部商品的商品编码、商品名称、单价、数量和金额; use Supermarket select com.commodity_code,com.commodity_name,com.commodity_price,sal.number,sal.amount from Commodity com,Salesdetails sal,Integralcard ca where com.commodity_code=sal.commodity_code and sal.User_id=ca.User_id and ca.User_name='张三' go --查询2016年4月各类商品销售数量的排行榜,要求显示商品编号、商品名称和数量(按降序排列) use Supermarket select com.commodity_code,com.commodity_name,sal.number,sal.sd_time from Commodity com,Salesdetails sal where com.commodity_code=sal.commodity_code and sal.sd_time between '2016-04-01' and '2016-04-30' order by sal.number desc go --查看结果 use Supermarket select number from Inventorylist where Inventorylist.commodity_code='sp03004561' go --根据销售详单中的销售流水号’xs80020001’和商品编码’sp03004561’,对存货表中的数量进行更新 use Supermarket update Inventorylist set number= number - (select sal.number from Salesdetails sal where sal.sales_id='xs80020001' and sal.commodity_code='sp03004561') go --查看更新结果 use Supermarket select number from Inventorylist where Inventorylist.commodity_code='sp03004561' go --查看结果 use Supermarket select number from Inventorylist where Inventorylist.commodity_code='sp03006677' go --根据进货表中的送货号码’sh00012288’和商品编码’sp03006677’, 对存货表中的数量进行更新 use Supermarket update Inventorylist set Inventorylist.number=Inventorylist.number +(select pur.number from Purchasetable pur where pur.delivery_number='sh00012288' and pur.commodity_code='sp03006677') go --查看更新结果 use Supermarket select number from Inventorylist where commodity_code='sp03006677' go --统计2016年4月中每一天的销售金额,要求显示日期、销售金额(按降序排列) use Supermarket select sal.sds_date,salemoney=com.commodity_price*sal.number from Commodity com,Salesdatesummary sal where com.commodity_code=sal.commodity_code and sal.sds_date>='2016-04-01' and sal.sds_date <p><span style="font-size:18px"><br> </span></p> <p><span style="font-size:18px"><span style="color:#ff6600">数据库实现截图:</span></span></p> <br> <img src="/static/imghw/default1.png" data-src="http://img.blog.csdn.net/20160519185100347?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" class="lazy" alt=""><br> <p><span style="font-size:18px"><br> </span></p>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.
