How Do You Manage Database Tables in SQL?

  • Context: MHB 
  • Thread starter Thread starter mathmari
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around managing database tables in SQL, specifically focusing on creating tables, inserting data, defining primary keys, and deleting tables. Participants explore the syntax and requirements for SQL commands, as well as the implications of data types and table structure.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • Some participants propose creating tables with specific columns and data types, questioning whether the length of varchar should be standardized or adjusted based on context.
  • There is a suggestion that the "von" and "bis" fields should be of DATE type rather than integer, with a discussion on the appropriate format for date values.
  • Participants discuss the concept of primary keys, with some clarifying that a primary key must uniquely identify each row and can generally be chosen by the user, although specific columns are indicated in the homework.
  • There is uncertainty about the correct SQL syntax for inserting values, particularly regarding the use of quotes for date values and integer fields.
  • Some participants express confusion about the commands for deleting tables and whether they should be executed separately or in one command.
  • There is a mention of checking SQL commands in a specific environment, with participants discussing how to structure commands for testing.

Areas of Agreement / Disagreement

Participants generally agree on the need for primary keys and the structure of SQL commands, but there are multiple competing views regarding data types, the necessity of quotes in SQL commands, and the specifics of table management. The discussion remains unresolved on some technical details and best practices.

Contextual Notes

Participants express uncertainty about the correct data types and formats for SQL commands, as well as the implications of using different SQL flavors. There are also unresolved questions about the best practices for defining primary keys and managing table structures.

Who May Find This Useful

This discussion may be useful for students learning SQL, database management, or those seeking clarification on best practices for creating and managing database tables.

mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! 😊

Consider the relations Personen, Hotel and BuchungH :
1638178182919.png
1638178194437.png
1638178210363.png


1. Create the three relations in the database.
2. Insert the tuples in all tables.
3. Create meaningful keystones for all tables.
4. Give an expression that deletes again the tables. Could you explain to me what we are supposed to do? Do we have to create the tables with SQL commands? :unsure:

If it is like that, then do we have the following at 1 ?

CREATE TABLE Personen (
PID int,
Name varchar(255),
);

CREATE TABLE Hotel (
HID int,
Name varchar(255),
Ort varchar(255),
);

CREATE TABLE BuchungH (
Bnr int,
PID int,
HID int,
von int,
bis int,
); Is everything correct and complete? The length of 255 is standard? Or does it depend each time on the specific table? :unsure: At 2 do we have the following ?

INSERT INTO Personen
VALUES ('1', 'Schmidt'),
('2', 'Meier'),
('3', 'Beier'),
('4', 'Bauer'),
('5', 'Wolf');

