[Database Principle] Experiment 2 Data Query and Update

  • Experimental purpose and requirements
    1. Understand the use of script files;
    2. Master the application of SELECT statement in single table query; 
    3. Master the use of complex queries;   
    4. Master the method of multi-table connection;
    5. Learn how to use subqueries
    6. Master SQL data update operations 
    7. Master view definition, query and update operations
  • Experimental content

1. Use the script file (create.sql) to create the CAP database.

Steps:

  • Create a new query, enter the content of create.sql in the appendix into the query window, and save it as create.sql
  • analyze and execute

2. Use T-SQL to load data

  • Set up Surface Area Configurator
  • Create a new query and enter the following statement:

--The following code imports data for a specific purpose by using the openrowset function

use CAP

go

Insert into customers select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[客户$]);

Insert into agents select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[代理商$]);

insert  into products select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[产品$]);

insert into orders select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[订单$]);

If the above operations cannot be performed, import the data in the four tables by importing cap.xls in Experiment 1.

3. Query and update operations

Find the triples (cid, aid, pid) where all customers, agents and products are in the same city.

select  cid,aid,pid

from agents,customers,products

where agents.city=customers.city and customers.city=products.city

Find all the triples (cid, aid, pid) where all customers, agents and products are not in the same city (there may be two in the same city).

select  cid,aid,pid

from agents,customers,products

where agents.city!=customers.city or customers.city!=products.city

Find all aid pairs of agents in the same city.

select  a.aid,b.aid

from agents a,agents b

where a.city=b.city and a.aid!=b.aid

