Inserting, updating, and deleting data Flashcards

1
Q

In column attributes what is the difference between VARCHAR and CHAR?

A

VARCHAR - variable chars * saves space

CHAR - will explicitly allocate specific number of characters like char(50).

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

PK describes what in column attributes?

A

The primary key uniquely identification on the table.

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

NN represents what in column attributes?

A

Determines if a column accepts null values, NOT NULL

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

Based on the column attribute pic what two columns on this customers table is optional?

A

the birth_date and phone columns are not marked NOT NULL, so they are optional values.

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

True or False

The primary key column is usually set to AI which is what?

A

Auto_Increment

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

When you don’t specify a value, MYSQL will provide a Default/Expression value of?

A

NULL

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

Write a query to insert the following information into the customers table.

Kelvin Waters

DOB: 08/02/1967

3401 Sunrise Villas Ct S, Tampa FL

Points 0 (default)

A

INSERT INTO customers (
first_name,
last_name,
birth_date,
address,
city,
state
)
VALUES (
‘Kelvin’,
‘Waters’,
‘1967-08-02’,
‘3401 Sunrise Villas CT S’,
‘Tampa’,
‘FL’
)

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

Write a query to insert multiple shippers in the shippers table

shipper1, shipper2, shipper3

A

INSERT INTO shippers (name)
VALUES
(‘shipper1’),
(‘shipper2’),
(‘shipper3’)

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

Exercise:

– Insert three rows into the products table

A

INSERT INTO products (
name,
quantity_in_stock,
unit_price
)
VALUES
(‘astroglide’, 10, 9.99),
(‘eatible panties’, 3, 14.89),
(‘dildo’, 16, 24.95)

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

What is this an example of?

INSERT INTO orders (customer_id, order_date, status)
VALUES (1, ‘2019-01-02’, 1);

INSERT INTO order_items
VALUES
(LAST_INSERT_ID(), 1, 1, 2.95),
(LAST_INSERT_ID(), 2, 1, 3.95)

A

Inserting hierarchical data on multiple tables (orders table has a parent child relationship with the order_items table)

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

How to copy one table data to another? (use the order table)

A

CREATE TABLE orders_archived AS
SELECT * FROM orders

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

When copying a table what two important attributes will NOT be copied?

A

The primary-key (pk) and auto increment (AI) status

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

What part of this query would be known as the subquery?

CREATE TABLE orders_archived AS
SELECT * FROM orders

A

SELECT * FROM orders

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

Truncating a table in mysql will do what exactly?

A

deletes all data in the table

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

This code is performing what in the orders_archived table that was previously truncated?

INSERT INTO orders_archived
SELECT *
FROM orders
WHERE order_date < ‘2019-01-01’

A

inserting filtered data via the WHERE statement into the orders_archived table.

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

What is the primary query of this query?

INSERT INTO orders_archived
SELECT *
FROM orders
WHERE order_date < ‘2019-01-01’

A

INSERT INTO orders_archived

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

What database is this query accessing?

USE sql_invoicing;

SELECT *
FROM invoices i
JOIN clients c
USING(client_id)

A

sql_invoicing

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

What column are these table being joined on?

USE sql_invoicing;

SELECT *
FROM invoices i
JOIN clients c
USING(client_id)

A

client_id on both tables

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

What two alias are being used in this query?

USE sql_invoicing;

SELECT *
FROM invoices i
JOIN clients c
USING(client_id)

A
  • *i** on invoices
  • *c** on clients
20
Q

Describe whats being performed in the bold of this query?

USE sql_invoicing;

SELECT
i.invoice_id,
i.number,
c.name AS client,
i.invoice_total,
i.payment_date,
i.due_date
FROM invoices i
JOIN clients c
USING(client_id)

A

The name column on the clients table is being changed to client

21
Q

The preceding i and c’s on this query indicates what?

USE sql_invoicing;

SELECT
i.invoice_id,
i.number,
c.name AS client,
i.invoice_total,
i.payment_date,
i.due_date
FROM invoices i
JOIN clients c
USING(client_id)

A

which table the column resides on

22
Q

What’s being filtered in this query?

USE sql_invoicing;

SELECT
i.invoice_id,
i.number,
c.name AS client,
i.invoice_total,
i.payment_date,
i.due_date
FROM invoices i
JOIN clients c
USING(client_id)
WHERE payment_date IS NOT NULL

A

return only records where there’s been a payment made

payment_date is NOT null

23
Q

After executing all this code what creates another table of this quiry?

USE sql_invoicing;

