Unit 4 - SQL Flashcards

(17 cards)

1
Q

What is DDL?

A

Data definition language - concerned w creating and editing tables, not w/ contents of data (CREATE)

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

What is DML, DQL?

A

DML - data manipulation language - insert, delete, update

DQL - data quert language - select

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

How would you create a student table containing a number, name, username, and email? Ensure proper constraints

A

CREATE TABLE students {
studentNum numeric primary key,
name text check(length(name) > 0),
username text unique,
email text not null);

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

How to autogenerate an ID? What’s the warning?

A

CREATE TABLE student (SID integer primary key autoincrement, …);

0 since ID autoincrements, you need to manually look for duplicates

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

Convert this to DDL:
Employee(EID, Name, Address)
Project(PID, Title, Budget, ManagerID) FK ManagerID REF Employee(EID)
- WorksOn(EID, PID, Start, Task(

A

CREATE TABLE employee(
eID integer primary key,
name text not null,
address text);

CREATE TABLE project(
pID integer primary key autoincrement,
title text not null,
budget numeric,
managerID integer references employee(eID));

CREATE TABLE worksOn(
eID integer references employee(eID),
pID integer references project(pID),
start date not null,
task text,
primary key(eID, pID) );

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

How to delete a table named city?

A

drop table city;

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

In table city, how to insert new city?

A

insert into city
values (“Winnipeg”, 1234, 49.1234, 312.1234, “Bowman”);

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

If we don’t know the mayor, what to put in the table to auto-set it?

A

mayor text default “Mayor: todo”

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

How to show entire city table?

A

select * from city;

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

How to query for populations in a city over 10000+?

A

select * from city
where population >10000;
(inclusive)

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

How to delete cities w/ population >=10000+ and latitude is more than 49?

A

delete from city
where population >10000
and latitude > 49;

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

Describe the different possible actions for deletes

A

no action/restrict - default and normal - do nothing
- wont let you delete if it has references

cascade - if X is delete, deleted every record with a FK pointing to X

set null/set default - autoset it to something else or null

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

How to set a domain in a key?

A

studentID integer primary key check (studentID >0)

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

How do you add a columnn for highest table in a city table, then drop it?

A

alter table city
add column highestBuilding numeric,
alter table city drop highestBuilding;

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

How could you rename all students to “bob”

A

update students set name=”bob”

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

How could you rename student with ID 67 to cool

A

update students
set name=”cool”
where studentID= 67;

16
Q

How could we ensure consistency for insertion, updates, and delete?

A

insertion + update: we shouldn’t be able to add an entity which references an entity that DNE

delete: if we delete a referenced entity, must account for entities that reference them