SQL CRUD Flashcards

1
Q

How to create and use a database?

A

CREATE DATABASE database_name;

USE database_name;

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

How to create a table with 2 text fields?

A

CREATE TABLE table_sample ( name VARCHAR(10), type_sample VARCHAR(6) );

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

What data type sql have? ( 8 types )

A

VARCHAR(10) -> string
CHAR(1) -> character with fixed size
BLOB -> big text

INT -> integer number
DEC -> decimal number

DATE -> date
DATETIME -> date and time
TIME -> Time

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

How to show a table structure?

A

use DESC -> decribe

DESC table_name;

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

How to remove table?

A

DROP TABLE table_name;

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

How to add new value to the table?

A

INSERT INTO my_contacts

(last_name, first_name) VALUES (‘Андерсон’, ‘Джиллиан’);

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

How to create a table and don’t allow null value?

A

CREATE TABLE table_sample ( name VARCHAR(10) NOT NULL );

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

How to create a table with a default value?

A

CREATE TABLE table_sample ( name VARCHAR(10) default ‘aaa’ );

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

How to delete the rows from the table?

A

DELETE FROM Customers WHERE City = ‘Paris’;

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

How to update the row?

A
# with 1 field
UPDATE Customers SET city = 'Berlin' WHERE city = 'London';
# with 2 fields for updation 
UPDATE Customers SET city='London', country='Germany' WHERE city = 'Berlin';
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to update the integer row using already existing values?

A
# price is already existing value 
UPDATE Products SET price = price + 1 WHERE categoryID = 1;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to create a primary key for the table?

A

CREATE TABLE sample (sample_id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (sample_id));
# OR
CREATE TABLE sample (sample_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY);

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

How to add a new column?

A

ALTER TABLE sample ADD COLUMN name VARCHAR(10);

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

How to rename table?

A

ALTER TABLE sample RENAME TO samples;

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

How to rename column and change column type?

A

ALTER TABLE samples CHANGE COLUMN number number1 VARCHAR(10) DEFAULT ‘ddd’;

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

How to update only column type?

A

ALTER TABLE samples MODIFY COLUMN number1 INT ;

17
Q

How to remove column?

A

ALTER TABLE samples DROP COLUMN name1;

18
Q

How to update the column name with case statement?

A
UPDATE Products SET
	ProductName = 
    CASE 
    	WHEN Price = 20 THEN 'Hello 20'
        WHEN Price = 30 THEN 'Hello 30'
        ELSE 'Nothing'
    END;
19
Q

How to insert values from another table to your table?

A

INSERT INTO my_table (name) SELECT name FROM another_table;

20
Q

How to create table with preinstalled values from another table?

A

CREATE TABLE profession (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(10)) AS SELECT name FROM samples;

21
Q

How to save request and reuse it later?

A
# Create view
CREATE VIEW my_sample_view AS SELECT * FROM Customers;
# then you can use this request
SELECT * FROM my_sampe_view;
22
Q

How to drop view?

A

DROP VIEW my_sample_view;