Databases (Flat File & Relational) and SQL

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/39

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

40 Terms

1
New cards

What is a database?

A structured collection of data that stores data in tables.

2
New cards

What is a table?

A collection of records with a similar structure.

3
New cards

What are fields? What do they store?

Fields are the columns and are used to store a specific piece of information

4
New cards

What are records? What do they store?

Records are the rows of the table, a record uses the fields to store details about a specific item.

5
New cards

What does each field in a record have?

A specific data type assigned to it.

6
New cards

What does each table in a database have? What is this?

A primary key. It is a field that can uniquely identify any record in the table. There can only be one primary key field per database table.

7
New cards

What are some key benefits of electronic databases:

  • Easier to add, delete, modify and update data

  • Data can be backed up & copied easier

  • Multiple users, from multiple locations can access the same database at the same time

8
New cards

What are the 2 main types of database:

  1. Flat file database

  2. Relational database

9
New cards

Flat file database

A database that stores all the data in one table.

10
New cards

Flat file pros & cons:

  • 👍Simple to set up

  • 👍Easy to understand

  • 👎Causes data redundancy - inefficient storage

  • 👎Uses more memory or storage than it needs to

  • 👎Takes longer to search for data

  • 👎Harder to maintain

<ul><li><p><span data-name="+1" data-type="emoji">👍</span>Simple to set up</p></li><li><p><span data-name="+1" data-type="emoji">👍</span>Easy to understand</p></li><li><p><span data-name="-1" data-type="emoji">👎</span>Causes data redundancy - inefficient storage </p></li><li><p><span data-name="-1" data-type="emoji">👎</span>Uses more memory or storage than it needs to</p></li><li><p><span data-name="-1" data-type="emoji">👎</span>Takes longer to search for data</p></li><li><p><span data-name="-1" data-type="emoji">👎</span>Harder to maintain</p></li></ul>
11
New cards

Relational databases

They are made up of multiple tables that are often linked together using primary keys and foreign keys.

<p>They are made up of <mark data-color="purple">multiple tables</mark> that are often linked together using primary keys and foreign keys.</p>
12
New cards

Foreign key

A field that references the primary key of another table - creating a link between the two tables.

<p>A field that <mark data-color="purple">references the primary key of another tabl</mark>e - creating a link between the two tables.</p>
13
New cards

Relational databases advantages:

  • Data is only stored in one place and then referenced - inputting, updating or deleting data only has to be done in one place - preventing inconsistencies

  • Only storing the same piece of data once will also save storage space

  • Are more secure - some tables can be made confidential so only some users can see certain data

14
New cards

Data redundancy definition:

Storing the same data more than once in a database in multiple locations. It will lead to data inconsistency.

15
New cards

Problems with data redundancy:

  • Wasted storage space

  • More difficult database update

  • Will lead to data inconsistency

  • Retrieval of data is slow & inefficient

16
New cards

Data inconsistency definition:

When the same data exists in different formats in multiple tables. Can cause unreliable & meaningless info.

17
New cards

What is SQL?

Structure Query Language is a programming language used to interact with a database.

18
New cards

What does the use of SQL allow a user to do?

  • Select data (flat file & relational)

  • Order data

  • Insert data

  • Update data

  • Delete records

19
New cards

What does the ‘SELECTcommand do?

Retrieves data from a database table.

20
New cards

What does the ‘FROMcommand do?

Specifies the tables to retrieve data from.

21
New cards

What does the ‘WHEREcommand do?

Filters the data that will be displayed based on a specified condition

22
New cards

What does the ‘ANDcommand do?

Combines multiple conditions in a WHERE clause.

23
New cards

What does the ‘ORcommand do?

Retrieves data when at least on of the conditions is true.

24
New cards

What is a wildcard command and what does the command do?

* is a wildcard command used to select all fields in a table.

25
New cards

What does the ‘ORDER BYcommand do?

It is used to organise/sort the data into ascending (ASC) or descending (DESC) order when it is retrieved.

26
New cards

What must all SQL code end with?

a semicolon ‘;’.

27
New cards

How to write a SELECT query:

Use the SELECT keyword followed by the names of the fields you want to retrieve and display. Then, use the FROM keyword followed by the name of the table/tables you want to search. You can also use the WHERE keyword to specify the conditions that a record must satisfy to be returned.

28
New cards

Write a query that will select all the fields from the Customers table:

SELECT*FROM Customers

<p>SELECT*FROM Customers</p>
29
New cards
<p>Write a <strong>query </strong>that will <strong>select the ID, name &amp; age</strong> of <strong>customers </strong>who are <strong>older than 25</strong>:</p>

