Creating tables - integrity constraints

  • MHB
  • Thread starter mathmari
  • Start date
  • Tags
    Constraints
In summary: But shouldn't we use a default value for status at the moment of creating the table? So that it is not null and it has a value. :unsure:That could be an option. But then we need to know what the initial value of Status should be. And maybe it is also fine to allow NULL for the status. It depends on the specific requirements of the system we are designing. :unsure:In summary, we discussed the creation of three tables - Kundenstamm, Produkte, and Bestellungen - and their respective attributes and integrity constraints. For Kundenstamm, we included a primary key and attributes for Name, Stadt, Land, Saldo, and Rabatt. For Produkte, we specified a primary
  • #1
mathmari
Gold Member
MHB
5,049
7
Hey! 😊

Create the following tables:

1. Kundenstamm
  • KNr (Primary Key)
  • Name (at most 15 characters)
  • Stadt (at most 10 characters)
  • Land (at most 10 characters)
  • Saldo (Type FLOAT)
  • Rabatt (Type FLOAT)
2. Produkte
  • PNr (greater than 1 and Primary Key)
  • Bez (not NULL, at most 10 characters and unique)
  • Gew (Type FLOAT)
Think about the integrity constraints for the columns Preis, Lagerort and Vorrat.

3. Bestellungen
  • BestNr (Type INTEGER, greater than 0 and Primary Key)
  • Mon (Type INTEGER, not NULL and between 1 and 12)
  • Tag (Type INTEGER, not NULL and between 1 and 31)
  • PNr (Foreign Key)
  • KNr (Foreign Key)
The attributes Monat, Tag, Pnr and Knr must together be unique. Think about the integrity constraints for the columns Menge, Summe and Status.
So we write for 1 the following :

CREATE TABLE Kundenstamm (
KNr PRIMARY KEY,
Name CHAR(15),
Stadt CHAR(10)
Land CHAR(10)
Saldo FLOAT
Rabatt FLOAT
);

:unsure:For 2 :

CREATE TABLE Produkte (
PNr PRIMARY KEY CHECK (PNr > 1) ,
Bez NOT NULL CHAR(10) UNIQUE.
Gew FLOAT
Preis FLOAT CHECK (Preis > 0) // Is checking if it is positiv an integrity constraint?
Lagerort CHAR(15) // What integrity constraint do we use here? If it is not Null for example?
Vorrat INTEGER // What integrity constraint do we use here? If it is not negativ for example?
);

:unsure:For 3 :

CREATE TABLE Bestellungen (
BestNr INTEGER PRIMARY KEY CHECK (BestNr > 0) ,
Mon INTEGER NOT NULL CHECK(Mon >= 1 and Mon <=12)
Tag INTEGER NOT NULL CHECK(Tag >= 1 and Tag <=31)
FOREIGN KEY (PNr) REFERENCES Kundenstamm (PNr),
FOREIGN KEY (KNr) REFERENCES Produkte (KNr)
Menge INTEGER CHECK(Menge >0) // It is the ordered quantity, or not? What integrity constraints can we consider?
Summe FLOAT // Is this the sum of invoices? Or what is this meant? What integrity constraints can we consider?
Status CHAR(20) // It is meant if is paid, delivered, etc? So this contains words, right? What integrity constraints can we consider?
UNIQUE (Mon, Tag, Pnr, Knr)
);

Do we write that as in the last line that the attributes Monat, Tag, Pnr and Knr must together be unique ?:unsure:
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Hey mathmari!

It seems all fine to me. (Nod)
I think there is no need to overthink the constraints. Typically we should decide if we want them to be required or not.
That is, either NOT NULL, or unspecified to allow NULL.
I believe CHECK is also considered an integrity constraint. 🤔

It depends a bit on the version of SQL that we use how to combine attributes into a unique constraint.
For a number of SQL versions we can apparently use something like "CONSTRAINT UC_Person UNIQUE (ID,LastName)" as explained in w3schools. 🤔
 
  • #3
Klaas van Aarsen said:
It seems all fine to me. (Nod)
I think there is no need to overthink the constraints. Typically we should decide if we want them to be required or not.
That is, either NOT NULL, or unspecified to allow NULL.
I believe CHECK is also considered an integrity constraint. 🤔

So at the ones that we have to give integrity constraints it is not enough just to give the type, right?

So :

