Вы находитесь на странице: 1из 11

Introduction to Database Shazia Abbasi

1.1 Enforcing Constraints


A constraint is a limitation or restriction that is enforced on fields, for
verifying data that is being entered into a table.

Primary key Constraint:

A primary key constraint is one , which ensures that the user does not enter duplicate values in
the field or set of fields on which constraint is enforced.

Check Constraint:

Most RDBMSs automatically validate value based on a field’s data type. That is, text value is not
allowed into a ‘Date’ or numeric data type. In addition to these the users can set some validation
rules or ‘Check Constraints’ that check validity of the data entering the database.

Two types of Check Constraints

Field Level Check Constraint


Record Level Check Constraint

Not Null Constraint

This constraint is used to ensure that the user does not enter a ‘NULL’ value into a field. A ‘NULL’
value is best defined as absence of any value in a field. This constraint can therefore be used to
ensure that the user is forced to enter a value into some field in a table.

In MS Access, this is done by setting the ‘Required’ property to ‘YES’.

Default Constraint:

This constraint is used to specify a value that will be displayed and stored in a field by default, in
case the user does not enter any value.

17/05/2020 1
Introduction to Database Shazia Abbasi

1.2 Field Properties


1.2.1 What Are Field Properties?
Field properties are a way of limiting the information which people can type into your table - useful
if you want to try and minimise the possibility of mistakes. For example, you can use them to…

 Limit the number of characters allowed into a field, e.g. allowing only 10 characters for
apostcode
 Specify a default, or automatic value, e.g. 'London' in the town field, if most of your addresses
are in London
 Change the way that data appears, e.g. formatting a date which is typed in 1/1/99 to appear
as 01 January 1999

1.2.2 Setting Field Properties


You can change the field properties at any time, but…
…. if you change them after you have already inputted data to the table, you
may lose some of the information. If existing data doesn't fit the new
requirements, Access may just delete it.

1. Ensure you are in Design view of the table


2. Click into the field you wish to set properties for

3. Change the properties as required (see next sections)

17/05/2020 2
Introduction to Database Shazia Abbasi

4. Click Save

Field properties change depending on the data type!


e.g. fields with a Text data type will have different properties available than a
field with a Number date type etc.

1.2.3 Changing Field Size


What does it mean?: The maximum number of characters which people can input into a field
(including spaces).
When am I likely to use it?: For text fields where there is always a limited number of characters,
e.g. postcode, phone number.

1.2.4 Changing Format


What does it mean?: Changes the way in which the data appears.
When am I likely to use it?: For dates or numbers which you wish to appear differently to the
way they are inputted, e.g. numbers which you wish to become percentages, dates which you
wish to become long dates, 01 Jan 1999.

1. Open the tables in design view that have date data types.
2. For each date field Click in Format box
3. Click down arrow

4. Click on format you require


5. After completing the above task open the Pending table in Design view
6. Select CCode Field
7. Enter ‘>’ (without inverted commas) in the Format property of the CCode field
8. When you enter a value in the CCode field and move out of it, you will notice that it
converts to Upper case.
9. The ‘Format’ property only controls the way data is displayed, not the way it is stored.

The format property can also be used to display some value in the field in case
some value is not entered here. The value in the ‘Default Value’ property is
displayed and stored in the table if a value is not specified. The alternate text
displayed in the example is only displayed, not stored.

10. To ensure that the word ‘None’ appears when there is no value in a text field, you
could type the custom format @;”None” as the control’s Format property setting.

17/05/2020 3
Introduction to Database Shazia Abbasi

11. The @ symbol causes the text from the field to be displayed; the second section
causes the word ‘None’ to appear when there is zero- length string or ‘Null’ value in
the field.

1.2.5 Setting an Input Mask


What does it mean?: Forces the information which is inputted to appear in a certain way. The
input mask determines how it should appear, for example an input mask of '0000' would force
people to enter four numbers into the field.
When am I likely to use it?: When there is a standard format for the field in question, e.g.
National Insurance numbers which are always 2 letters, followed by six numbers and ending in
one letter.

1. Open the customer table

2. For CCode click into Input Mask box


3. Type in input mask you require, e.g. LL000000L for National Insurance number

These are the symbols you are most likely to use in your input mask:-

0 Number which must be filled in


9 Number which does not have to be filled in
# Number or space which does not have to be filled in and can include plus or minus signs
L Letter which must be filled in
? Letter which does not need to be filled in
A Letter or number which must be filled in
a Letter or number which does not need to be filled in
& Any character or space which must be filled in
C Any character or space which does not have to be filled in
< All characters which follow this will be converted to lowercase (the symbol does not show when
you input data)
> All characters which follow this will be converted to uppercase (the symbol does not show when
you input data)
\ Causes the character that follows it to be displayed as a literal (exact)character

17/05/2020 4
Introduction to Database Shazia Abbasi

Suppose the input mask “APT-“0000;0;* were to be entered in the Input Mask property for the
MembersCode field. We would be able to enter only Member Codes such as APT-0001, APT-
3432, APT-9999 etc.

Input Mask has three parts of which the first part (the mask itself) is mandatory:

“APT-“0000 ; 0 ;*

The mask itself; The


inverted commas around
Semi
‘APT-‘ ensure that ‘APT-‘ The PlaceHolder
Colons: Act
is displayed in every character: The character
as Save Literals? ‘0’ if
Member’s Code as a entered here is
separators the literal characters
Constant. ‘APT- ‘ are displayed in the field (in
between the are to be stored, and
also referred to as datasheet view). It
3 parts of ‘1’ if they are not to
‘literal’ characters. The indicates the number of
the Input be stored. If this part
Four zeros indicate that letters or numbers to be
Mask is left empty, it is
the user is expected to entered in the field
enter 4 numbers taken by default to
compulsorily be ‘1’

