通过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/imghwm/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>

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools