Book 16
Book 16
show database Shows all the databases available in MySQL server. >SHOW DATABASE;
create database Creates a new database if it does not exist. >CREATE DATABASE DATABASENAME;
alter database Changes or modifies the characteristics of an existing database. >ALTER DATABASE [DATABASENAME] ALTEROPTION ;
Allow you to use a particular database or change from the current >USE DATABASENAME;
use database
database to another database.
show tables Shows all tables within the current database. >SHOW TABLES;
COLUMN1 DATATYPE,
COLUMN2 DATATYPE,
create table Creates a new table in the current database. COLUMN3 DATATYPE,
....
CONSTRAINTS ....
);
alter table (add column) Adds a new column to an existing table. >ALTER TABLE TABLENAME ADD COLUMNNAME DATATYPE;
alter table (drop column) Deletes a column from an existing table. >ALTER TABLE TABLENAME DROP COLUMN COLUMNNAME;
alter table(drop foreign key) Deletes an existing foreign key in an already existing table. > ALTER TABLE TABLENAME DROP FOREIGN KEY FOREIGNKEY_NAME;
rename table Changes the name of an existing table. >RENAME TABLE OLD_TABLENAME TO NEW_TABLENAME;
drop table Deletes the entire table along with its definition. >DROP TABLE TABLE_NAME;
truncate table Remove all records in a MySQL table. >TRUNCATE TABLE TABLENAME;
describe table Displays all the columns of an existing table. >DESCRIBE TABLE_NAME;
describe table column Displays all the values stored in a particular column. >DESCRIBE TABLE_NAME COLUMN_NAME;
4. MySQL DML(Data Manipulation Language) Commands
select * (multiple tables) Displays all the rows of the cartesian product of the two tables >SELECT * FROM TABLENAME1,TABLENAME2;
select columns Select particular columns from table(s) >SELECT COLUMN1,COLUMN2 FROM TABLENAME;
select with condition Displays rows based on a particular condition > SELECT * FROM TABLENAME WHERE CONDITION
select with condition(NOT) Displays rows based on negation of a particular condition. >SELECT * FROM TABLENAME WHERE NOT CONDITION.
select with group by Displays rows that have same values into summary rows > SELECT .. FROM .. WHERE… GROUP BY COLUMN3;
select distinct Display all unique rows discarding duplicate ones. >SELECT DISTINCT (COLUMN1) FROM TABLENAME;
order by Used to sort results in ascending order or descending order > SELECT … FROM TABLENAME ORDER BY COLUMN1 ASC|DESC;
column alias Changes the output of the name of the column. > SELECT COLUMN1 AS NEWNAME FROM TABLENAME;
like Used to search for a specific pattern. > SELECT COLUMN1 FROM TABLENAME WHERE COLUMN1 LIKE ‘%PATTERN%’;
insert record Adds a new row to an existing table. > INSERT INTO TABLENAME (COLUMN1,COLUMN2…) VALUES (VALUE1,VALUE2…);
delete with where Deletes specific records >DELETE FROM TABLENAME WHERE CONDITION;
between Selects values in a given range >SELECT * FROM TABLENAME WHERE AGE BETWEEN 25 AND 30.
in Used instead of multiple OR operators. > SELECT * FROM TABLENAME WHERE COLUMN2 IN (V1,V2…);
exists Tests for existence of a certain record. Returns a boolean value. > SELECT * FROM TABLE NAME WHERE EXIST (SUB QUERY);
update table Modifies data in existing tables. > UPDATE TABLENAME SET COLUMNNAME=VALUE WHERE CONDITION;
Selects records that have the same values in two same or distinct > SELECT COLUMN(S) FROM TABLENAME1 INNER JOIN TABLENAME2 ON
inner join
tables. TABLENAME1.COLUMNAME=TABLENAME2.COLUMNAME;
Selects all the records from the left table and matching records >SELECT COLUMN(S) FROM TABLENAME1 LEFT JOIN TABLENAME2 ON
left join
from the right table. TABLENAME1.COLUMNAME=TABLENAME2.COLUMNAME;
Selects all the records from the right table and matching records >SELECT COLUMN(S) FROM TABLENAME1 RIGHT JOIN TABLENAME2 ON
right join
from the left table. TABLENAME1.COLUMNAME=TABLENAME2.COLUMNAME;
cross join Selects rows from cartesian product of both the tables. >SELECT COLUMN(S) FROM TABLE1 CROSS JOIN TABLE2;