Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
34 views4 pages

MySQL Syntaxes - 241023 - 235003

Uploaded by

Manas Arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

MySQL Syntaxes - 241023 - 235003

Uploaded by

Manas Arora
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

MySQL Syntaxes

[black is for syntaxes, italicised black is for code specific things that will change according to
code (eg: database_name, column_name etc), green is for explanations, red is for code
examples]

Table/Database Related Operations:


1. create database database_name;
- creates a database that can hold multiple tables
- create database employee;

2. use database_name;
- sets the created database as the default database being used
- use employee;

3. create table table_name (column_name datatype primary key, column_name


datatype,...);
- creates a table with the listed columns and data types, primary key acts as an identifier
(like our registration numbers)
- create table employee_data(empID int primary key, firstname varchar(50), lastname
varchar(50), department varchar(50), designation varchar(50));

4. desc table_name;
- gives details of the table including information on datatypes

Keyword Datatype Syntax

int Integer (numbers only) col_name int

char Character (single letter, number, symbol, col_name char


whitespace etc.)

varchar String (can contain multiple letters, col_name


numbers, spaces etc.) varchar(characterlimit)
Eg: firstname varchar(50)

- desc employee_data;

5. insert into table_name values(value,value,value,...);


- add data entries to the table
- insert into employee_data values(1001, “AAAA”, ”XYZ”, “R&D”, “Junior
Developer”);

6. delete from table_name where primary_key_column name = value;


- delete a row from a table
- delete from employee_data where empID=1001;

7. show databases;
- shows all databases
- show databases;

8. update table_name set col_name=value where primary_key_col_name=value;


- updates data (alteration, addition or modification)
- update employee_data set designation=”Senior Developer” where empID=1002;

9. select * from table_name;


- show table (* means all entries)
- select * from employee_data;

10. alter table table_name add new_col_name datatype;


- add a column to the table (idt she taught this yet, googled it.)
- alter table employee_data add basic_salary varchar(50);

11. alter table table_name drop col_to_remove_name;


- removes a column from the table (same as above, googled it)
- alter table employee_data drop basic_salary;

12. create table table_name (column_name datatype primary key, column_name datatype
default value);
- set a default value to the column (will be ignored if data is given for the column)
- create table students (regno int primary key, class varchar(10), dept varchar(10) default
‘BBA’);
- note that when this is used, the insert into command changes syntax a bit
- insert into students (regno, class) values (2320721, “3BBAG”);
- to override the default just insert data as you would normally
- insert into students values(234287, “3BCOMSF”,”COMM”);

13. create table table_name (column_name datatype primary key auto_increment,


column_name datatype,...);
- set a +1 increment to the column, use it for primary keys like employee id, registration
no., serial no., etc (will be ignored if data is given for the column)
- create table students (regno int primary key auto_increment, class varchar(10), dept
varchar(10));
- note that when this is used, the insert into command changes syntax a bit, the first entry
has to be made normally for it to have a base value
- insert into students values (2320701, “3BBAG”, “BBA”);
insert into students (class, dept) values (“3BBAG”,”BBA);
- it’ll take the regno as 702 (701 base +1)

14. Select old_col_name AS ‘new_col_name’ from table_name;


- temporarily rename the column, AS stands for alias
- select dept AS department from students;

String Operations:
1. select concat(“value”,”value”);
- join two string values (to add space in between do: select concat(“value”,” “,”value”))
- select concat(“hello”,” “,”world);

2. select primary_key_col_name, concat(col1_name,col2_name) AS extra_col_name from


table_name;
- concatenate values from a table (eg first name and last name, in a temporary full name
column, does not change the main table)
- the AS fullname part is just to give the temporary col a name, not mandated can skip
- select empID, concat(firstname,lastname) AS fullname from employee_data;

3. select replace(col_name,existing_value,new_value) AS new_col_name from table_name;


- replace values partly or fully, returns existing 101, 102, 103 empIDs as 1001, 1002,
1003 in a temporary col, doesn’t change main table
- the AS employee ID part is just to give the temporary col a name, not mandated can
skip
- select replace(empID,10,100) AS employeeID from employee_data;

4. select reverse(“value”);
- returns value reversed (eg: hello as olleh)
- select reverse(“hello”);

5. select upper(“value”);
- returns value in uppercase (hello as HELLO)
- select upper(“hello”);
6. select lower(“value”);
- returns value in uppercase (HELLO as hello)
- select lower(“hello”);

7. select upper(col_name) from table_name;


- returns value from table in uppercase (hello as HELLO), same for lower
- select upper(dept) from students;
- or
- select regno, upper(dept) from students;
(this one shows values of regno (initially entered case itself) and dept values in
uppercase)
- select dept, lower(dept) from students;
(this one shows og dept values in one col and dept values in lowercase in another)

8. select * from table_name order by column_name desc;


- arranges data according to descending (Z-A) order of selected column
- select * from students order by regno desc;

9. select * from table_name order by column_name asc;


- arranges data according to ascending (A-Z) order of selected column (if desc or asc isn’t
mentioned, it takes asc by default)
- select * from students order by regno asc;

You might also like