Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Introduction.

Similar presentations


Presentation on theme: "Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Introduction."— Presentation transcript:

1 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Introduction 1 Microsoft Enterprise Consortium Microsoft Enterprise Consortium: http://enterprise.waltoncollege.uark.eduhttp://enterprise.waltoncollege.uark.edu Microsoft Faculty Connection/Faculty Resource Center http://www.facultyresourcecenter.comhttp://www.facultyresourcecenter.com

2 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What you’ll need … This is the first in a series of presentations about SQL fundamentals. For the remaining presentations in this series, you will need a user account from the Microsoft Enterprise Consortium. Teachers can request these accounts for students. ◦The MEC URL is provided at the beginning and end of this presentation. 2

3 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Structured Query Language Structure Query Language - SQL ◦a.k.a. SQL—say each letter or the word “sequel” The core SQL commands are consistent across most DBMS software. The American National Standards Institute (ANSI) sets standards for SQL compliance. Each DBMS, however, does have some SQL variations. ◦If you write an SQL script—a set of statements or commands—for one DBMS, it is possible that it will not run in another DBMS without some revision. 3

4 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Transact-SQL & MSSMS The SQL used in this series will be either in a generic form compatible with most DBMSs or in Transact-SQL, which is specifically for Microsoft’s SQL Server. T-SQL – This shorthand for Transact-SQL may be used in this series of lessons. Microsoft SQL Server Management Studio – This is the software used to connect to SQL Server and provides interfaces for viewing & creating databases, writing & executing SQL, and many other tools. MSSMS may be used as an abbreviation. 4

5 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL – Subsets of statements Data Definition Language (DDL) ◦The set of SQL statements used to define objects in the database. ◦For example, the statement to create a table is a DDL statement. Creating objects such as a table, view or index are DDL statements. Data Manipulation Language (DML) ◦The set of SQL statements used to work with the database in a database. ◦For example, the statement to retrieve rows from a customer table, update a customer’s phone number or add a new customer are DML statements. 5

6 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Data Definition Language (DDL) The DDL is a subset of SQL statements used to create and maintain database objects such as tables. CREATE TABLE ◦Below is an example of a CREATE TABLE statement that creates a TEAMS table with:  2 required columns,  1 optional column  A primary key constraint, and  A unique constraint. CREATE TABLE teams ( teamidVARCHAR(8)NOT NULL, team_nameVARCHAR(30)NOT NULL, projectVARCHAR(45), CONSTRAINTpk_teamid PRIMARY KEY (teamid), CONSTRAINTuniq_team UNIQUE (team_name) ); 6

7 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium DDL – CREATE TABLE Each column must be assigned a data type. VARCHAR – This data type is used for variable width alphanumeric fields. The width should be defined. Use NOT NULL to define whether a column must have data or not. CREATE TABLE teams ( teamidVARCHAR(8)NOT NULL, team_nameVARCHAR(30)NOT NULL, projectVARCHAR(45), CONSTRAINTpk_teamid PRIMARY KEY (teamid), CONSTRAINTuniq_team UNIQUE (team_name) ); 7

8 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium DDL – Constraints CONSTRAINT … PRIMARY KEY – This constraint clause lists which field(s) is the primary key. Every table should have a primary key. ◦In this example, teamid is the primary key. ◦PK_TEAMID is the name of the constraint. CONSTRAINT … UNIQUE – Even though team_name is not the primary key, you can still define the column as unique. ◦No two teams can have the same name in this table. CREATE TABLE teams ( teamidVARCHAR(8)NOT NULL, team_nameVARCHAR(30)NOT NULL, projectVARCHAR(45), CONSTRAINTpk_teamid PRIMARY KEY (teamid), CONSTRAINTuniq_team UNIQUE (team_name) ); 8

9 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium DDL – Foreign Key This CREATE TABLE statement below shows how a foreign key constraint is defined. CONSTRAINT … FOREIGN KEY … REFERENCES ◦FK_TEAMID is the name of the constraint. ◦STD_TEAMID is the field name of the foreign key. ◦The foreign key field references the primary key in the TEAMS table. Notice that the primary key field in TEAMS is not listed. It was already defined when the TEAMS table was created. CREATE TABLE students ( stdid VARCHAR(10)NOT NULL, stdfname VARCHAR(12)NOT NULL, stdlname VARCHAR(13)NOT NULL, stdmajor VARCHAR(6), std_teamIDVARCHAR(8), CONSTRAINTpk_stdid PRIMARY KEY (stdid), CONSTRAINTfk_teamid FOREIGN KEY (std_teamid) REFERENCES teams ); 9

10 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Relational Model – Creating relationships in the database 10 A data model shows the sequence in which tables are created in the database. In a one-to-many relationship, the table on the “one” side is created first so that it already exists when the foreign key constraint is created in the “many” table. TEAMS is created before STUDENTS. STUDENTS is created before EVALUATIONS. EVAL_ITEMS and EVALUATIONS are created before EVAL_ITEMS_SCORES. TEAMSSTUDENTS EVALUATIONSEVAL_ITEMS_SCORES EVAL_ITEMS

11 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Databases for this SQL series For this series of lessons and exercises, we will start with these databases. AdventureWorks Light & Adventure Works ◦These are existing databases available through your MEC account. Student-Teams ◦You will create this database from SQL scripts provided. There are 3 script files. 1.A file to create 5 tables. 2.A file to insert data into the tables. 3.A file to drop all the tables, if needed. ◦You will probably only use the first two files but the third file enables you to start over, if you need to. ◦There is a PowerPoint show and video that walks through the first two steps. 11

12 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium What was covered … ANSI Transact-SQL (T-SQL) Microsoft SQL Server Management Studio (MSSMS) SQL ◦DDL ◦DML DDL ◦CREATE TABLE  Define columns  Define constraints Databases used in this SQL series ◦AdventureWorks (pre-existing) ◦Student-Teams (to be created) 12

13 Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium Resources http://enterprise.waltoncollege.uark.edu/mec.asp Microsoft Faculty Connection—Faculty Resource Center http://www.facultyresourcecenter.com/ http://www.facultyresourcecenter.com/ Microsoft Transact-SQL Reference http://msdn.microsoft.com/en-us/library/aa299742(v=SQL.80).aspx 13


Download ppt "Prepared by Jennifer Kreie, New Mexico State UniversityHosted by the University of Arkansas Microsoft Enterprise Consortium SQL Fundamentals Introduction."

Similar presentations


Ads by Google