Top Banner
1 MIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 2020 The SQL Server is a Microsoft product for a multi-user, heavy duty business use. It is comparable to MS Access and so knowledge of MS Access is transferable. However, the SQL Server is also similar to other DBMS and particularly with respect to relying extensively on SQL for performing database tasks. In this course, the SQL server is used to a limited extent for the sake of an initial exposing students to it. The SQL Server is running at IST premises of UofM. It can be accessed via a client software in the Asper lab or from anywhere else. This first part of instructions is about the lab-based access. To access the SQL Server outside the Asper lab, certain preparations must be performed beforehand. 1. Login into SQL Database Server From Lab To log into a the SQL Server for the lab do this: 1. On a lab computer desktop, find an click the icon SQL Server Management Studio 17. 2. On the form that opens up, fill the fields as shown below. Be sure to type correctly the server name: msqlacad.ad.umanitoba.ca. For the login enter your UofM userid. For the password enter your userid (starting with a capital letter), an underscore, and this number 20200131 (the date when your account was created in format yyyymmdd); for example, Johndoe_20200131. When done, click button Connect.
18

MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

Jul 05, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

1

MIS 3500 Database Systems * Instructor: Bob TravicaMIS 3500 - SQL Server Instructions

Update March 20, 2020

The SQL Server is a Microsoft product for a multi-user, heavy duty business use. It is comparable to MS Access and so knowledge of MS Access is transferable. However, the SQL Server is also similar to other DBMS and particularly with respect to relying extensively on SQL for performing database tasks.

In this course, the SQL server is used to a limited extent for the sake of an initial exposing students to it.

The SQL Server is running at IST premises of UofM. It can be accessed via a client software in the Asper lab or from anywhere else. This first part of instructions is about the lab-based access. To access the SQL Server outside the Asper lab, certain preparations must be performed beforehand.

1. Login into SQL Database Server From Lab

To log into a the SQL Server for the lab do this:

1. On a lab computer desktop, find an click the icon SQL Server Management Studio 17.2. On the form that opens up, fill the fields as shown below. Be sure to type correctly the server

name: msqlacad.ad.umanitoba.ca. For the login enter your UofM userid. For the password enter your userid (starting with a capital letter), an underscore, and this

number 20200131 (the date when your account was created in format yyyymmdd); for example, Johndoe_20200131.

When done, click button Connect.

3. Once logged into the SQL server, you may see a screen like the one below. Find your database and get into its folder. Your database name is MIS3500_youruserid; e.g., MIS3500_johndoe).

Page 2: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

2

2. Login into SQL Database Server Outside Lab

To connect to the SQL server from anywhere by using your own computer, you need to make software downloads explained in the previous lab. Then, the login procedure is like the one above except that the first step is starting the PulseSecure VPN client to encrypt the connection between your machine and he UofM's SQL server.

The login procedure recap:

1. Start PuleSecure of your computer and fill it in as shown below.

2. Click SQL Server Management Studio 17 on your computer.3. On the form that opens up, enter server name msqlacad.ad.umanitoba.ca and

your UofM userid. your password (if you never changed it, the default password is your userid starting with a

capital letter, an underscore, and this number 20200131 (the date when your account was created in format yyyymmdd); for example, Johndoe_20200131.

Click button Connect.

Page 3: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

3

4. Once logged into the SQL server, you may see a screen like the one below. Find your database and get into its folder. Your database name is MIS3500_youruserid;e.g., MIS3500_johndoe).

3. Creating Tables

3.1 Creating Table via SQL

A table can be created by the SQL CREATE query.

Run the following procedure:

1. Right-click the name of your database.

2. On the pop-up menu, select New Query.

Optionally, in the upper horizontal menu called Standard menu, click button New query.

Page 4: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

4

A space for writing a query appears on the right side of the screen. It looks like a single line but expands down by pressing the Enter key (or a text containing multiple lines).

3. Copy the SQL statement below and paste it into the query writing space:

create table Customer(CustomerID int Primary Key NOT NULL, CustFirstName varchar(20),CustMidName varchar(20), CustLastName varchar(25));

4. Click button Execute.

If all is OK, the system will respond in the Messages space: "Commands completed successfully." Otherwise, you will need to do some debugging.

To see the new table (and others if they exist), run a SELECT query on database objects:

SELECT * FROM SYSOBJECTS WHERE xtype = 'U';

The 'U' stands for user tables which exist in the system along with other objects. This command is a sure method to see the new table.

Otherwise, it should be possible to see it also in your refreshed Tables folder. Try this:

1. Right-click folder Tables in your database2. On the pop-up menu, select Refresh3. Click the small plus sign next to label Tables; your new table's name dbo.Customer should show

up under the folder File Tables (in the picture below 'xxx' stands for your user ID).

Page 5: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

5

3.2 Creating Table in Design View

Tables can be created similarly as in the MS Access Design View. Follow this procedure:

1. Right-click the image of sub-folder Table in your database.2. On the pop-up menu, select New, and then Table.

The system should open design space on the right side of the screen, which resembles Design View in MS Access.

3. Name the first column OrderID, specify the data type as Numeric (18,0), and uncheck Allow Nulls.

4. In the same way, create other columns as follows:

OrderDate, date, Allow Nulls checkedCustomerID, int, uncheck Allow Nulls

5. Save table by clicking File, and write label Order on the form that pops up. Refresh the folder to see the new table dbo.Order, or run the SELECT query as in section 3.1.

3.3 Data Entry

To enter data into table Customer, do this:

1. Right-click the name of the table shown as dbo.Customer.

Page 6: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

6

2. On the pop-up menu, select Edit Top 200 Rows. This should open up, on the right side of the screen, a table like space resembling the MS Access Datasheet View.

3. Enter data by typing over the default label null.

To close the table and save it, click the X symbol on the tables tab. Saving is automatic.

To see the new records:

1. Right-click the table name.

2. On the pop-up menu, choose Select Top 1000 Rows. As shown on the image below, the upper part on the right side of the screen show the query and lower part shows a new record.

Page 7: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

7

3.4 Renaming Table

Table Customer you have just created is for practice. Rename it so that it does not collide with another table with the same name you will work with later. To this:

1. In folder File Tables, find table dbo.Customer and right-click it.2. On the pop-menu, choose Rename and, having the table name editable, add a zero to rename the

table to Customer0.

4. Importing Table and Database

4.1 Importing Single Table From MS Access Database

If a table already exists it can be imported as a whole. Let us use tables from the PetStore database. First, you want to create a flat file from an existing table, which is a standard way of exporting databases. An appropriate flat file on the Microsoft platform is the CSV format. To make it, you will first create an Excel file, then save it as a CSV file.

To export table Customer to a CSV file, do this:

1. Start MS Access and open the PetStore database2. Mark table Customer (just click it once without opening it)3. Click ribbon External Data, and the icon Excel

4. On the form "Export – Excel Spreadsheet", navigate to the storage location for your Excel file, leave the default choice in cell File format (xlsx), and click in the checkbox Export data with formatting and layout.

5. On the next form for saving steps, just click button Close. The Excel file is now created. 6. Go to the directory in which you saved the Excel file, open it, and then save as a CSV file.

The next procedure will import the CSV file with customer data onto the SQL server.

Page 8: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

8

1. While having the SQL server window active, right-click the name of your database. 2. On the menu that pops up, click button Tasks.3. On the next menu, select Import Flat File.4. A form Import Flat File… shows up with the option Specify Input File. Mark it if it is not

already marked. Use the Browse button to navigate to the location of the CSV file. No need for any other input unless the new table should have a different name. Click button Next.

5. A preview of the data shows up. Click Next.

6. On the next view, use option Modify Columns. Make the checkmarks so that the PK is marked and the null option is disabled, while other columns can all be null. Click Next.