Preis FLOAT CHECK (Preis > 0) :
This one is a complete integrity constraint, right?

Lagerort CHAR(15) NOT NULL :
We want that the storage location is specified, or not?

Vorrat INTEGER CHECK (Vorrat >= 0) :
The stock must be non-negativ, or not?

Menge INTEGER CHECK(Menge >0) :
This one is a complete integrity constraint, right?

Summe FLOAT :
I suppose that this is from the description of the other thread " The invoice amount for an order is the product of the price and the ordered quantity of the ordered product minus the customer discount.". So what integrity constraint can we use here?


Status CHAR(20) :
At the description of the other thread we have " The status of a paid order must never change again." and " Der Status einer Bestellung darf sich nur in “geliefert” ¨andern. Der Status einer gelieferten Bestellung nur in “bezahlt”. ". Can we maybe use these ones to make a integrity constraint?

:unsure:
 
  • #4
It doesn't say that we "have to give constraints". Instead it only says to "think about the integrity constraints".
I think that usually most constraints - if they are specified - are just NOT NULL. 🤔

It seems indeed reasonable that Vorrat must be specified. And if we have a Vorrat, then we also want to know where that Vorrat is, so the Lagerort should be NOT NULL.
Summe should probably be NOT NULL, and we might for instance require that it is non-negative.
I'm not aware of any content constraint we can set on a text attribute, other than NOT NULL. :unsure:
 
  • #5
So we have the following : For 1 :

CREATE TABLE Kundenstamm (
KNr PRIMARY KEY,
Name CHAR(15),
Stadt CHAR(10)
Land CHAR(10)
Saldo FLOAT
Rabatt FLOAT
);For 2 :

CREATE TABLE Produkte (
PNr PRIMARY KEY CHECK (PNr > 1) ,
Bez NOT NULL CHAR(10) UNIQUE.
Gew FLOAT
Preis FLOAT CHECK (Preis > 0)
Lagerort CHAR(15) NOT NULL
Vorrat INTEGER NOT NULL
);For 3 :

CREATE TABLE Bestellungen (
BestNr INTEGER PRIMARY KEY CHECK (BestNr > 0) ,
Mon INTEGER NOT NULL CHECK(Mon >= 1 and Mon <=12)
Tag INTEGER NOT NULL CHECK(Tag >= 1 and Tag <=31)
FOREIGN KEY (PNr) REFERENCES Kundenstamm (PNr),
FOREIGN KEY (KNr) REFERENCES Produkte (KNr)
Menge INTEGER CHECK(Menge >0)
Summe FLOAT NOT NULL CHECK(Summe >= 0)
Status CHAR(20) NOT NULL
UNIQUE (Mon, Tag, Pnr, Knr) OR CONSTRAINT Comb UNIQUE (Mon, Tag, Pnr, Knr)
);Is everything correct ? :unsure:
 
  • #6
It looks correct to me.
However, in your other thread it appears there is supposed to be a dynamic constraint on Status.
After it has a value, it may not be changed.
In that case it makes sense to me that Status should be allowed to be NULL. Then we can place an order, and set its state later. 🤔
 

1. What are integrity constraints in database tables?

Integrity constraints are rules that are applied to the data in a database table to ensure its accuracy and consistency. These constraints help to maintain the quality of the data and prevent any invalid or incorrect data from being entered into the table.

2. Why are integrity constraints important in creating database tables?

Integrity constraints are important because they help to maintain the integrity of the data in a database. They ensure that the data is accurate, consistent, and follows certain rules, which is crucial for the proper functioning of the database and the applications that use it.

3. What are the different types of integrity constraints that can be applied to a table?

There are several types of integrity constraints that can be applied to a table, including primary key, foreign key, unique, check, and not null constraints. Each of these constraints serves a specific purpose and helps to maintain the integrity of the data in different ways.

4. How do integrity constraints affect the performance of a database?

Integrity constraints can have a positive impact on the performance of a database by ensuring that the data is accurate and consistent. However, they can also slow down the performance if they are not properly designed or if there are too many constraints on a table. It is important to strike a balance between data integrity and performance when creating tables.

5. Can integrity constraints be modified or removed after a table has been created?

Yes, integrity constraints can be modified or removed after a table has been created. However, this should be done carefully and with caution, as it can affect the data in the table and potentially cause errors or conflicts. It is recommended to consult with a database administrator before making any changes to integrity constraints.

Back
Top