What is Structured query language and why is it important

structured query language advantages disadvantages and what is structured query language with example and why is it important pdf free download
Dr.AlexanderTyler Profile Pic
Dr.AlexanderTyler,India,Teacher
Published Date:21-07-2017
Your Website URL(Optional)
Comment
University College of Southeast Norway Structured Query Language Hans-Petter Halvorsen, 2016.01.08 The Tutorial is available Online: http://home.hit.no/hansha/?tutorial=sql http://home.hit.no/hansha Table of Contents 1 Introduction to SQL ........................................................................................................... 5 1.1 Data Definition Language (DDL) .................................................................................. 7 1.2 Data Manipulation Language (DML) ........................................................................... 7 2 Introduction to SQL Server ................................................................................................ 8 2.1 SQL Server Management Studio ................................................................................. 9 2.1.1 Create a new Database ...................................................................................... 10 2.1.2 Queries .............................................................................................................. 11 3 CREATE TABLE ................................................................................................................. 12 3.1 Database Modelling .................................................................................................. 14 3.2 Create Tables using the Designer Tools .................................................................... 16 3.3 SQL Constraints ......................................................................................................... 16 3.3.1 PRIMARY KEY ..................................................................................................... 17 3.3.2 FOREIGN KEY ..................................................................................................... 18 3.3.3 NOT NULL / Required Columns ......................................................................... 21 3.3.4 UNIQUE ............................................................................................................. 22 3.3.5 CHECK ................................................................................................................ 24 3.3.6 DEFAULT ............................................................................................................ 26 3.3.7 AUTO INCREMENT or IDENTITY ......................................................................... 27 3.4 ALTER TABLE ............................................................................................................. 28 4 INSERT INTO .................................................................................................................... 30 5 UPDATE ........................................................................................................................... 32 2 3 Table of Contents 6 DELETE ............................................................................................................................. 34 7 SELECT ............................................................................................................................. 36 7.1 The ORDER BY Keyword ............................................................................................ 38 7.2 SELECT DISTINCT ....................................................................................................... 39 7.3 The WHERE Clause .................................................................................................... 39 7.3.1 Operators .......................................................................................................... 40 7.3.2 LIKE Operator .................................................................................................... 40 7.3.3 IN Operator ........................................................................................................ 41 7.3.4 BETWEEN Operator ........................................................................................... 41 7.4 Wildcards .................................................................................................................. 41 7.5 AND & OR Operators ................................................................................................ 42 7.6 SELECT TOP Clause .................................................................................................... 43 7.7 Alias .......................................................................................................................... 44 7.8 Joins .......................................................................................................................... 44 7.8.1 Different SQL JOINs ........................................................................................... 45 8 SQL Scripts ....................................................................................................................... 47 8.1 Using Comments ....................................................................................................... 47 8.1.1 Single-line comment .......................................................................................... 47 8.1.2 Multiple-line comment ...................................................................................... 47 8.2 Variables ................................................................................................................... 48 8.3 Built-in Global Variables ........................................................................................... 49 8.3.1 IDENTITY ..................................................................................................... 49 8.4 Flow Control ............................................................................................................. 50 8.4.1 IF – ELSE ............................................................................................................. 50 8.4.2 WHILE ................................................................................................................ 51 8.4.3 CASE ................................................................................................................... 52 Tutorial: Structured Query Language (SQL) 4 Table of Contents 8.4.4 CURSOR ............................................................................................................. 53 9 Views ............................................................................................................................... 55 9.1 Using the Graphical Designer ................................................................................... 56 10 Stored Procedures ........................................................................................................ 60 10.1 NOCOUNT ON/NOCOUNT OFF .............................................................................. 63 11 Functions ...................................................................................................................... 65 11.1 Built-in Functions .................................................................................................. 65 11.1.1 String Functions ............................................................................................. 65 11.1.2 Date and Time Functions ............................................................................... 66 11.1.3 Mathematics and Statistics Functions ........................................................... 66 11.1.4 AVG() .............................................................................................................. 67 11.1.5 COUNT() ......................................................................................................... 67 11.1.6 The GROUP BY Statement .............................................................................. 68 11.1.7 The HAVING Clause ........................................................................................ 69 11.2 User-defined Functions ......................................................................................... 70 12 Triggers ......................................................................................................................... 71 13 Communication from other Applications ..................................................................... 74 13.1 ODBC ..................................................................................................................... 74 13.2 Microsoft Excel ...................................................................................................... 75 14 References .................................................................................................................... 77 Tutorial: Structured Query Language (SQL) 1 Introduction to SQL SQL (Structured Query Language) is a database computer language designed for managing data in relational database management systems (RDBMS). SQL, is a standardized computer language that was originally developed by IBM for querying, altering and defining relational databases, using declarative statements. SQL is pronounced /ˌɛs kjuː ˈɛl/ (letter by letter) or /ˈsiːkwəl/ (as a word). What can SQL do? • SQL can execute queries against a database • SQL can retrieve data from a database • SQL can insert records in a database • SQL can update records in a database • SQL can delete records from a database 5 6 Introduction to SQL • SQL can create new databases • SQL can create new tables in a database • SQL can create stored procedures in a database • SQL can create views in a database • SQL can set permissions on tables, procedures, and views Even if SQL is a standard, many of the database systems that exist today implement their own version of the SQL language. In this document we will use the Microsoft SQL Server as an example. There are lots of different database systems, or DBMS – Database Management Systems, such as: • Microsoft SQL Server o Enterprise, Developer versions, etc. o Express version is free of charge • Oracle • MySQL (Oracle, previously Sun Microsystems) - MySQL can be used free of charge (open source license), Web sites that use MySQL: YouTube, Wikipedia, Facebook • Microsoft Access • IBM DB2 • Sybase • … lots of other systems In this Tutorial we will focus on Microsoft SQL Server. SQL Server uses T-SQL (Transact-SQL). T-SQL is Microsoft's proprietary extension to SQL. T-SQL is very similar to standard SQL, but in addition it supports some extra functionality, built-in functions, etc. Tutorial: Structured Query Language (SQL) 7 Introduction to SQL Other useful Tutorials about databases: • Introduction to Database Systems • Database Communication in LabVIEW These Tutorials are located at: http://home.hit.no/hansha 1.1 Data Definition Language (DDL) The Data Definition Language (DDL) manages table and index structure. The most basic items of DDL are the CREATE, ALTER, RENAME and DROP statements: • CREATE creates an object (a table, for example) in the database. • DROP deletes an object in the database, usually irretrievably. • ALTER modifies the structure an existing object in various ways—for example, adding a column to an existing table. 1.2 Data Manipulation Language (DML) The Data Manipulation Language (DML) is the subset of SQL used to add, update and delete data. The acronym CRUD refers to all of the major functions that need to be implemented in a relational database application to consider it complete. Each letter in the acronym can be mapped to a standard SQL statement: Operation SQL Description Create INSERT INTO inserts new data into a database Read (Retrieve) SELECT extracts data from a database Update UPDATE updates data in a database Delete (Destroy) DELETE deletes data from a database Tutorial: Structured Query Language (SQL) 2 Introduction to SQL Server Microsoft is the vendor of SQL Server. We have different editions of SQL Server, where SQL Server Express is free to download and use. SQL Server uses T-SQL (Transact-SQL). T-SQL is Microsoft's proprietary extension to SQL. T- SQL is very similar to standard SQL, but in addition it supports some extra functionality, built- in functions, etc. T-SQL expands on the SQL standard to include procedural programming, local variables, various support functions for string processing, date processing, mathematics, etc. SQL Server consists of a Database Engine and a Management Studio (and lots of other stuff which we will not mention here). The Database engine has no graphical interface - it is just a service running in the background of your computer (preferable on the server). The Management Studio is graphical tool for configuring and viewing the information in the database. It can be installed on the server or on the client (or both). 8 9 Introduction to SQL Server 2.1 SQL Server Management Studio SQL Server Management Studio is a GUI tool included with SQL Server for configuring, managing, and administering all components within Microsoft SQL Server. The tool includes both script editors and graphical tools that work with objects and features of the server. As mentioned earlier, version of SQL Server Management Studio is also available for SQL Server Express Edition, for which it is known as SQL Server Management Studio Express. A central feature of SQL Server Management Studio is the Object Explorer, which allows the user to browse, select, and act upon any of the objects within the server. It can be used to visually observe and analyze query plans and optimize the database performance, among others. SQL Server Management Studio can also be used to create a new database, alter any existing database schema by adding or modifying tables and indexes, or analyze performance. It includes the query windows which provide a GUI based interface to write and execute queries. When creating SQL commands and queries, the “Query Editor” (select “New Query” from the Toolbar) is used (shown in the figure above). With SQL and the “Query Editor” we can do almost everything with code, but sometimes it is also a good idea to use the different Designer tools in SQL to help us do the work without coding (so much). Tutorial: Structured Query Language (SQL) 10 Introduction to SQL Server 2.1.1 Create a new Database It is quite simple to create a new database in Microsoft SQL Server. Just right-click on the “Databases” node and select “New Database…” There are lots of settings you may set regarding your database, but the only information you must fill in is the name of your database: Tutorial: Structured Query Language (SQL) 11 Introduction to SQL Server You may also use the SQL language to create a new database, but sometimes it is easier to just use the built-in features in the Management Studio. 2.1.2 Queries In order to make a new SQL query, select the “New Query” button from the Toolbar. Here we can write any kind of queries that is supported by the SQL language. Tutorial: Structured Query Language (SQL) 3 CREATE TABLE Before you start implementing your tables in the database, you should always spend some time design your tables properly using a design tool like, e.g., ERwin, Toad Data Modeler, PowerDesigner, Visio, etc. This is called Database Modeling. The CREATE TABLE statement is used to create a table in a database. Syntax: CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... ) The data type specifies what type of data the column can hold. 12 13 CREATE TABLE You have special data types for numbers, text dates, etc. Examples: • Numbers: int, float • Text/Stings: varchar(X) – where X is the length of the string • Dates: datetime • etc. Example: We want to create a table called “CUSTOMER” which has the following columns and data types: CREATE TABLE CUSTOMER ( CustomerId int IDENTITY(1,1) PRIMARY KEY, CustomerNumber int NOT NULL UNIQUE, LastName varchar(50) NOT NULL, FirstName varchar(50) NOT NULL, AreaCode int NULL, Address varchar(50) NULL, Phone varchar(50) NULL, ) GO Best practice: When creating tables you should consider following these guidelines: • Tables: Use upper case and singular form in table names – not plural, e.g., “STUDENT” (not students) • Columns: Use Pascal notation, e.g., “StudentId” • Primary Key: o If the table name is “COURSE”, name the Primary Key column “CourseId”, etc. Tutorial: Structured Query Language (SQL) 14 CREATE TABLE o “Always” use Integer and Identity(1,1) for Primary Keys. Use UNIQUE constraint for other columns that needs to be unique, e.g. RoomNumber • Specify Required Columns (NOT NULL) – i.e., which columns that need to have data or not • Standardize on few/these Data Types: int, float, varchar(x), datetime, bit • Use English for table and column names • Avoid abbreviations (Use RoomNumber – not RoomNo, RoomNr, ...) 3.1 Database Modelling As mention in the beginning of the chapter, you should always start with database modelling before you start implementing the tables in a database system. Below we see a database model in created with ERwin. With this tool we can transfer the database model as tables into different database systems, such as e.g., SQL Server. CA ERwin Data Modeler Community Edition is free with a 25 objects limit. It has support for Oracle, SQL Server, MySQL, ODBC and Sybase. Tutorial: Structured Query Language (SQL) 15 CREATE TABLE Below we see the same tables inside the design tool in SQL Server. Tutorial: Structured Query Language (SQL) 16 CREATE TABLE 3.2 Create Tables using the Designer Tools Even if you can do “everything” using the SQL language, it is sometimes easier to do it in the designer tools in the Management Studio in SQL Server. Instead of creating a script you may as well easily use the designer for creating tables. Step1: Select “New Table …”: Step2: Next, the table designer pops up where you can add columns, data types, etc. In this designer we may also specify Column Names, Data Types, etc. Step 3: Save the table by clicking the Save button. 3.3 SQL Constraints Constraints are used to limit the type of data that can go into a table. Tutorial: Structured Query Language (SQL) 17 CREATE TABLE Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement). Here are the most important constraints: • PRIMARY KEY • NOT NULL • UNIQUE • FOREIGN KEY • CHECK • DEFAULT • IDENTITY In the sections below we will explain some of these in detail. 3.3.1 PRIMARY KEY The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain unique values. It is normal to just use running numbers, like 1, 2, 3, 4, 5, … as values in Primary Key column. It is a good idea to let the system handle this for you by specifying that the Primary Key should be set to identity(1,1). IDENTITY(1,1) means the first value will be 1 and then it will increment by 1. Each table should have a primary key, and each table can have only ONE primary key. If we take a closer look at the CUSTOMER table created earlier: CREATE TABLE CUSTOMER ( CustomerId int IDENTITY(1,1) PRIMARY KEY, CustomerNumber int NOT NULL UNIQUE, LastName varchar(50) NOT NULL, FirstName varchar(50) NOT NULL, AreaCode int NULL, Address varchar(50) NULL, Phone varchar(50) NULL, ) GO As you see we use the “Primary Key” keyword to specify that a column should be the Primary Key. Tutorial: Structured Query Language (SQL) 18 CREATE TABLE Setting Primary Keys in the Designer Tools: If you use the Designer tools in SQL Server, you can easily set the primary Key in a table just by right-click and select “Set primary Key”. The primary Key column will then have a small key in front to illustrate that this column is a Primary Key. 3.3.2 FOREIGN KEY A FOREIGN KEY in one table points to a PRIMARY KEY in another table. Example: We will create a CREATE TABLE script for these tables: Tutorial: Structured Query Language (SQL) 19 CREATE TABLE SCHOOL: CREATE TABLE SCHOOL ( SchoolId int IDENTITY(1,1) PRIMARY KEY, SchoolName varchar(50) NOT NULL UNIQUE, Description varchar(1000) NULL, Address varchar(50) NULL, Phone varchar(50) NULL, PostCode varchar(50) NULL, PostAddress varchar(50) NULL, ) GO CLASS: CREATE TABLE CLASS ( ClassId int IDENTITY(1,1) PRIMARY KEY, SchoolId int NOT NULL FOREIGN KEY REFERENCES SCHOOL (SchoolId), ClassName varchar(50) NOT NULL UNIQUE, Description varchar(1000) NULL, ) GO The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables. The FOREIGN KEY constraint also prevents that invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the table it points to. Setting Foreign Keys in the Designer Tools: If you want to use the designer, right-click on the column that you want to be the Foreign Key and select “Relationships…”: Tutorial: Structured Query Language (SQL) 20 CREATE TABLE The following window pops up (Foreign Key Relationships): Click on the “Add” button and then click on the small “…” button. Then the following window pops up (Tables and Columns): Tutorial: Structured Query Language (SQL)

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.