Download presentation
Presentation is loading. Please wait.
Published byFlorence Turner Modified over 5 years ago
1
CS4540 Special Topics in Web Development SQL and MS SQL
Chengyu Sun California State University, Los Angeles
2
A Relational DB Example
Employees EmployeeId FirstName LastName DateHired SupervisorId 1 John Doe null 2 Jane Projects ProjectMembers ProjectId Name LeaderId 1 Firestone 2 Blue ProjectId EmployeeId 1 2
3
Terminology DBMS DB DB DB Database Schema Schema Schema
Tables (relations) Views, indexes Procedures and triggers … Schema Schema Schema Database
4
DBMS Database Management System (DBMS) is a software that manages databases Common DBMS Commercial – Oracle, IBM DB2, MS SQL Server, Access Open source – MySQL, PostgreSQL
5
Database and Schema A database is a collection of data managed by a DBMS Many DBMS also support sub-collections within a database called schemas The default schema in MS SQL Server is dbo (stands for database owner) Tables, indexes, stored procedures … are often called database objects or schema elements
6
More Terminology Table (relation) Attributes (fields, columns)
StudentId Name 1001 John Doe 1002 Jane Doe Rows (Records) (Tuples) Students Table schema: Students( StudentId, Name ) Database schema: database name + table schemas
7
Client-Server Architecture
SSMS Azure Data Studio sqlcmd Applications … SQL Server DB DB DB
8
Connect to a Database Use one of the client software
Connection information Host Port (default 1433) Username Password Database/Schema See Using MS SQL Server on ECST-CSPROJ2
9
SQL Structured Query Language
Standard query language of relational databases Supported by all major relational databases with some variations Transact-SQL (T-SQL) for MS SQL Server
10
Create Tables Coding conventions Data types and literals SQL script
11
Coding Conventions Plural form for table names, and singular form for column names PascalCasing for names, and capitalize SQL keywords Use [] around names in case they contains special characters
12
Common Data Types Numeric String Date and time
bit, int, smallint, bigint real, float(n) String char(n), varchar(n) nchar(n), nvarchar(n) for Unicode Date and time date, time, datetime, datetime2
13
About IDENTITY Similar to AUTO_INCREMENT in MySQL
Can be customized with seed and increment, e.g. IDENITY(1000,1) Must set IDENTITY_INSERT to ON before inserting an explicit value to an identity column
14
SQL Literals Called Constants in MS documentation
Unicode strings are prefixed with N Default date format is 'YYYY-MM-dd'
15
SQL Script A text file contains SQL statements and comments
Usually uses .sql suffix Use sqlcmd -i <script> to run script on command line When using sqlcmd, the GO command sends the preceding statements to SQL server to execute as a batch
16
Simple Selections Example: find the employees whose last names are Doe
17
Using Functions T-SQL Functions
Example: find the names of the employees who were hired in 2015
18
Join Example: find the name of the leader of the project Blue
Equi-join Inner join
19
Aggregation Example: list the name and the number of employees for each project
20
Other Statements Change password ALTER LOGIN <username> WITH
PASSWORD = '<new_password>' OLD_PASSWORD = '<old_password>'
21
References Transact-SQL (T-SQL) Reference
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.