17/05/2020 5
Introduction to Database Shazia Abbasi

1.2.6 Adding a Caption


What does it mean?: The text which appears at the top of the column in Datasheet view.
When am I likely to use it?: When you want the text at the top of the column to be different from
the actual field name.

1.2.7 Changing the Default Value


What does it mean?: Text which is already typed into the field before you start to input data.
When am I likely to use it?: When you often type the same data into a field, e.g. if most of your
records contain London in the address, you could change the default for town/city to London.

1. Set the default value of ‘OrderDate’ field in the ‘OrderMaster table to Date(), and
save the table

2. Switch to datasheet view of the table and notice that the new or blank record
already displays the current date

1.2.8 Setting Validation Rule and Validation Text


What does it mean?: Setting a rule for the data which can be inputted into a field. The validation
text is the error message which will be displayed if the rule is broken.
When am I likely to use it?: One example would be a credit limit. Imagine that nobody can have
a credit limit over £500,000. You can set the validation rule as <500000, which forces Access to
only accept numbers which are less than 500,000.

1. Select OrderDate field and enter the ‘Validation Rule’ and Validation Text’ properties
as shown

17/05/2020 6
Introduction to Database Shazia Abbasi

2. Switch to the Datasheet view and enter some values into the table. Ensure that the
value entered in the ‘OrderDate’ field is greater than the current date
3. Move on to next record and observe the message.

Record Level Validation

1. Open the ‘OrderMaster’ table in Design view


2. Select OrderNo field

3. Cick on the properties button on the tool bar


4. Click on Validation rule box. This displays a little box with ellipsis ‘…’

4. Click on this button


5. This will invoke the Expression Builder dialog box

17/05/2020 7
Introduction to Database Shazia Abbasi

6. The name ‘OrderMaster’ table is displayed in the lower left pane of the window
7. The middle pane displays the list of fields in the selected table.
8. The right most pane does not seem to contain anything of great significance.
9. Double click on the ‘Functions’ folder in the left most pane. Notice that this displays a
sub folder called ‘Built in Functions’. Click on this and then click on Text functions in
the middle pane
10. Double click on the function ‘Right’ in the right pane so that it appears on the window
as shown in the diagram

11. Click on the <<stringexpr>>


12. Double click on the OrderNo field found in ‘OrderMaster’ folder
13. Click on <<n>> and type ‘2’ over it.

17/05/2020 8
Introduction to Database Shazia Abbasi

14. Type an ‘=’ sign after this and then follow the steps given above to add the ‘Right’
function again.
15. The final expression should look like this

16. Click on ‘OK’ button.


17. Enter the validation text as ‘ The OrderNo should end in the last two digits of the
Order Date year’

17/05/2020 9
Introduction to Database Shazia Abbasi

1.2.9 Making a Field Required


What does it mean?: People must enter something into this field when they create a new record.
When am I likely to use it?: When you have a field which you do not want to be left blank, e.g.
Organisation Name, phone number etc.

1. Open the ‘Pending Table’ in Design View


2. Select CCode field
3. Click into Required box
4. Click the drop down arrow
5. Click Yes

If you do not fill in a required field in Datasheet view, Access will display the following message…

Allowing Zero Length


What does it mean?: Allows you to leave a field blank on purpose.
When am I likely to use it?: When you have a field which you know is to be left blank.

1. Click into Zero Length box


2. Click the down arrow
3. Click Yes

Note:
 If you want to leave the field blank and don’t care why it’s blank, set both the Required
and Allow Zero Length properties to No.
 If you never want to leave a field blank, set Required to Yes and Allow Zero Length to No.
you cannot leave the field without entering a value, even if it’s only “None” or “don’t know”
 If you want to tell why the field is blank, set Required to No and Allow Zero Length to Yes.
Then you would leave the field blank or enter (“”) to indicate the filed dose not apply to
the current record.
 If you want to leave the field blank only if you know the field isn’t relevant to a record, set
both properties to Yes. Then the only way to leave the field blank is to enter a zero-length
string either by typing “” or pressing the SPACEBAR

17/05/2020 10
Introduction to Database Shazia Abbasi

Exercise

1. In the ‘Pending’ table make sure that the ‘PQty’ should never be below ‘0’ (zero). Also
provide an appropriate message that will be displayed in case the user enters an invalid
value
2. In the ‘Pending’ table, ensure that data must be entered for the ICode field. (ICode
cannot be a primary key)
3. In the Customer table ensure that the customer’s address should not be empty. However
in case the Order-Processing executive has not got the address he should enter at least
a zero length string (i.e. “ “) [Hint: The ‘Allow Zero Length’ property has to be used]
4. The CPhone field isone which may or may not have a value, depending on whether the
particular customer has a phone or not. Set the appropriate property of the ‘CPhone’ field
such that if any phone no exists, it is displayed, else ‘No Phone’ is displayed.
5. Set a suitable input mask for Order No in Pending, OrderMaster and OrderDetail table
6. Define input masks (on paper) to accept the following kinds of data in a field
a. ISBN0123-23 : The first 4 letters have to ‘ISBN’. This should be followed by 4
numbers (mandatory). A hyphen should follow this, and the last part of the data
should be able to hold a minimum of 2 numbers and a maximum of 4 numbers.
(for example: ISBN9231-213 AND ISB0213-2123 are both valid)
b. CON-323-A90: The first three characters should be letters (in upper case). The
second part of the code should have 3 numbers (each of which is mandatory).
The final portion should have a maximum of three characters – these could be
numbers or letters, but only one is compulsory.

17/05/2020 11