Skip to content

Instantly share code, notes, and snippets.

@SoumadeepChoudhury
Last active December 16, 2022 17:33
Show Gist options
  • Save SoumadeepChoudhury/0265185bd32f24b15e7d012e9ad950a6 to your computer and use it in GitHub Desktop.
Save SoumadeepChoudhury/0265185bd32f24b15e7d012e9ad950a6 to your computer and use it in GitHub Desktop.
MySQL CodeBase.

MySQL Commands

Link



To show Databases:

show databases;



To create new Database:

create database <name>;



To create new Database if old database do not exist:

create database if not exists <name>;



To enter into specific database:

use <dbname>;



To delete a database:

drop database <name>;



To create table:

create table <table_name> ( <field> <datatype(<size>)>,<field> <datatype(<size>)>,<field> <datatype(<size>)>......);



  • DATATYPES in MYSQL : :

    • varchar(<size>)
    • date
    • double(<total no. of digits>,<digits after decimal>)



To Display structure of a table:

desc <table_name>;

OR

describe <table_name>;



Insert into Table:

insert into <table> (<field>,<field>....) values(<value>,<value>....);

NOTE: values in ''/"" in case of varchar and char and date



Show values of table:

select * from <table>;



Display details for specific condition (Query) :

select * from <table> where <field>=<value>;



Insert into specific field :

insert into <table> (<field>,<field>...) values(<value>,<value>...);



Insert all values together:

insert into <table> values(<value>,<value>.....);

NOTE: Values in above command should be in sequence as in field names....



Create Primary Key:

create table <table_name> ( <field> <datatype(<size>)> primary key,<field> <datatype(<size>)>,<field> <datatype(<size>)>......);



Declaring Foreign Key:

create table <table_name> ( <field> <datatype(<size>)>,FOREIGN KEY (<field>) REFERENCES <other_table_name>(<field>) ON DELETE CASCADE ON UPDATE CASCADE,<other_fields...>......);



Update Value in a table:

update <table_name> set <field>=<value> where <primary_Key>=<value>;



Delete record in a table:

delete from <table_name> where <primary_key>=<value>;



Virtually increase field value and display...:

select <field>,<field>...,<field>+<increament> as <New Alias> from <table_name>;



Virtually decrease field value and display...:

select <field>,<field>...,<field>-<decreament> as <New Alias> from <table_name>;



Table structure (size) modification:

alter table <table_name> modify <field> <datatype>(<size>);



Table Structure (datatype) modification..:

alter table <table_name> modify <field> <new_datatype>;



Add primary key in existing table:

alter table <table_name> add primary key(<field>);



Eliminating Redundant data:

select DISTINCT <field_name> from <table_name>;



Selecting from all the rows- ALL keyword:

select ALL <field_name> from <table_name>;

OR

select <field_Name> from <table_name>;



Perform simple calculations:

select 1+6;

OR

select 4 * 3 from dual;
@imanmay2
Copy link

GOOD..
I HOPE MORE THINGS WILL BE ADDED SOON....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment