[Database principle] SQL statement practice

Article Directory

E1.

1. There are two data tables as follows, the results and field names in each table are as follows:
Book (Book) includes book number (BNo), type (BType), book title (BName), author (BAuth), unit price (BPrice), publication Company number (PNo);
publishing house (Publish) includes publishing house number (PNo), publishing house name (PName), city (PCity), telephone (PTel).
Use SQL to achieve the following functions.
(1) The name of the author of the book titled "Operating System" published in "Higher Education Press";
(2) Find the phone number of the publishing house that publishes all "novel" books for the author "Zhang Xin";
(3) ) Query the price of "Computer" books published by "Electronic Industry Press", and output the name of the publishing house and book category at the same time;
(4) Search for books with the same name that are lower in price than "Advanced Mathematics" published by "People's Posts and Telecommunications Press" Relevant information;
(5) Find the title and author of the book with the word "computer" in the title;
(6) Add the "publishing time" (BDate) item to the "books" table, and the data type is date type;
(7) Create an index with "author" in the "books" table.

//Problem 1
select BAuth
from Book
where BName='操作系统'
	and PNo=(select PNo 
				from Publish 
				where PName='高等教育出版社')

select BAuth
from Book,Publish
where Book.PNo=Publish.PNo
	and BName='操作系统'
	and PName='高等教育出版社'

//Problem 2
select PTel
from Publish,Book
where Publish.PNo=Book.PNo
	and BType='小说'
	and BAuth='张欣'

//Problem 3
select BPrice,PName,BType
from Book,Publish
where Book.PNo=Publish.PNo
	and BType='计算机'
	and PName='电子工业出版社'

// Problem 4
select *
from Book
where BName='高等数学' and 
	  BPrice<(select BPrice 
				from Book,Publish 
				where Book.PNo=Publish.PNo
					and BName='高等数学'
					and PName='人民邮电出版社')

//Problem 5
select BAuth,BName
from Book
where BName like '%计算机%'

//Problem 6
alter table Book
add
Bdate datetime

//Problem 7
create index AuthorIndex
on Book(BAuth)

E2.

Suppose there is a bookstore. The manager of the bookstore wants to manage the operating status of the bookstore and needs to establish a database, which includes two tables:
deposit books (book number, book title, publisher, edition, publication date, author, book price , Purchase price, quantity)
sales (date, book number, quantity, amount)
please use SQL to achieve the following requirements of the bookstore manager.
(1) Establish a book storage table and a sales table;
(2) Master the inventory of books, list all the titles, quantities, and balances of the current inventory (balance = purchase price × quantity, that is, the funds occupied by the inventory);
(3) Statistic total sales;
(4) List the daily sales report, including book title, quantity and total amount (the total sales of each book);
(5) Analyze the best sellers, that is, list the current period (from the current date) , The previous 30 days) The title and quantity of the book whose sales quantity is greater than 100.

//Problem 1
create table StoreBook
(
	BNo varchar(6) Primary Key,
	BName nvarchar(20),
	Publish nvarchar(20),
	Edition int,
	PubDate datetime,
	Author nvarchar(20),
	SalePrice smallmoney,
	Inprice smallmoney,
	Count int
)

create table Sale
(
	Date datetime,
	BNo varchar(6) foreign key references Book(BNo),
	Count int,
	Price smallmoney,
	constraint Sale_Prim Primary Key(Date,BNo)
)

//Problem 2
select BName Count,InPrice*Count as RestPrice
from Book

//Problem 3
select Date,SUM(Count*Price) as TotalMoney
from Sale
group by Date

//Problem 4
select BName,Count,Count*Price as TotalMoney,Date
from Book,Sale
where Book.BNo=Sale.BNo

//Problem 5
select BName,Count
from Book,Sale
where Book.BNo=Sale.BNo
	and Date+30>(select MAX(Date) from Sale)
	and Count>100

E3.

There are four basic tables S, C, SC, T as follows, and the structure is shown in Figure 3-20.
Insert picture description here
(1) Create S table with SQL DDL language, S# is the main code, and SN cannot be empty.
(2) Create a view of computer students. The attribute column of the view is composed of student ID, name, course ID and teacher ID.
(3) Retrieve the student ID of students over 20 years old in the Department of Computer Science.
(4) Search the course number and course name of the course taught by the teacher surnamed Wang.
(5) Retrieve the grades of the courses taught by Zhang San, and list SN, C# and GR.
(6) Retrieve the names, course numbers, and grades of students of courses taught by teachers whose elective income exceeds 1,000 yuan.
(7) Retrieve the names and average grades of students who have not taken C1 courses and the number of elective courses is two, and sort them in descending order of average grades.
(8) Search for the same student name and course name in any of the elective courses selected by Zhang San.
(9) S1 students took C3 as an elective, and insert this information into the SC table.
(10) Delete the records of students who have not taken any courses in the S table.

//Problem 1
create table S
(
	S# varchar(6) Primary Key,
	SN nvarchar(20) not null,
	age int,
	dept navrchar(20)
)

//Problem 2
create view Com_View(S#,SN,C#,T#)
as select S.S#,SN,SC.C#,T#
from S,SC,T
where S.S#=SC.S#
	and SC.C#=T.C#
	and dept='计算机'

//Problem 3
select S#
from S
where age>20 and dept='计算机'

//Problem 4
select T.C#,CN
from T,C
where T.C#=C.C#
	and TN like '王%'

//Problem 5
select SN,C#,GR
from S,SC
where S.S#=SC.S#
	and SN='张三'

//Problem 6
select SN,T.C#,GR
from T,SC,S
where T.C#=SC.C#
	and SC.S#=S.S#
	and (SAL+COMM)>1000

//Problem 7
select S.S#,SN,AVG(GR) as AvgScore
from SC,S
where SC.S#=S.S#
	and C# !='C1'
group by S#,SN
having count(*)=2
order by AVG(GR) desc

//Problem 8
select SN,CN
from S,C,SC
where SC.C#=C.C#
	and S.S#=SC.S#
	and C# in (select C# 
				from S,SC
				where S.S#=SC.S#
					and SN='张三')
	and SN != '张三'

//Problem 9
insert into table SC(S#,C#)
values('S1','C3')

//Problem 10
delete table S
where S# not in (select distinct S# from SC)

Some Tips.

  • create tableMiddle, student number, book number, etc., usually use nchar(x)type;
  • Name, title, department alias commonly used nvarchar(x)type, and the nchardifference is that the former non-fixed-length, and ncharlength is fixed, you listen to the tube did not understand, use would be finished.
  • When defining constraints for the combination of multiple keys, as a table constraint, it needs to be written after the definition of the attribute column, for example:
create table Sale
(
	Date datetime,
	BNo varchar(6) foreign key references Book(BNo),
	Count int,
	Price smallmoney,
	constraint Sale_Prim Primary Key(Date,BNo)
)

Creating XXX usually corresponds to the createstatement:

  • Create a database :create database db_name on ... log on ...
  • Create a data table:create table table_name (Attributes)
  • Create index :create index index_name on tb_name(Attributes)
  • Create a view :create view view_name as select...
  • Modify the definition of the data table is usually used alter, among which more detailed commands areadd\alter\drop
  • Constraints are divided into column constraints and table constraints. The former is appended to the column definition (in the create statement), and the latter is on its own line.

Guess you like

Origin blog.csdn.net/weixin_44246009/article/details/108431321