34
loading...
This website collects cookies to deliver better user experience
MySQL is a free and fast Relational Database Management System. It is ideal for both small and large applications.
In MySQL, SQL is used to insert, search, update, and delete database records. All MySQL commands will be in capital letters.
In MySQL, most common SQL commands are:
To select the full table
SELECT * FROM tableName;
To select one or many specific columns(with all column data) from the data table.
SELECT column, column2, ... FROM tableName;
To select one or many specific columns(without duplicate column data) from the data table.
SELECT DISTINCT column, column2, ... FROM tableName;
The UPDATE statement used to modify the existing date of a table.
If you don’t use WHERE with condition then when all dates in the column will change.
UPDATE tableName
SET column = value, column2= value2, ...
WHERE conditions;
The DELETE statement is used to delete records from a data table.
If you don’t use WHERE then all records will delete.
DELETE FROM tableName
WHERE conditions;
You can write an INSERT INTO statement in two ways.Firstly, you can specify the column name and then specify their values in orderly like this
INSERT INTO tableName (column, column2, column3, ...)
VALUES (value, value2, value3, ...);
Secondly, you don’t need to specify the column name but you can specify only the values for each column orderly of your data table.
INSERT INTO tableName
VALUES (value, value2, value3, ...);
With the CREATE DATABASE statement we can create a new database.
CREATE DATABASE tableName;
To add a new column and must be specify the dataType.
ALTER TABLE tableName
ADD columnName dataType;
To delete an existing column from a data table
ALTER TABLE tableName
DROP COLUMN columnName;
To change an existing column’s dataType from a data table.
ALTER TABLE tableName
MODIFY COLUMN columnName dataType;
CREATE TABLE statement used to create a new table in a database.
==syntex==
CREATE TABLE tableName (
columnName1 dataType,
columnName2 dataType,
columnName3 dataType,
... ...
);
==example==
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Create table using another existing table
With CREATE TABLE you can copy one existing table and create a new one with existing table columns and also can apply conditions to get existing table columns into your new table.
CREATE TABLE newTableName AS
SELECT column1, column2, ...
FROM existingTableName
WHERE conditions;
DROP DATABASE statement used to delete an existing database.
Be careful to drop because once delete a table all of data in this table will lost forever
DROP DATABASE tableName;
To create indexes in a table you can use the CREATE INDEX statement. Indexes allow us to retrieve our data from a table quickly. Usually users can’t see the indexes. Indexes are just used to speed up our queries.
Create an index in a table. Duplicate values are allowed.
CREATE INDEX indexName
ON tableName (column1, column2, ...);
Create an unique index in a table. Duplicate values are not allowed here.
CREATE UNIQUE INDEX indexName
ON tableName (column1, column2, ...);
Use DROP INDEX statement to delete an index
ALTER TABLE tableName
DROP INDEX indexName;