CHAPTER II- INTRODUCTION TO STRUCTURED QUERY LANGUAGE WHAT IS SQLyog? - SQLyog provides you with powerful means to manage your MySQL databases. Is a GUI tool for the RDBMS MySQL Developed by Webyog, Inc. trusted by 2.5 million users around the world. Database Connection Making a New Connection After you start SQLyog, you have to enter the details of MySQL server you want to connect to. You must connect to a MySQL server before you can perform any activities with the software. If you are using SQLyog for the first time then you have to first create an instance of your database connection. You can do this by clicking on New button in the connection manager dialog New- Click the new button. In the name field (figure shown right) type the name by which you will recognize the connection. By default SQLyog will create an instance with default values (e.g. localhost, root). Save- This button allows you to save any change in the connection details. DeleteClick this button to Delete the currently selected connection. Click Yes to Delete. Rename- Click the rename button to rename a connection. Note that Connection Names are names used internally by SQLyog only (and not MySQL). Connection Names are used by SQLyog for storing the connection details. Also, certain SQLyog functionalities will make use of the connection name as stored by SQLyog. ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0 SQLyog USER INTERFACE Status Bar Title Bar The title bar shows the connection name and the host name of current connection. Object Browser This window gives you complete details of the server you are connected to in a tree format. Expanding database node will show the table(s), view(s), store procedure(s), function(s), trigger(s) and event(s) in the database and expanding the table name node will show the columns and indexes available for the table. You may Show/Hide Object Browser through Ctrl+SHift+1 buttons. When you right click on any object in the Object Browser, you will get a context sensitive popup menu. The options in the popup menu allow you to perform different operations on the selected object. ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0 SQL Window Here you write your SQL commands that you want to execute. Result Window This window shows Results of Query which you can edit in an easy to use Excel like grid interface. It also shows you information about Databases and Tables and all the query(s) that has been executed in the current connection. The window has three permanent tabs - Data, Object and History and two context specific tabs - Result and Message. Message Tab It shows the information about the various query(s) executed (e.g. number of row(s) returned/ updated / modified etc.) All those data are as they are returned in the 'status message' sent by the MySQL server after (successful or unsuccessful) execution of the query, but before transmission of data has started. Status Bar The status bar is divided into 5 parts showing information about 1. General Information: Information about work being done in SQLyog, description of the menu item selected. 2. Process Time: Time taken (in hh:mm:ss:ms) to execute the query i.e. Execution time and Total time. Note: In HTTP connection only Total time is displayed. 3. Number of rows in the currently selected Resultset. 4. Line number and column number in which the cursor is located in different Edit windows. 5. Number of connections which have been made in SQLyog. Executing SQL Queries You can execute queries in SQLyog in following four ways: Execute Current Query: To execute a particular query in a batch, just place the cursor on the query to be executed (i.e. before the semicolon separating the query from the next query). Select Edit -> Execute Query -> Execute Current Query (or press F9 or click the 'single green arrow' icon in the icon bar). Only the current query would be executed. Execute All Queries: To execute the whole batch of queries entered in the SQL window. Select Edit -> Execute Query -> Execute All Queries (or press Shift+F9 or click the 'double green arrow' icon in the icon bar). All queries would be executed one by one, showing result set in separate tab window. ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0 Introduction to SQL SQL is a standard language for accessing and manipulating databases. What is SQL? SQL stands for Structured Query Language SQL lets you access and manipulate databases SQL became a standard of the American National Standards Institute (ANSI) in 1986, and of the International Organization for Standardization (ISO) in 1987 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 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 SQL is a Standard - BUT.... Although SQL is an ANSI/ISO standard, there are different versions of the SQL language. However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner. The SQL CREATE DATABASE Statement The CREATE DATABASE statement is used to create a new SQL database. Syntax CREATE DATABASE databasename; CREATE DATABASE Example The following SQL statement creates a database called "testDB": Example CREATE DATABASE testDB; The SQL CREATE TABLE Statement The CREATE TABLE statement is used to create a new table in a database. Syntax CREATE TABLE table_name ( column1 datatype, ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0 column2 datatype, column3 datatype, .... ); The column parameters specify the names of the columns of the table. The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.). SQL CREATE TABLE Example The following example creates a table called "Persons" that contains five columns: PersonID, LastName, FirstName, Address, and City: Example CREATE TABLE Persons ( PersonID int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) ); The PersonID column is of type int and will hold an integer. The LastName, FirstName, Address, and City columns are of type varchar and will hold characters, and the maximum length for these fields is 255 characters The SQL INSERT INTO Statement The INSERT INTO statement is used to insert new records in a table. INSERT INTO Syntax It is possible to write the INSERT INTO statement in two ways. The first way specifies both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); INSERT INTO Example The following SQL statement inserts a new record in the "Customers" table: Example INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country) VALUES ('Cardinal', 'Tom B. Erichsen', 'Skagen 21', 'Stavanger', '4006', 'Norway'); ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0 CHAPTER III- ADVANCED DATA DEFINITION COMMANDS SQL ALTER TABLE Statement The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table. ALTER TABLE - ADD Column To add a column in a table, use the following syntax: ALTER TABLE table_name ADD column_name datatype; The following SQL adds an "Email" column to the "Customers" table: Example ALTER TABLE Customers ADD Email varchar(255); ALTER TABLE - DROP COLUMN To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column): ALTER TABLE table_name DROP COLUMN column_name; The following SQL deletes the "Email" column from the "Customers" table: Example ALTER TABLE Customers DROP COLUMN Email; ALTER TABLE - ALTER/MODIFY COLUMN To change the data type of a column in a table, use the following syntax: ALTER TABLE table_name MODIFY COLUMN column_name datatype; Change Data Type Example Now we want to change the data type of the column named "DateOfBirth" in the "Persons" table. We use the following SQL statement: ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0 ALTER TABLE Persons MODIFY COLUMN DateOfBirth varchar (40); Example ALTER TABLE Persons MODIFY COLUMN DateOfBirth year; ADD MULTIPLE COLUMNS IN TABLE Syntax To add multiple columns to an existing table, the SQL ALTER TABLE syntax is: ALTER TABLE table_name ADD (column_1 datatype, column_2 datatype, ... column_n datatype); Example Let's look at SQL ALTER TABLE example that adds more than one column. For example: ALTER TABLE supplier ADD (supplier_name char(50), city char(45)); This SQL ALTER TABLE example will add two columns, supplier_name as a char(50) field and city as a char(45) field to the supplier table. RENAME COLUMN IN TABLE Syntax To rename a column in an existing table, the SQL ALTER TABLE syntax is: For Oracle and PostgreSQL: ALTER TABLE table_name CHANGE COLUMN old_name TO new_name; Example: ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0 ALTER TABLE supplier CHANGE COLUMN supplier_name sname VARCHAR(100); RENAME TABLE Syntax To rename a table, the SQL ALTER TABLE syntax is: ALTER TABLE table_name RENAME TO new_table_name; Example Let's look at an example that renames a table called supplier to the new name vendor. ALTER TABLE supplier RENAME TO vendor; The SQL UPDATE Statement The UPDATE statement is used to modify the existing records in a table. UPDATE Syntax UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; UPDATE Table The following SQL statement updates the first customer (CustomerID = 1) with a new contact person and a new city. Example UPDATE Customers SET ContactName = 'Alfred Schmidt', City= 'Frankfurt' WHERE CustomerID = 1; The selection from the "Customers" table will now look like this: Custo merID Customer Name ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0 ContactName Address City Postal Code Country 1 Alfreds Futterkiste Alfred Schmidt Obere Str. 57 Frankfurt 12209 Germany 2 Ana Trujillo Emparedad os y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico 3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico 4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK 5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden UPDATE Multiple Records It is the WHERE clause that determines how many records will be updated. The following SQL statement will update the contactname to "Juan" for all records where country is "Mexico": Example UPDATE Customers SET ContactName='Juan' WHERE Country='Mexico'; The selection from the "Customers" table will now look like this: ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0 Custom erID CustomerNa me ContactName Addre ss City PostalC ode Country 1 Alfreds Futterkiste Alfred Schmidt Obere Str. 57 Frankfurt 12209 Germany 2 Ana Trujillo Emparedados y helados Juan Avda. de la Constitución 2222 México D.F. 05021 Mexico 3 Antonio Moreno Taquería Juan Mataderos 2312 México D.F. 05023 Mexico 4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK 5 Berglunds snabbköp Christina Berglund Berguvsväge n8 Luleå S-958 22 Sweden SQL TRUNCATE TABLE The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table itself. Syntax TRUNCATE TABLE table_name; Example TRUNCATE TABLE Customers The SQL DROP TABLE Statement The DROP TABLE statement is used to drop an existing table in a database. ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0 Syntax DROP TABLE table_name; Note: Be careful before dropping a table. Deleting a table will result in loss of complete information stored in the table! SQL DROP TABLE Example The following SQL statement drops the existing table "Shippers": Example DROP TABLE Shippers; ISUR-ICT-InM-065 Effectivity: Oct. 21, 2018 Revision: 0