How Do You Manage Database Tables in SQL?

  • MHB
  • Thread starter mathmari
  • Start date
In summary: Ibis', 'Mailand');INSERT INTO BuchungHVALUES (1, '1', '1', '20211030', '20211107'), (2, '2', '1', '20211030', '20211107'), (3, '3', '4', '20211031', '20211104'), (4, '3', '2', '20211030', '20211107'),
  • #1
mathmari
Gold Member
MHB
5,049
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
  • #2
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. 🤔
 
  • #3
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:
  • #4
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)
 
  • #5
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:
 
  • #6
Can we check that for example here or is that not possible? :unsure:
 
  • #7
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. 🤔
 
  • #8
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:
 
  • #9
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. 🤔
 

FAQ: How Do You Manage Database Tables in SQL?

How do I create a table in a database?

To create a table in a database, you will need to use a Structured Query Language (SQL) command. The specific syntax for creating a table may vary depending on the database management system you are using, but the general format is:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
...
);
This command will create a table with the specified name and columns, along with their respective data types.

What is the purpose of primary keys in a table?

Primary keys are used to uniquely identify each record in a table. They ensure that each row in a table has a unique identifier, which is especially important when working with large databases. Primary keys also allow for efficient data retrieval and help maintain the integrity of the data in the table.

How can I delete a table from a database?

To delete a table from a database, you will need to use the DROP TABLE command in SQL. The syntax for this command is:
DROP TABLE table_name;
This will permanently remove the specified table and all of its data from the database.

Can I add columns to an existing table?

Yes, you can add columns to an existing table using the ALTER TABLE command in SQL. The syntax for this command is:
ALTER TABLE table_name
ADD column_name datatype;
This will add a new column to the existing table with the specified name and data type.

Is it possible to rename a table in a database?

Yes, you can rename a table in a database using the RENAME TABLE command in SQL. The syntax for this command is:
RENAME TABLE old_table_name TO new_table_name;
This will change the name of the specified table to the new name you have chosen.

Similar threads

Replies
67
Views
3K
Replies
35
Views
2K
Replies
6
Views
1K
Replies
3
Views
461
Replies
1
Views
1K
Replies
2
Views
2K
Back
Top