Find the cid value of the customer who ordered both items p01 and p07. (What if you find out the customer's cname?)

select distinct a.cid

from orders a,orders b

where  the _ pid = 'p01' or  pid = 'p07' and  a . chi = b . chi

Calculate the total sales of each product.

select pid,sum(qty)

from orders

group  by  pid

When the total amount of a certain product ordered by an agent exceeds 1000, print out all qualified products, agent IDs and the total amount.

select aid,pid,sum(qty)

from orders

group  by  aid , pid

having sum(qty)>1000

Find the name of the customer who ordered product p05.

select distinct cname

from customers,orders

where customers.cid=orders.cid and pid='p05'

Retrieve customer-agent name pairs (cname, aname) that satisfy the following conditions, where customer cname orders through agent aname.

select distinct cname,aname

from agents,customers,orders

where agents.aid=orders.aid and customers.cid=orders.cid

Find the pid value of the product ordered by at least two customers.

select distinct pid

from orders

group  by  pid 

having count(distinct cid)>1

Insert a new row into the customers table.

  1. Insert into customers(cid,cname,city) values (‘c007’,’WinDix’,’Dallas’);

Retrieve the rows in the customers table with an empty discnt value.

select*

from customers

where discnt=0

Retrieve details of customers and what they ordered. (with outer join)

        Select customers.cid,cname,ordno,month,cid,aid,pid pty,dollars

From customers left outer join product on (customers.cid=product.cid)

Retrieve all information about agents living in Duluth or Dallas. (requires implementation with IN predicate)

select  ordno from orders        

where cid in (select cid from customers                  

where city = 'Duluth') and             

aid in (select aid from agents                  

where city = ‘Dallas')               

order by ordno

Find the names and discount rates of all customers who ordered through an agent who lives in Duluth or Dallas. (requires implementation with IN predicate)

select cname name,discnt discount  

       from customers 

       where cid in ( 

       select distinct orders.cid customer number

       from agents ag inner join orders ord 

       on ag.aid = orders.aid 

       where ag.city = 'Duluth' or ag.city = 'Dallas' 

       )  

Find the cid values ​​of all customers whose discnt value is less than the discnt value of any customer who lives in Duluth.

Select cid

From customers 

Where discnt<all

(select dicint

From cuseomers

Where city=’ Duluth’)

And city<>’ Duluth’

Retrieve the names of all customers who did not order through agent a05.

Tip: You can use not in or <>all to achieve.

Select sname

From agents

Where aid not in

(select aid

From agents

Where aid=a05)

Retrieve a name containing the city where the customer is located or the city where the agent is located. (implemented using UNION)

Select city

From agents

Union

Select city

From customers

Insert a new row into the orders table.

Insert into orders(ordno,month,cid,aid,pid) values (1107,’aug’,‘c006’,’a04’,’p01’);

Create a table called swcusts that contains all customers who live in the Southwest, and insert into this table all customers from Dallas or Austin.

 Create swcusts(cid char(4) not null

Cname varchar(13),city varchar(20),

Discnt real,primary key(cid))

Insert into swcusts

Select* from customers

Where city in(‘Dallas’,’Auston’)

Increased commission rates by 10% for all agents living in New York.

Update agents

Set per=per+10

Where city=’New York’

        

Delete all agents living in New York.

Delete

From agents

Where city=’New Yourk’

Create an agentorders view that extends the rows of the orders table to include details of the ordering agent.

Creat view agentorders

As

Select ordno,momth,cid,order.aid,pid,qty,dollars,aname,city,per

From agents,orders

Where agent.aid=order.aid

Use the agentorders view to query all order information of the agent Brown

Select*

From agentorders

Where aname=’Brown’

Create a cacities view that lists all cities that are paired in the table customers and the table agents through which the customer ordered an item.

Creat view cacites

As

Select cid,cname,customers.city,agents.city

From customers,agents,orders

Where customers.cid=order.cid

And agents.aid=order.cid

Create the custs view

create view custs as select * from customers where discnt<=15.0 with check option;

Update the custs view.

Update custs set discnt=discnt+4;

database backup

Steps:

  • In the Object Explorer window, right-click the database you want to back up. Select Task from the pop-up menu, and click Backup.
  • In the above window, select the backup type as "Full".
  • Click the "Add" button to set the target properties. (It is required to keep the database backup to the user disk, such as E disk or F disk, so as to facilitate the operation of database restoration in the next experiment)

The example is saved to the f:\backup folder, and the backup file name is cap.bak

  • After setting the path, click the "OK" button. Then click the "OK" button in the "Backup" window. This will create a backup of the database at the specified location.

thinking questions

  1. How are null values ​​handled? (sorting, grouping, comparing, using aggregate functions, etc.)
  2. Problems and solutions

appendix

1. Contents of the script file (create.sql)

-- create.sql

-- SQL statements for table creation for CAP database

create database CAP

on

(name=cap_data, -- the logical name of the data file, be careful not to have the same name as the log logic

filename='d:\sql_data\cap_data.mdf' ,--physical name, note that the path must exist

size=10, -- data initial length is 5M

maxsize=50, -- the maximum length is 10M

filegrowth=5 % )--The data file grows by 5 % each time

log on

( name=cap_log, 

filename='d:\sql_data\cap_log.ldf ' , 

size=2 , 

maxsize=5 , 

filegrowth=1)

go

use CAP

go

create table customers (cid char(4) not null, cname varchar(13),

  city varchar(20), discnt real, primary key(cid));

create table agents (aid char(3) not null, aname varchar(13),

  city varchar(20), per smallint, primary key (aid));

create table products (pid char(3) not null, pname varchar(13),

  city varchar(20), quantity integer, price money,

  primary key(pid));

create table orders (ordno integer not null, month char(3),

  cid char(4), aid char(3), pid char(3),

  qty integer, dollars money, primary key(ordno));

Contents of the script file (import.sql)

Insert into customers select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[客户$]);

Insert into agents select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[代理商$]);

insert  into products select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[产品$]);

insert into orders select * from openrowset('Microsoft.Jet.OLEDB.4.0','Excel 8.0;Database=e:\cap.xls',[订单$]);

Guess you like

Origin blog.csdn.net/CE00001/article/details/130163045