MySQL Flashcards

1
Q

What are DDL languages?

A

Data definition languages?

Create

Alter

Drop

Retrieve

Truncate

Comment.

These are used on tables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are DML languages?

A

Data Manipulation languages

Select

Insert

Update

Delete

these are used on the table’s data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Query to see all databases on the database server

A

show databases;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Query to tell the database server which database to work with

A

use database_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to delete a database?

A

drop database database_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to create a table?

A

create table table_name (column1_name datatype (size) null/not null/primary key,column2 column1_name datatype (size) null/not null,…..);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

to get all tables in a database

A

show tables;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to see the details and pattern of a table?

A

desc table_name;

this is short cut for

show columns from table_name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to insert values into a table?

A

insert into table_name values (value1,value2,value3...)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to create a table with primary key?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to insert multiple rows at once into the table?

A

insert into table_name values

(value11,value12,value13….),

(value 21,value22,value23);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

how to select a particular column/s from a table?

A

select column_name from tablename;

mysql> select pnr,train_num from train;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are constraints in Mysql

A

Not null

Unique

Primary key

Foreign key

Default

Check

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to add a column to already created table?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to add acolumn after a specific column in an already created table?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to make the column just inserted to be the first?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How to change the column name of a table?

A

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();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

how to delete a column?

A

mysql> alter table train drop column AC;

alter table table_name drop column column_name;

notice since this drop action is on the table entities we use ‘drop’ not delete .

19
Q

Difference between modify and change in alter table command

A

Modify and change both are used in Alter table command.

‘change’ is used for renaming the column,changing data size etc.

‘Modify’ does everything as’ change’ except renaming the column name.

20
Q

How to delete the complete data of a table?

A

Truncate table table_name;

it drops the table and create a new table. faster way to the data in a table unlike delete.

21
Q

How to get first N records of a table?

A

Select * from table_name limit N;

22
Q

How to get N records after the record number S?

A

select * from table_name limit S,N

S is like offset,if s=10 and N =3 then we get record numbers 11,12,13

23
Q

Does the count of records in mysql strat from 1?

A

No starts from 0.

24
Q

Like operator

A

mysql> select * from train where train_num like ‘%2%’

gives anything which has 2 and starts and ends with anything.

SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;

25
Q

In

not in

A

mysql> select * from train where 3 in (npsgr);

mysql> select * from train where 3 in (train_num,npsgr);(either or)

SELECT column_name(s)/*
FROM table_name
WHERE column_name IN (value1,value2,…);

26
Q

between and not between

A

is used for range. instead of a>3 & a<6

we can write

select * from table_name where column_name between 3 and 6;

27
Q

wild cards in mysql

A

wild cards are used with ‘like’ operator

%

_

[charlist]

[!charlist]

28
Q

How to output fields in uppercase/lowercase?

A

SELECT UPPER(col_name) FROM table_name;

29
Q

How to get the length of fields?

A

SELECT LENGTH(col_name) FROM table_name;

30
Q

How to output fields by reversing them?

A

SELECT REVERSE(col_name) FROM table_name:

31
Q

How to get a few of the characters from left/right?

A

SELECT LEFT(col_name,# of digits) FROM table_name;

32
Q

How to get a few characters from the middle of field?

A

SELECT MID(col_name,from_val,to_val) FROM table_name;

33
Q

How to get day/year/month part from a date field?

A

SELECT YEAR/(DAY/MONTH)(col_name) FROM table_name:

34
Q

How to MAKE A FIELD STORE BOTH DATE AND TIME?

A

MAKE THE DATATYPE OF THE FIELD ‘DATETIME’

35
Q

HOW TO OUTPUT A DATE FIELD IN OUR OWN FORMAT?

LIKE

A

SELECT DATE_FORMAT(col_name,’%y-%d-%m) FROM table_name (14-12-4)

SELECT DATE_FORMAT(col_name,’%Y-%d-%m) FROM table_name (2014-12-4)

SELECT DATE_FORMAT(col_name,’%Y-%D-%m) FROM table_name (2014-12th-4)

SELECT DATE_FORMAT(col_name,’%Y-%D-%M) FROM table_name (2014-12th-April)

36
Q

What is the default date format for a date field?

A

m/dd/yy

37
Q

Aliases for a column

A

SELECT col_name AS alias_name FROM table_name;

38
Q

ALIAS FOR TABLE NAME

A

SELECT col_name FROM table_name AS table_alias_name;

39
Q

CONCATENATE COLUMNS WITH ALIAS NAMES

A

SELECT col_name CONCAT(col_name1,’, ‘,col_name2,’, ‘,col_name,)AS alias_name FROM table_name;

SELECT CustomerName, CONCAT(Address,’, ‘,City,’, ‘,PostalCode,’, ‘,Country) AS Address
FROM Customers;

40
Q

HOW TO CREATE A TABLE SIMILAR TO AN EXISITN TABLE?

A

CREATE TABLE table2 SIMILAR TO table1;

41
Q

HOW TO INSERT VALUES FROM ONE TABLE INTO AN EXISTING TABLE WITHOUT AFFECTING THE ROWS OF THE EXISTING TABLE?

A

INSERT INTO table2 SELECT * FROM table1,

42
Q

unqiue constraint create

A

CREATE TABLE table_name( col_name1 datatype not null/null, col_name2 datatype,….,UNIQUE(col_name));

43
Q

TO ADD UNQIUE CONSTRAINT ON A COLUMN

A

ALTER TABLE table_name ADD unique (col_name);

44
Q

tO DROP UNQIUE CONSTRAINT ON A COLUMN

A

ALTER TABLE table_name DROP INDEX UNQIUE