CS4540 Special Topics in Web Development SQL and MS SQL Chengyu Sun California State University, Los Angeles
A Relational DB Example Employees EmployeeId FirstName LastName DateHired SupervisorId 1 John Doe 2015-01-10 null 2 Jane 2015-02-20 Projects ProjectMembers ProjectId Name LeaderId 1 Firestone 2 Blue ProjectId EmployeeId 1 2
Terminology DBMS DB DB DB Database Schema Schema Schema Tables (relations) Views, indexes Procedures and triggers … Schema Schema Schema Database
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
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
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
Client-Server Architecture SSMS Azure Data Studio sqlcmd Applications … SQL Server DB DB DB
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
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
Create Tables Coding conventions Data types and literals SQL script
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
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
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
SQL Literals Called Constants in MS documentation Unicode strings are prefixed with N Default date format is 'YYYY-MM-dd'
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
Simple Selections Example: find the employees whose last names are Doe
Using Functions T-SQL Functions Example: find the names of the employees who were hired in 2015
Join Example: find the name of the leader of the project Blue Equi-join Inner join
Aggregation Example: list the name and the number of employees for each project
Other Statements Change password ALTER LOGIN <username> WITH PASSWORD = '<new_password>' OLD_PASSWORD = '<old_password>'
References Transact-SQL (T-SQL) Reference