CREATE TABLE invoices_archived AS
SELECT
i.invoice_id,
i.number,
c.name AS client,
i.invoice_total,
i.payment_date,
i.due_date
FROM invoices i
JOIN clients c
USING(client_id)
WHERE payment_date IS NOT NULL

A

CREATE TABLE invoices_archived AS

24
Q

True or False

In mysql you have to refresh the panel to view all changes made to table and databases

25
True or False In mysql you can create as many tables of indentical names as you want?
False tables and databases names must be unique
26
What table is being quired here? ## Footnote SELECT \* FROM sql\_invoicing.invoices;
invoices
27
What database is being quired here? ## Footnote SELECT \* FROM sql\_invoicing.invoices;
sql\_invoicing
28
What table is being updated in this query? ## Footnote UPDATE invoices SET payment\_total = 10, payment\_date = '2019-03-01' WHERE invoice\_id = 1
the invoices table
29
What two columns are being updated/changed? ## Footnote UPDATE invoices SET payment\_total = 10, payment\_date = '2019-03-01' WHERE invoice\_id = 1
the payment\_total, payment\_date columns
30
What specific row is being affected by this query? ## Footnote UPDATE invoices SET payment\_total = 10, payment\_date = '2019-03-01' WHERE invoice\_id = 1
the invoice\_id 1 on the invoices table
31
Is this a valid query? ## Footnote UPDATE invoices SET payment\_total = **DEFAULT**, payment\_date = **NULL** WHERE invoice\_id = 1
only if the table itself has a default value set for the payment column and the payment\_date accepts NULL in the table attributes
32
In this query how much is being paid? ## Footnote UPDATE invoices SET payment\_total = invoice\_total \* 0.5, payment\_date = due\_date WHERE invoice\_id = 3
50% of the invoice\_total and the payment\_date is set to the due\_date
33
True or False In mysql one must disable Safe Updates(rejects UPDATEs and DELETESs with no restrictions) before executing a query which updates multiple rows?
True
34
True or False This query is updating multiple row via the WHERE statement USE sql\_invoicing; UPDATE invoices SET payment\_total = invoice\_total \* 0.5, payment\_date = due\_date **WHERE** invoice\_id = 3
True
35
What is this query performing? ## Footnote UPDATE customers SET points = points + 50 WHERE birth\_date \< '1990-01-01'
making changes on the customers table, adding 50 to the points value in the points column on all rows where the birth\_date is earlier than 01-01-1990
36
True or False A SELECT statement within another sql statement is considered a subquery.
True
37
The bold portion of this query is showing what? ## Footnote UPDATE invoices SET payment\_total = invoice\_total \* 0.5, payment\_date = due\_date WHERE client\_id = **(SELECT client\_id FROM clients WHERE name = 'Myworks')**
Updating multiple rows based on the name col 'Myworks'
38
How many rows are being affected by this query? ## Footnote UPDATE invoices SET payment\_total = invoice\_total \* 0.5, payment\_date = due\_date WHERE client\_id **IN** (SELECT client\_id FROM clients WHERE state **IN** ('NY', 'CA'))
all rows that have state of NY or CA
39
True or False In mysql is always a good idea to perform a micro query (shift+ctrl+enter) using a SELECT statement before updating multiple rows!
True this is a good way to verify the data (rows) being affected.
40
Cite two ways to set the active database for querying in mysql.
1. Use the *USE database*; statement 2. Double-click the database in the mysql workbench schemas left pane.
41
True or False It's totally possible to update a table in mysql based on data from a different table withouy an explict JOIN statement?
True ## Footnote UPDATE orders SET comments = 'Gold customer' WHERE customer\_id IN (SELECT customer\_id FROM customers WHERE points \> 3000)
42
What is the bold part of this query? ## Footnote UPDATE orders SET comments = 'Gold customer' WHERE customer\_id IN **(SELECT customer\_id FROM customers WHERE points \> 3000)**
subquery
43
Why is the IN statement required in this query? ## Footnote UPDATE orders SET comments = 'Gold customer' WHERE customer\_id **IN** (SELECT customer\_id FROM customers WHERE points \> 3000)
Because it affects multiple rows
44
What two tables are involved in this query? ## Footnote UPDATE orders SET comments = 'Gold customer' WHERE customer\_id IN (SELECT customer\_id FROM customers WHERE points \> 3000)
the orders table is being updated with based on data from the customers table points greater than 3000 on the customer table will dictate who gets commented as a Gold customer on the orders table
45
What is the significance of using the FROM statement in this query? ## Footnote DELETE FROM invoices
It will delete the invoices table entirely!!!
46
What is being deleted in this query? ## Footnote DELETE FROM invoices WHERE client\_id = ( SELECT \* FROM clients WHERE name = 'Myworks')
Myworks invoices data