Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating and Populating a MS SQLServer Database Presented By: Dr. Adam P. Anthony.

Similar presentations


Presentation on theme: "Creating and Populating a MS SQLServer Database Presented By: Dr. Adam P. Anthony."— Presentation transcript:

1 Creating and Populating a MS SQLServer Database Presented By: Dr. Adam P. Anthony

2 Prerequisites 1.Basic knowledge of SQL DDL (data definition language) 2.Basic knowledge of SQL DML (data manipulation language) 3.Standard professional installation of Visual Studio (incl. MS SQLServer 2005 Express)

3 Getting Started Ways to access SQLServer on a local machine: – Command line tool (SQLCMD) – SQLServer Management Studio Express – Running Scripts in Visual Studio Running remotely: – Not covered in this class – Depends on server configuration (ask sysadmin) – Once connected, there is no difference, functionally, from working on a local instance

4 SQLExpress Command Line Start  Run… – SQLCMD -S localhost\SQLExpress Load your database first: – use Type your commands at the prompt: 1> SELECT * FROM Student You can break up lines, without any trouble: 1> Select * 2> FROM STUDENT Cycle through previously typed commands using the arrow keys

5 The GO Command Type as much SQL as you want. Nothing will happen until you type GO Also usable in SQL scripts: – Less useful (not needed to allow line breaks) – Reports “number of rows affected” after each GO statement

6 Visual Studio SQL Projects Many choices, make sure you get the right one! – File  New  Project – Expand the ‘Other Project Types’ option, pick ‘Database Project’ First window: Add a database reference (OR choose data source) – “Add New Reference” Next window: New database reference – Server name: localhost\SQLExpress – Select or enter a database name: either pick an existing, or type in a new, name for DB you want to create – If you are just practicing/doing homework and don’t want the data to persist, use tempdb CAUTION: tempdb is erased when you end your session!

7 VS Database Project Window Three folders categorize SQL scripts: – Create (creating databases, tables) – Change (loading data, changing structures) – Queries (everything else!) To get started: – Right click on Create Scripts and choose “Add SQL Script” Create Tables.sql sounds like a good file name – DO NOT ADD A QUERY!! OK, you can add one if you want to but I’m not going to teach you how to use it. When the script is finished, right click on the file, and chose ‘run’

8 Writing Code (Finally!) For This Class Scripts vs. Command line: – Use command line to practice/test features you don’t understand – Use scripts to do your homework (e.g. I’ll expect.sql files) In scripts: – Type your SQL just as you see it in the book – Indentation, line breaks are your friend! – Use a double dash (--) to leave an inline comment – Comments are Required!

9 SQLServer 2005 Data Types Frequently-used types: int, float, numeric, datetime, varchar Some types have only subtle differences: char  max size = 8000 varchar  max size = 2 32 numeric == decimal float == real Numeric vs. float: Numeric uses soft representation, float uses IEEE hardware representation More info on types: http://technet.microsoft.com/en-us/library/ms187912%28SQL.90%29.aspx

10 Creating an Entity Table Subtle difference: no colon between column name, type Primary key, foreign key syntax is same as SQL standard Naming constraints: – Sometimes SQLServer requires you to drop constraints, and to do that you need to know the name CREATE TABLE student( snum numeric(9), sname varchar(30), major varchar(25), standing varchar(2), age numeric(3), CONSTRAINT student_pk PRIMARY KEY (snum) ); GO

11 Inserting Data Standard notation for a single insert: INSERT INTO student (snum,sname,age) VALUES (111223333,"default student",18) For more efficient bulk-inserts from a text file: BULK INSERT student FROM –- must be full path 'C:\bookTableScripts\student.txt' -- what separates the value of each field? WITH (FIELDTERMINATOR = ',')

12 Additional Resources SQL in a nutshell: http://proquest.safaribooksonline.com/?uiCo de=ohlink&xmlId=9780596155322 http://proquest.safaribooksonline.com/?uiCo de=ohlink&xmlId=9780596155322 Learning SQL on SQL Server 2005: http://proquest.safaribooksonline.com/?uiCo de=ohlink&xmlId=0596102151 http://proquest.safaribooksonline.com/?uiCo de=ohlink&xmlId=0596102151 MSDN SQL Server 2005 Reference: http://technet.microsoft.com/en- us/library/ms130214%28SQL.90%29.aspx http://technet.microsoft.com/en- us/library/ms130214%28SQL.90%29.aspx

13 Practice Session Student snum Numeric(9) sname Varchar(30) major Varchar(30) standing Varchar(2) age int Faculty fid Numeric(9) fname Varchar(30) deptid Varchar(4) Class fid Numeric(9) name Varchar(40) meets_at Varchar(20) room Varchar(10) Teaches Enrolled


Download ppt "Creating and Populating a MS SQLServer Database Presented By: Dr. Adam P. Anthony."

Similar presentations


Ads by Google