MySQL Flashcards
What are DDL languages?
Data definition languages?
Create
Alter
Drop
Retrieve
Truncate
Comment.
These are used on tables
What are DML languages?
Data Manipulation languages
Select
Insert
Update
Delete
these are used on the table’s data
Query to see all databases on the database server
show databases;
Query to tell the database server which database to work with
use database_name;
How to delete a database?
drop database database_name;
How to create a table?
create table table_name (column1_name datatype (size) null/not null/primary key,column2 column1_name datatype (size) null/not null,…..);
to get all tables in a database
show tables;
How to see the details and pattern of a table?
desc table_name;
this is short cut for
show columns from table_name;
How to insert values into a table?
insert into table_name values (value1,value2,value3...)
How to create a table with primary key?
If the table has more than one primary key then
.….. primary key(column name1,column name2);
mysql> create table train (pnr integer not null, train_num integer not null, nps
gr integer default 1, primary key (pnr,train_num));
mysql> create table train (pnr integer not null primary key, train_num integer not null primary key, npsgr integer default 1);
How to insert multiple rows at once into the table?
insert into table_name values
(value11,value12,value13….),
(value 21,value22,value23);
how to select a particular column/s from a table?
select column_name from tablename;
mysql> select pnr,train_num from train;
What are constraints in Mysql
Not null
Unique
Primary key
Foreign key
Default
Check
How to add a column to already created table?
Alter table table_name add column_name datatype constraints;
mysql> alter table train add (air_conditioning char default 1);
Query OK, 0 rows affected (0.77 sec)
How to add acolumn after a specific column in an already created table?
mysql> alter table train add express char(10) after npsgr;
mysql> select * from train;
+—–+———–+——-+———+——————+
| pnr | train_num | npsgr | express | air_conditioning |
+—–+———–+——-+———+——————+
| 10 | 123 | 2 | NULL | 1 |
+—–+———–+——-+———+——————+
1 row in set (0.00 sec)
How to make the column just inserted to be the first?
alter table train add doj date first;
mysql> alter table train add doj date first;
mysql> desc train;
+——————+———-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+——————+———-+——+—–+———+——-+
| doj | date | YES | | NULL | |
| pnr | int(11) | NO | PRI | NULL | |
| train_num | int(11) | NO | PRI | NULL | |
| npsgr | int(11) | YES | | 1 | |
| express | char(10) | YES | | NULL | |
| air_conditioning | char(1) | YES | | 1 | |
+——————+———-+——+—–+———+——-+
6 rows in set (0.02 sec)
How to change the column name of a table?
mysql> alter table train change air_conditioning AC char(10);
alter table table_name change
<em>old_column_name new _column_name datatype(),</em>
old_column_name new _column_name datatype(),
old_column_name new _column_name datatype();