Experiment 1 Create a CAP database
- Experimental purpose and requirements
- Familiar with the environment of SQL Server Management Studio (SSMS) in SQL Server 2008
- Understand the logical and physical structure of a SQL Server 2008 database
- Know how to create and delete databases
- Know how to create and drop tables
- Master simple queries
- Experimental content
There is a product ordering management system, the database is named "CAP", the initial size is 10MB, the database automatically grows, and the growth method is 1M increments; the log file is initially 2MB, and the growth is 10%. The logical file name of the database is "cap", the physical file name is "cap.mdf", and the storage path is "d:\sql_data". The logical file name of the log file is "cap_log", and the physical file name is "cap_log.ldf". The storage path is "d:\sql_data". (If there is no sql_data directory on the D drive, please create this directory first)
-
- Create a CAP database using SSMS.
Experimental steps:
- Start SSMS
In the Start menu: All Programs - SQL Server 2008 - SQL Server Management Studio. Click the "Connect" button to enter the SSMS window. If you select "mixed mode" for authentication, you need to enter the password of sa.
build database
Select the "Object Explorer" window, right-click on the database node and select New to create a CAP database.
Use SSMS to delete the CAP database created above.
Select the "Object Explorer" window, right-click the CAP database in the database node, and select "Delete" from the pop-up shortcut menu.
Create a CAP database using SQL statements.
Tip: Create a Database Using Query Analyzer
Click the "New Query" button in the first toolbar on the left side of the screen, then write the query in the query window
The SQL statement to create the CAP database is as follows:
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=1)--The data file grows by 1M each time
log on
( name=cap_log,
filename='d:\sql_data\cap_log.ldf ' ,
size=2 ,
maxsize=5 ,
filegrowth=1)
Note: When creating a query, be sure to select the correct database. Since the default database is specified in the user profile, a common mistake is to execute a query against the default database. If logged in as the admin or sa user, the default database is the master database. To ensure that the query is executed against the correct database, use use <databasename>;go at the beginning of the query to establish a connection to the database.
- Create the customers table using SSMS.
- Start SSMS
- Connect to the database engine
- Expand the "Database" node and select the CAP database
- Right-click the "Table" node and select "New Table" from the pop-up menu
Create the customer table customers, which consists of the following attributes: cid char(4), cname varchar(13), city varchar(20), discnt real. Among them, cid is the primary key (right-click cid, select "Set primary key"), and cannot take a null value.
Click the Save button, pop up:
Name it customers, OK.
Use SQL statements to create the products and orders tables.
Tip: Create a new query in the query window, the SQL statement is as follows
use CAP;
go
create table agents(aid char(3) not null, aname varchar(13),
city varchar(20), per smallint, primary key(aid));
go
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));
Note: The table created above is an empty table with no data in it. Use the use <databasename> statement, and <databasename> is case-sensitive.
Import Data.
Tip: Right-click the CAP database, select "Tasks" in the pop-up menu, and then select "Import Data".
Next select the data source table and the target table for import. Note: The four tables must correspond, for example, products correspond to products, agents correspond to agents, orders correspond to orders, and customers correspond to customers.
Use the SELECT statement to complete the following query
Find out the aid value and name of an agent living in New York.
select aid,aname
from agents
where city='New York'
Retrieves the pid values of all parts in the order record.
select pid
from products
Retrieve all information about agents living in Duluth or Dallas.
select*
from agents
where city='Duluth'or'city='Dallas'
Retrieve information on customers whose residence names begin with "D".
select*
from agents
where city='Duluth'or city='Dallas'
Retrieves the highest discount rate for all customers.
select cname,discnt
from customers
Find the total amount of all order transactions.
select sum(dollars)
from orders
Find the total order quantity of product p01.
select sum(quantity)
from products
where pid='p01'
Find the number of cities where customers live.
select sum(city)
from customers
thinking questions
What are the two authentication methods of MS SQL? Under what circumstances are each applicable?
Can the primary key be defaulted when creating a basic table?
Can the structure of the basic table be modified? How to modify? Try an example.
According to the data in the CAP database in the appendix of this experiment, establish its conceptual model (represented by ER diagram).
4. Problems and solutions
Appendix (CAP Database)