Mysql: The SQL Create Database Statement
Mysql: The SQL Create Database Statement
Mysql: The SQL Create Database Statement
MySQL is the most popular Open Source Relatonal SQL Database Management System. MySQL is one of
the best RDBMS being used for developing various web-based sofware applicatons. MySQL is
developed, marketed and supported by MySQL AB, a company founded in 1995 in Sweden.
MySQL is released under an open-source license. So you have nothing to pay to use it.
MySQL uses a standard form of the well-known SQL data language.
MySQL works on many operatng systems and with many languages including PHP, PERL, C, C++,
JAVA, etc.
MySQL works very quickly and works well even with large data sets.
MySQL is very friendly to PHP, the most appreciated language for web development.
MySQL supports large databases, up to 50 million rows or more in a table. The default fle size
limit for a table is 4GB, but you can increase this (if your operatng system can handle it) to a
theoretcal limit of 8 million terabytes (TB).
MySQL is customizable. The open-source GPL license allows programmers to modify the MySQL
sofware to ft their own specifc environments.
Syntax
EX:
Note:
1) Once a database is created, you can check it in the list of databases with the SQL command:
SHOW DATABASES;
2) SQL keywords are NOT case sensitve: show is the same as SHOW
3) Some database systems require a semicolon at the end of each SQL statement. Semicolon is the
standard way to separate each SQL statement in database systems that allow more than one
SQL statement to be executed in the same call to the server.
Syntax
EX:
Note: Be careful before dropping a database. Deletng a database will result in loss of complete
informaton stored in the database!
use databasename;
Syntax
Example:
The frst way specifes both the column names and the values to be inserted:
If you are adding values for all the columns of the table, you do not need to specify the column names in
the SQL query. However, make sure the order of the values is in the same order as the columns in the
table. The INSERT INTO syntax would be as follows:
EX:
The WHERE clause is used to extract only those records that fulfll a specifed conditon . It is also used in
UPDATE, DELETE statement, etc.!
WHERE Syntax
The following SQL statement selects all the customers from the country "INDIA", in the "Customers"
table:
Example
SQL requires single quotes around text values (most database systems will also allow double quotes).
The MySQL AND conditon is used with SELECT, INSERT, UPDATE or DELETE statements to test two or
more conditons in an individual query.
Ex:
SELECT *
FROM student
WHERE stu_frstname = 'Ajeet'
AND roll > 3;
MySQL OR Conditon
The MySQL OR conditon specifes that if you take two or more conditons then one of the conditons
must be fulflled to get the records as result.
Ex:
SELECT *
FROM student
WHERE stu_frstname = 'Ajeet'
OR roll > 5;
Ex:
SELECT *
FROM students
WHERE (course_name = 'Java' AND student_name = 'Aryan')
OR (student_id < 2);
In MySQL, LIKE conditon is used to perform patern matching to fnd the correct result. It is used in
SELECT, INSERT, UPDATE and DELETE statement with the combinaton of WHERE clause.
Ex:
SELECT student_name
FROM student
WHERE address LIKE 'Jaip%';
Ex:
SELECT student_name
FROM student
WHERE address NOT LIKE 'Jaip%';
MySQL IN Conditon
The MySQL IN conditon is used to reduce the use of multple OR conditons in a SELECT, INSERT,
UPDATE and DELETE statement.
EX:
SELECT *
FROM student
WHERE student_name IN ('Anil', 'Rahim', 'Deepika');
The MySQL NOT conditon is opposite of MySQL IN conditon. It is used to negate a conditon in a
SELECT, INSERT, UPDATE or DELETE statement.
EX:
SELECT *
FROM student
WHERE student_name NOT IN ('Anil', 'Rahim', 'Deepika');
EX:
SELECT *
FROM student
WHERE student_name IS NOT NULL;
EX:
SELECT *
FROM student
WHERE student_name NOT LIKE 'A%';
Ex:
SELECT *
FROM student
WHERE student_id NOT BETWEEN 3 AND 5;
MySQL IS NULL conditon is used to check if there is a NULL value in the expression. It is used with
SELECT, INSERT, UPDATE and DELETE statement.
Ex:
SELECT *
FROM student
WHERE student_name IS NULL;
The SQL UPDATE Statement
UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE conditon;
Note: Be careful when updatng records in a table! Notce the WHERE clause in the UPDATE statement.
The WHERE clause specifes which record(s) that should be updated. If you omit the WHERE clause, all
records in the table will be updated!
Example
UPDATE Student
SET name = 'Aman', City= 'Jaipur'
WHERE roll = 1;
DELETE Syntax
Note: Be careful when deletng records in a table! Notce the WHERE clause in the DELETE statement.
The WHERE clause specifes which record(s) that should be deleted. If you omit the WHERE clause, all
records in the table will be deleted!
Syntax
Note: Be careful before dropping a table. Deletng a table will result in loss of complete informaton
stored in the table!
Ex:
The ALTER TABLE statement is used to add, delete, or modify columns in an existng table.
EX:
To delete a column in a table, use the following syntax (notce that some database systems don't allow
dram
eletng a column):
EX:
To change the data type of a column in a table, use the following syntax:
The SQL MIN() and MAX(), COUNT(), AVG() and SUM() Functons
The MIN() functon returns the smallest value of the selected column.
The MAX() functon returns the largest value of the selected column.
The COUNT() functon returns the number of rows that matches a specifed criteria.
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE conditon;
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE conditon;
COUNT() Syntax
SELECT COUNT(column_name)
FROM table_name
WHERE conditon;
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE conditon;
SUM() Syntax
SELECT SUM(column_name)
FROM table_name
WHERE conditon;
Some of The Most Important SQL Commands
MySQL DISTINCT clause is used to remove duplicate records from the table and fetch only the unique
records. The DISTINCT clause is only used with the SELECT statement.
Syntax:
Example:
SELECT DISTINCT officer_name, address FROM officers;
The MYSQL ORDER BY Clause is used to sort the records in ascending or descending order.
Syntax:
SELECT expressions
FROM tables
[WHERE conditons]
ORDER BY expression [ ASC | DESC ];
Example1:
SELECT *
FROM officers
WHERE address = 'Lucknow'
ORDER BY officer_name;
OR
SELECT *
FROM officers
WHERE address = 'Lucknow'
ORDER BY officer_name ASC;
Note: If you use MySQL ORDER BY clause without specifying the ASC and DESC modifer then by default
you will get the result in ascending order.
Example2:
SELECT *
FROM officers
WHERE address = 'Lucknow'
ORDER BY officer_name DESC;
The MYSQL GROUP BY Clause is used to collect data from multple records and group the result by one
or more column. It is generally used in a SELECT statement.
Example1:
SELECT address, COUNT(*)
FROM officers
GROUP BY address;
Example2:
SELECT emp_name, SUM(working_hours) AS "Total working hours"
FROM employees
GROUP BY emp_name;
MySQL HAVING Clause is used with GROUP BY clause. It always returns the rows where conditon is
TRUE.
Example:
SELECT emp_name, SUM(working_hours) AS "Total working hours"
FROM employees
GROUP BY emp_name
HAVING SUM(working_hours) > 5;
The MySQL frst functon is used to return the frst value of the selected column. Here, we use limit
clause to select frst record or more.
Syntax:
SELECT column_name
FROM table_name
LIMIT 1;
SELECT officer_name
FROM officers
LIMIT 1;
SELECT officer_name
FROM officers
LIMIT 2
MySQL last functon
MySQL last functon is used to return the last value of the selected column.
Syntax:
SELECT column_name
FROM table_name
ORDER BY column_name DESC
LIMIT 1;
SELECT officer_name
FROM officers
ORDER BY officer_id DESC
LIMIT 1;
SQL commands are divided into four subgroups, DDL, DML, DCL, and TCL.
1) DDL is short name of Data Defniton Language, which deals with database schemas and
descriptons, of how the data should reside in the database.
2) DML is short name of Data Manipulaton Language which deals with data manipulaton and
includes most common SQL statements such SELECT, INSERT, UPDATE, DELETE, etc., and it is
used to store, modify, retrieve, delete and update data in a database.
3) DCL is short name of Data Control Language which includes commands such as GRANT and
mostly concerned with rights, permissions and other controls of the database system.