7. On the next view called Summary, click Finish. If all went well, the last form should show that the operation is complete. Click Close.

Page 9: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

9

By importing the CSV file, you have actually created a table on the SQL server. To check if it is really there, refresh you database and open the table as follows:

1. Right-click the name of your database (collapse the view of database objects first if necessary).2. Click option Refresh.3. Expand the view of objects and of sub-directory FileTables. 4. Mark table Customer which you have just imported. Right-click it and click option Select Top

1000 Rows. The output should be similar to the image below (some details are different than in your view).

4.2 Importing Entire MS Access Database

Here is how you can transfer the entire PetStore database into the SQL Server. You first need to create the Access file type .mdb. Do this by saving PetStore2010 as the 2000 version to get this filename extension.

Then follow this procedure:.

Page 10: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

10

1. In the SQL Server window, right-click your database icon.

2. On the pop-up menu, select Tasks and then Import Data.

3. The Data Transfer Services Wizard screen appears. Click Next.

4. Form Choose a Data Source appears. In the Data Source field, choose Microsoft Access. In field File Name, browse to the location where your PetStore2010 file is stored. If the file does not appear on the screen, type: PetStore2010. Click Open. The whole path should now appear in the field File Name. Click on Next on the bottom of the form.

Page 11: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

11

5. Screen Choose a Destination appears. The destination should be Microsoft OLE DB Provider for SQL Server. In the Server name field, enter: msqlacad.ad.umanitoba.ca. 

6. Check Use SQL Server Authentication and enter your username and password. In field Database, write the name of your database folder on the SQL Server (the image shows an example). Click Next.

7. On the next form, check the top option. Click Next.

8. On the next wizard form, check the tables to be transferred (14 of them). Click Next.

Page 12: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

12

9. On the next form, check the box Run immediately. Click Next.

10. On the next form, click Finish.

11. You should see an Executing Package screen which shows progress of the tables' transfer MS Access PetStore database to the SQL Server.

Go to your database folder on the SQL Server and check if all the tables are transferred (right-click image Tables and select Refresh). Check the tables (click the plus signs; right-click a table name, click Select Top 1000 Rows).

To get a class diagram of the PetStore database, take a look at this short video: https://tinyurl.com/u7ercvl .

Page 13: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

13

5. Searching Database4.1 SELECT Query

A SQL database is searched with a SELECT query much like any other relational database.

For example: SELECT * FROM Customer; will produce an output like below (with your own data, of course). Note that SQL server is not case-sensitive.

Knowledge of SQL is transferable across DBMS products, with some differences in syntax. Note the query below that is similar to MS Access (the LIKE operator) and also a bit different (the wild card is the percentage symbol rather than the star).

Here is one example of INNER JOIN of two tables. The query finds orders for each customer, each entity identified just by their IDs. The SQL statement is in the image below along with the output.

Other Interesting Commands

Page 14: MIS 3500 Database Systems * Instructor: Bob Travica€¦ · Web viewMIS 3500 Database Systems * Instructor: Bob Travica MIS 3500 - SQL Server Instructions Update March 20, 20 20 The

14

MS Access commands work in SQL Server. But SQL Server provides more of search power. Check this site https://www.sqlteaching.com/ for commands by the end of the menu CASE, SUBSTR, and COALESCE.

====================================================================

Help on SQL server

Modifying Table Design:

Make column StudentID to be the primary key column:

ALTER TABLE dbo.Table_Student ALTER COLUMN StudentID int NOT NULL(UPDATE [Table] SET [Column]=0 WHERE [Column] IS NULL )

Other alterations: https://www.techonthenet.com/sql_server/tables/alter_table.php

Help on data types:https://docs.microsoft.com/en-us/sql/t-sql/data-types/char-and-varchar-transact-sql?view=sql-server-2017

Other:https://docs.microsoft.com/en-us/sql/2014-toc/books-online-for-sql-server-2014?view=sql-server-2014