Write a query that will select the ID, name & age of customers who are older than 25:

SELECT ID, name, Age

FROM Customers

WHERE Age > 25;

<p>SELECT ID, name, Age </p><p>FROM Customers</p><p>WHERE Age &gt; 25;</p>
30
New cards

Write a query that will select the name and country of customers who are from the USA:

SELECT Name, Country

FROM Customers

WHERE Country = ‘USA’;

<p>SELECT Name, Country</p><p>FROM Customers</p><p>WHERE Country = ‘USA’;</p>
31
New cards

Write a command that orders the records into descending/ascending order:

ORDER BY field DESC

or ORDER BY field ASC

32
New cards

How to add records to a table?

Use the key words INSERT INTO followed by the name of the table & the fields you want to add the records to. Then the the keyword VALUES followed by data you want to add.

e.g.

INSERT INTO users (name, age)
VALUES ('John Doe',25);
(inserts a new user with the name 'John Doe' and age 25)

33
New cards

How to edit data in a database?

Use UPDATE, SET and WHERE. UPDATE followed by the table you want to update, SET followed by the fields you want to change and the values you want to change them to, then use WHERE to specify the condition that determines which records will be updated.

e.g.

UPDATE users
SET name = 'Bob', age = 56
WHERE ID = 4;
(updates name and age details for user ID = 4)

34
New cards

How to remove data from a table:

Use DELETE, FROM and WHERE. DELETE FROM followed by the table you want to delete a record from, then WHERE followed by the condition that determines which records are deleted.

e.g.

DELETE FROM users
WHERE age < 18;
(deletes all users younger than 18 from the 'users' table)

35
New cards
<p><strong>Write </strong>a <strong>query </strong>that will<strong> insert a new employee </strong>into the <strong>Employees table </strong>with the<strong> ‘Name’, ‘Salary’, ‘Department’ and ‘City’ fields</strong>:</p>

Write a query that will insert a new employee into the Employees table with the ‘Name’, ‘Salary’, ‘Department’ and ‘City’ fields:

INSERT INTO Employees(Name, Salary, Department, City)

VALUES (‘George Rope’, 47250, ‘Sales’, ‘Leeds’)';

<p><strong>INSERT INTO </strong>Employees(Name, Salary, Department, City)</p><p><strong>VALUES </strong>(‘George Rope’, 47250, ‘Sales’, ‘Leeds’)';</p>
36
New cards
<p><strong>Write </strong>a <strong>query </strong>that will<strong> update employee ID 3</strong> to a <strong>salary </strong>of <strong>47500 </strong>and <strong>city </strong>to <strong>London</strong>:</p>

Write a query that will update employee ID 3 to a salary of 47500 and city to London:

UPDATE Employees

SET Salary = 47500, City = ‘London’

WHERE ID = 3';

<p><strong>UPDATE </strong>Employees</p><p><strong>SET </strong>Salary = 47500, City = ‘London’</p><p><strong>WHERE </strong>ID = 3';</p>
37
New cards
<p><strong>Write</strong> a <strong>query </strong>that will <strong>delete all records</strong> from the <strong>Employees table </strong>whose <strong>department </strong>is ‘<strong>Marketing</strong>’</p>

Write a query that will delete all records from the Employees table whose department is ‘Marketing

DELETE FROM Employees

WHERE Department = ‘Marketing’;

<p><strong>DELETE FROM</strong> Employees</p><p><strong>WHERE </strong>Department = ‘Marketing’;</p>
38
New cards

How to retrieve data from a Relational database:

SELECT table1.field, table2.field

FROM table1, table 2

WHERE table1.primarykey = table2.foreignkey (table1’s primary key)

AND condition;

39
New cards
<p>Write a <strong>query </strong>that <strong>selects </strong>the <strong>name</strong>, <strong>manager </strong>and <strong>email address </strong>of <strong>employees </strong>in the ‘<strong>Sales</strong>’<strong> department</strong>:</p>

Write a query that selects the name, manager and email address of employees in the ‘Sales department:

SELECT Employees.Name, Departments.Manager, Departments.Email

FROM Employees, Departments

WHERE Employees.EmployID = Departments.EmployID

AND Department = ‘Sales’;

<p><strong>SELECT </strong>Employees.Name, Departments.Manager, Departments.Email</p><p><strong>FROM </strong>Employees, Departments</p><p><strong>WHERE </strong>Employees.EmployID = Departments.EmployID</p><p><strong>AND </strong>Department = ‘Sales’;</p>
40
New cards