All courses › Databases, Networks and Security
Databases, Networks and Security: free practice, theory and problems
Almost every app stores its data in a database: users, orders, measurements. A relational database stores data in tables, and you fetch exactly what you need with the query language SQL.
Contents
1. Relational databases and SQL
What is it about?
Almost every app stores its data in a database: users, orders, measurements. A relational database stores data in tables, and you fetch exactly what you need with the query language SQL.
In short
- A table has columns (fields) and rows (records).
- Primary key: a column that identifies each row uniquely, for example
student_id. - Foreign key: a column that points to the primary key of another table. This is how tables are linked.
- The basic form of SQL:
SELECT columns FROM table WHERE condition ORDER BY column;
- Aggregate functions:
COUNT(*),SUM(x),AVG(x),MIN(x),MAX(x). WithGROUP BYthey are computed per group. - JOIN combines rows from two tables where the keys match:
SELECT s.name, e.course FROM Student s JOIN Exam e ON e.student_id = s.id;
- Normalization: each fact is stored in one place. That prevents conflicting copies when something changes.
The relational model
A relational database stores data in tables (relations) with rows (records) and columns (attributes). Each table has a primary key that uniquely identifies each row, and tables are linked by foreign keys pointing to the primary key in another table. An order, for example, points to the customer with a customer_id column.
Normalisation
The aim is to avoid redundancy – the same fact stored in several places that can become inconsistent.
- 1NF: each cell holds one value, no repeating groups.
- 2NF: every column depends on the whole primary key.
- 3NF: no column depends on another non-key column (postcode → town belongs in a separate table).
An ER diagram (entity–relationship) is used to model before creating tables. Relationships can be one-to-one, one-to-many and many-to-many; the last is resolved with a junction table.
SQL in a nutshell
- SELECT columns FROM table WHERE condition ORDER BY column.
- JOIN connects tables: INNER JOIN gives only rows matching in both, LEFT JOIN keeps all rows from the left table.
- GROUP BY with aggregate functions (COUNT, SUM, AVG, MIN, MAX); HAVING filters groups, while WHERE filters rows.
- INSERT, UPDATE, DELETE change data – never forget WHERE on UPDATE and DELETE.
- CREATE TABLE with data types and constraints (PRIMARY KEY, FOREIGN KEY, NOT NULL, UNIQUE).
Transactions and ACID
A transaction is a group of operations that must happen completely or not at all – like a bank transfer. ACID stands for atomicity (all or nothing), consistency (the rules hold), isolation (concurrent transactions do not interfere) and durability (what is committed survives a power cut).
Performance and security
Indexes make searches fast but slow down inserts a little. SQL injection – user input interpreted as SQL – is one of the most common security flaws; always use parameterised queries. NoSQL databases (document, key–value, graph) are alternatives when data does not fit fixed tables.
How to solve the problems
- Read
FROMfirst (which table), thenWHERE(which rows), thenSELECT(what is shown). - Go through the rows one by one and check the condition.
- Apply the aggregate function to the remaining rows.
Example
The table Employee has the salaries 520,000, 610,000, 480,000 and 700,000. What does SELECT COUNT(*) FROM Employee WHERE salary > 500000; give?
- The rows with salary above 500,000: 520,000, 610,000 and 700,000.
COUNT(*)counts them: the result is 3.
Common mistakes
- Forgetting
WHEREin anUPDATEorDELETE. Then all rows are changed or deleted. - Mixing up
WHERE(filters rows) andHAVING(filters groups afterGROUP BY). - Storing the same fact in several tables, so they end up disagreeing.
Concepts in this part
2. IP networks and subnetting
What is it about?
Everything connected to the internet has an IP address. To split a network into smaller parts, for example one for the office and one for production, you use subnetting. You also need to calculate how long it takes to send data.
In short
- An IPv4 address is 32 bits, written as four numbers 0–255:
192.168.1.10. - Prefix
/p: the first bits are the network part, the rest are for machines (hosts). - The number of addresses in a subnet is . The number of usable hosts is
because the network address (first) and the broadcast address (last) cannot be used by machines.
- /24 corresponds to the netmask
255.255.255.0(256 addresses, 254 hosts). - The network address is found by rounding the last part down to a whole number of blocks: the block size is .
- Transfer time: . Remember that 1 byte = 8 bits.
- Layers: IP is the network layer, TCP and UDP are the transport layer. TCP guarantees delivery in the right order. DNS translates names to IP addresses.
The layer model
Network communication is split into layers, each using the services of the layer below. In the TCP/IP model:
- Link layer: transfer within one local network, with MAC addresses (Ethernet, Wi-Fi). Switches work here.
- Network layer: IP addresses and routes packets between networks. Routers work here.
- Transport layer: TCP gives a reliable, ordered stream with acknowledgements and retransmission (web, email, files); UDP is fast with no guarantees (video, games, DNS). Port numbers separate services: 80 HTTP, 443 HTTPS, 22 SSH, 53 DNS.
- Application layer: HTTP, DNS, SMTP and others.
Addresses in practice
- Private addresses (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) are used inside homes and companies. NAT in the router translates them to one public address.
- DHCP hands out IP address, subnet mask, default gateway and DNS server automatically.
- ARP finds the MAC address belonging to an IP address on the local network.
- A host sends directly to receivers in the same subnet; everything else goes via the gateway.
More on subnetting
The prefix sets the size: each bit borrowed from the host part halves the subnet and doubles the number of subnets. A /24 can be split into two /25 (126 hosts each), four /26 (62) or eight /27 (30). VLSM lets you use different sizes as needed, for example /30 (2 hosts) for point-to-point links between routers. Always plan for growth.
IPv6
IPv4 has only about 4.3 billion addresses, and they have run out. IPv6 has 128-bit addresses written in hexadecimal (2001:db8::1), so many that every device can have a public address without NAT.
Troubleshooting
ping tests whether a host replies, traceroute/tracert shows the path through the routers, ipconfig/ip addr shows your own configuration, and nslookup tests DNS. Often the problem is a wrong gateway, wrong subnet mask or DNS.
How to solve the problems
- Hosts: compute .
- Network address: block size , divide the last octet by it and round down.
- Time: convert MB to bits () before dividing by Mbit/s.
Example
How long does it take to download 100 MB at 50 Mbit/s?
- Mbit.
- s.
Common mistakes
- Forgetting the two reserved addresses when counting hosts.
- Mixing MB (megabytes) and Mb (megabits), a factor of 8.
- Thinking a larger prefix gives more addresses. It is the opposite: /30 is much smaller than /24.
Concepts in this part
3. Cyber security
What is it about?
Every engineer needs basic cyber security: data must be kept secret, not changed behind your back, and be available when needed. Many attacks start with a weak password or a tricked user.
In short
- CIA: confidentiality (only the right people can read), integrity (data is not changed without authorization), availability (the system works when needed).
- Password strength (entropy) in bits, when each character is chosen at random from characters and the password has characters:
- The number of possible passwords is . Time to try them all: divided by the number of guesses per second.
- Hash function: turns data into a fingerprint that cannot be reversed. Passwords are stored as hashes (with salt), never in plain text.
- Symmetric encryption: the same key locks and unlocks. Asymmetric: the public key encrypts, only the private key decrypts.
- Two-factor authentication: something you know (password) plus something you have (phone or token).
- Phishing: fake emails or sites that trick the user into giving away passwords.
What must be protected?
ICT security is about protecting confidentiality (only the right people can see), integrity (data is not changed without authorisation) and availability (the system works when needed) – the CIA triad. A risk assessment combines likelihood and impact to prioritise measures.
Common attacks
- Phishing and social engineering: fake emails or calls that trick people into giving away passwords or clicking malicious links. The most common way in.
- Malware: viruses, worms, trojans and ransomware, which encrypts data and demands payment.
- Password attacks: guessing, credential stuffing (leaked passwords tried on other services) and dictionary attacks on leaked hashes.
- Denial of service (DDoS): overload from many machines at once.
- Software vulnerabilities: SQL injection, cross-site scripting, unpatched systems.
- Man-in-the-middle: eavesdropping on or altering traffic on insecure networks.
Defence in depth
- Authentication: long passphrases, a password manager and multi-factor authentication (MFA) – the single most effective barrier against account takeover.
- Least privilege: users and programs get only the rights they need.
- Updates (patching) close known holes; many attacks exploit vulnerabilities that have had fixes for months.
- Encryption: TLS (HTTPS) for data in transit, encrypted disks for data at rest. Passwords are stored as salted hashes with a slow algorithm, never in plain text.
- Firewalls, network segmentation and monitoring/logging.
- Backups following the 3-2-1 rule (three copies, two media, one off-site) – and test that restoring works. It is the best defence against ransomware.
- User training.
Regulation and incident response
GDPR requires personal data to be secured and breaches to be reported to the Data Protection Authority within 72 hours. NSM (the Norwegian National Security Authority) publishes basic principles for ICT security. During an incident: detect, contain, eradicate, recover and learn – and have the plan ready in advance.
How to solve the problems
- Entropy: multiply the length by of the number of possible characters.
- Cracking time: compute and divide by guesses per second, then convert to a suitable unit.
Example
A password of 8 lowercase letters (26 possible characters):
- bits.
- possibilities. At guesses per second, all are tried in under half a minute.
Common mistakes
- Thinking complicated short passwords beat long ones. Length gives the most strength.
- Storing passwords in plain text or with a fast hash without salt.
- Reusing the same password in several places.
Concepts in this part
Example problems with solutions
Here are some of the problems in databases, Networks and Security. In the app, calculation problems get new numbers every time, so you can practise until it sticks – and take a practice exam with an indicative grade as you prepare for the exam.
Relational databases and SQL: What is a primary key in a table?
Answer: A column that identifies each row uniquely
The primary key is unique and never empty, so each row can be found unambiguously. A column pointing to another table is a foreign key.
IP networks and subnetting: How many usable host addresses does a /26 network have?
Answer: 62
.
Cyber security: How many bits of entropy does a random password of 8 lowercase letters (26 possible characters) have?
Answer: 37.604 bit
bits.
Relational databases and SQL: The table Employee has the salaries 520,000, 610,000, 480,000 and 700,000. What does the query return?SELECT COUNT(*) FROM Employee WHERE salary > 500000;
SELECT COUNT(*) FROM Employee WHERE salary > 500000;Answer: 3
The rows above 500,000 are 520,000, 610,000 and 700,000. COUNT(*) gives 3.
Matches these university courses
The content follows the syllabus found in, for example:
- TDT4145 (NTNU)