INSERT INTO Hotel
VALUES ('1', 'Beach Club', 'Mallorca'),
('2', 'Radisson', 'Mallorca'),
('3', Le Flaubert', 'Nizza'),
('4', 'Ibis', 'Nizza'),
('5', 'Ibis', 'Mailand');

INSERT INTO BuchungH
VALUES ('1', '1', '1', '20211030', '20211107'),
('2', '2', '1', '20211030', '20211107'),
('3', '3', '4', '20211031', '20211104'),
('4', '3', '2', '20211030', '20211107'),
('5', '4', '4', '20211031', '20211108');
Is everything correct and complete? :unsure: Could you explain to me questions 3 and 4 ? :unsure:
 
Physics news on Phys.org
mathmari said:
CREATE TABLE BuchungH (
Bnr int,
PID int,
HID int,
von int,
bis int,
);
Is everything correct and complete? The length of 255 is standard? Or does it depend each time on the specific table?

Hey mathmari!

I think "von" and "bis" should be DATE's instead of integers. 🤔

For now we can use a length of 255. If the database grows bigger, it might help to use a fixed length string to improve performance. That is not a consideration now though. 🤔

mathmari said:
At 2 do we have the following ?

INSERT INTO Personen
VALUES ('1', 'Schmidt'),
('2', 'Meier'),
('3', 'Beier'),
('4', 'Bauer'),
('5', 'Wolf');
Is everything correct and complete?

If the type of a field is integer, then we should provide a value without quotes. 🤔

mathmari said:
Could you explain to me questions 3 and 4 ?

"keystones" seems to be the wrong term. I think that instead we need PRIMARY KEY's.
That is, we mark a column as a primary key, which enforces that it must be unique. 🤔

In question 1 we used CREATE TABLE to create the tables. In question 4 we're supposed to use DELETE TABLE to delete them. 🤔
 
So do we have the following ?

1 :

CREATE TABLE Personen (
PID INTEGER,
Name CHAR(255),
);

CREATE TABLE Hotel (
HID INTEGER,
Name CHAR(255),
Ort CHAR(255),
);

CREATE TABLE BuchungH (
Bnr INTEGER,
PID INTEGER,
HID INTEGER,
von DATE,
bis DATE,
);

2 :

INSERT INTO Personen
VALUES (1, 'Schmidt'),
(2, 'Meier'),
(3, 'Beier'),
(4, 'Bauer'),
(5, 'Wolf');

INSERT INTO Hotel
VALUES (1, 'Beach Club', 'Mallorca'),
(2, 'Radisson', 'Mallorca'),
(3, 'Le Flaubert', 'Nizza'),
(4, 'Ibis', 'Nizza'),
(5, 'Ibis', 'Mailand');

INSERT INTO BuchungH
VALUES (1, 1, 1, '20211030', '20211107'),
(2, 2, 1, '20211030', '20211107'),
(3, 3, 4, '20211031', '20211104'),
(4, 3, 2, '20211030', '20211107'),
(5, 4, 4, '20211031', '20211108');At the dates we need the quotes, don't we? :unsure:

3 :

What exactly is the primary key ? Is that the column that identifies the table? Do we chose that by ourselves?
The column that we choose must contain only uniquely defined elements, right?
So in Personen both columns can be the primary key, in Hotel the first column must be the primary key and in BuchungH the first column must be the primary key, right?

:unsure:

4 :

DROP TABLE Personen;
DROP TABLE Hotel;
DROP TABLE BuchungH;

Or do we write that in one command? :unsure:
 
Last edited by a moderator:
mathmari said:
At the dates we need the quotes, don't we?

Yes, we need the quotes. But I think it should be like '2021-10-30'. That is, it seems the DATE format is usually YYYY-MM-DD. 🤔

mathmari said:
3 :
What exactly is the primary key ? Is that the column that identifies the table? Do we chose that by ourselves?
The column that we choose must contain only uniquely defined elements, right?
So in Personen both columns can be the primary key, in Hotel the first column must be the primary key and in BuchungH the first column must be the primary key, right?

The primary key is usually the first column in a table and it uniquely identifies each row in the table.
We can generally choose it ourselves, but in the questions it is already given.
What is left is to tell SQL that it is a PRIMARY KEY.
See for instance this w3schools page. It also shows that the syntax is different for different flavors of SQL. 🤔
mathmari said:
4 :
DROP TABLE Personen;
DROP TABLE Hotel;
DROP TABLE BuchungH;
Or do we write that in one command?

We created each table in a separate command, so it makes sense to also drop them in separate commands. (Nod)
 
Klaas van Aarsen said:
Yes, we need the quotes. But I think it should be like '2021-10-30'. That is, it seems the DATE format is usually YYYY-MM-DD. 🤔

Ah although in the given table we have all number together wew can nwrite that in this way, right?

So do we have :

INSERT INTO BuchungH
VALUES (1, 1, 1, '2021-10-30', '2021-11-07'),
(2, 2, 1, '2021-10-30', '2021-11-07'),
(3, 3, 4, '2021-10-31', '2021-11-04'),
(4, 3, 2, '2021-10-30', '2021-11-07'),
(5, 4, 4, '2021-10-31', '2021-11-08');:unsure:
Klaas van Aarsen said:
The primary key is usually the first column in a table and it uniquely identifies each row in the table.
We can generally choose it ourselves, but in the questions it is already given.
What is left is to tell SQL that it is a PRIMARY KEY.
See for instance this w3schools page. It also shows that the syntax is different for different flavors of SQL. 🤔

So the primary key is the id ofeach table, right? ;unsure:So do we write the following ?

ALTER TABLE Personen
ADD PRIMARY KEY (PID);

ALTER TABLE Hotel
ADD PRIMARY KEY (HID);

ALTER TABLE BuchungH
ADD PRIMARY KEY (Bnr);:unsure:
 
Can we check that for example here or is that not possible? :unsure:
 
mathmari said:
ALTER TABLE Personen
ADD PRIMARY KEY (PID);

ALTER TABLE Hotel
ADD PRIMARY KEY (HID);

ALTER TABLE BuchungH
ADD PRIMARY KEY (Bnr);

Yep. That works. (Nod)

mathmari said:
Can we check that for example here or is that not possible?

Yes. We can check there. The CREATE TABLE and INSERT INTO commands can go in the left pane. The SELECT queries go into the right pane.
We can select the SQL flavor. Previously I selected PostgreSQL so that syntax would be the same as in your other thread. 🤔
 
Klaas van Aarsen said:
Yes. We can check there. The CREATE TABLE and INSERT INTO commands can go in the left pane. The SELECT queries go into the right pane.
We can select the SQL flavor. Previously I selected PostgreSQL so that syntax would be the same as in your other thread. 🤔

And the Primary Key goes to the left ? :unsure:
 
mathmari said:
And the Primary Key goes to the left ?
Yes. The identification of the primary key is part of the 'schema' of the tables. 🤔
 

Similar threads

  • · Replies 67 ·
3
Replies
67
Views
4K
  • · Replies 35 ·
2
Replies
35
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 64 ·
3
Replies
64
Views
6K