Thursday, February 27, 2014

Attribute Types of Database Tables



Attribute Types in Database Design


An attribute can be designed to store any values in it according to a particular domain (Domain would mean a set of permitted values. For example, in Phone attribute we are about to store set of phone numbers, in Name attribute of STUDENT table, we are about to store set of Student Names and not any other names) for which the attribute is designed. Apart from various domains, an attribute can be of many types like an attribute with one value, attribute with many values, and so on. Based on this semantics of an attribute, we can derive the following set of attribute types.

1. Simple attributes

Attributes whose value is simple, indivisible values. For example, attribute Salary is simple if salary stores single number as salary. Attribute Department_Name may store single name as value for every record.
* In ER diagram, simple attribute is represented with single ellipse.

2. Composite attributes

Attributes whose value is composite, i.e which can be further divided into meaningful component attributes. For example, according to the user requirement, if the Salary attribute needs to store all the information like Basic Salary, HRA, DA and any other allowances, then salary is a composite attribute with Basic_Salary, HRA, and DA as its component attributes.

 * In ER diagram, composite attributes are represented using links between the composite and component attributes. Here, each attribute is represented with an ellipse.

3. Single-valued attributes

Attribute which accepts only one value for every record is called single-valued attribute. For example, all the employees of any organization will be assigned with single employee number value. Hence, Emp_No attribute is single-valued.
* In ER diagram, single-valued attribute is represented with simple ellipse.

4. Multi-valued attributes

Attribute which may expect one or more values for every entity is called multi-valued attribute. For example, an organization while storing employee details may accept more than one phone number per employee.
* In ER diagram, multi-valued attribute is represented with a double ellipse.

5. Derived attributes

An attribute whose value can be derived from another attribute of the same table or from a set of entities is called Derived attribute. For example, in a table CAR_OWNERS which stores information about the car owners with every car’s individual information, we could include an attribute which is the count of all the cars owned by single owner. As another example, consider the DOJ (Date of Joining) of employee. The employment length of any employee can be calculated from the attribute DOJ’s value.
The value of Derived attribute need not be stored. The reason would be the dynamic nature of the attribute. For example, every year the Age of an employee will change.
* In ER diagram, a derived attribute is represented using dashed ellipse.

6. Descriptive attributes

An attribute which is used for storing information which is part of any relationship set is called descriptive attribute. For example, in a many-to-many relationship set Depositor (which relates the entity sets Customer and Account), we may include an attribute Last_Access_Date to record the last access of the account by the customer. This attribute will be part of table Depositor (which is reduced from relationship set to table).


Wednesday, February 26, 2014

CREATE TABLE statement in SQL



Structured Query Language - CREATE TABLE Statement Explained


Conventions used in writing the syntax:

1. Pair of square brackets ([]) represent optional portion. They may be used if necessary. They are not compulsory components in creating a table.
2. All the words given in Bold letters are Reserved Words (system defined key words).
3. The pipe symbol (|) is used to represent OR.
4. Pair of Curly Brackets ({}) used to represent groups. For example, Column-i-Definition mean the whole thing {Column-i-Name Data-type (size) [Column-Level-Constraint][DEFAULT value]}.

CREATE TABLE Table_Name
(Column-1-Definition,
Column-2-Definition,
Column-3-Definition, …,
Column-N-Definition,
[Table-Level-Constraint-1,
Table-Level-Constraint-2, …,
Table-Level-Constraint-N])

Column-i-Definition:

{ Column-i-Name Data-type (size) [Column-Level-Constraint][DEFAULT value] }

Column-Level-Constraint:

{[NOT NULL] | [CONSTRAINT constraint-name] {[CHECK (search-condition) | PRIMARY KEY | UNIQUE | REFERENCES clause]}}

Table-Level-Constraint:

{ [CONSTRAINT constraint-name] [CHECK (search-condition)] | [PRIMARY KEY (column-name1 [, column-name2, …])] | [UNIQUE (column-name1 [, column-name2, …])] | FOREIGN KEY (column-name1 [, column-name2, …]) REFERENCES clause] }

REFERENCES clause:

{ REFERENCES table-name [(column-name1 [, column-name2, …])] }


Discussions:

Column-i-Definition:

Any column defined as part of the table is of the form “column-name data-type(size)”. For example, the column Emp_Name can be defined as “Emp_Name VARCHAR(25)”. The Column-Level-Constraint or DEFAULT is optional.

DEFAULT – represents that the value for this column would be passed while defining the table structure. While entering a record into the table, if you leave the DEFAULT column without any values, the default value gets stored.

Can we create a complete table at this level using Column Definitions?

Yes. We can design simple table without much Integrity Constraints.
Example 1:

CREATE TABLE Employee ( Emp_No CHAR (5),
Emp_Name VARCHAR(25),
Dept_No Number(3),
Branch_Location VARCHAR(25),
DOB DATE,
Phone Number(10));

Example 2:

CREATE TABLE Employee ( Emp_No CHAR (5),
Emp_Name VARCHAR(25),
Dept_No Number(3) DEFAULT 1,
DOB DATE,
Branch_Location VARCHAR(25) DEFAULT ‘CHENNAI’,
Phone Number(10));

First example creates a table named Employee with 6 columns with the type and size specified.

Second example creates a table named Employee with 6 columns with the type and size specified. Along with that, it also defines default value 1 for Dept_No attribute, and default value ‘CHENNAI’ for Branch_Location attribute. If for any records, the value of Dept_No and Branch_Location attributes are not provided, the system according to the definition inserts 1 and ‘CHENNAI’ into the table automatically in the respective columns.

Column-Level-Constraint:

Column-level-constraint is a constraint (condition) which refers to a single column in which it is defined. Except CHECK constraints, all the other constraints need not specify a column in its definition. The following are the Column-level constraints in SQL;

NOT NULL – represents that the column must have a value. We cannot leave it blank or we cannot use NULL value.
PRIMARY KEY – represent the key for the whole relation (table) to uniquely identify a record among other records of the same table. PRIMARY KEY means both UNIQUE and NOT NULL.
UNIQUE – represents that the value for this column must be unique, and should not be repeated in any other records of the same table.
FOREIGN KEY – represents that the value accepted for the column must be available already in the column from which it is referred.
CHECK – represents that the search condition must be satisfied while entering a value into this column.

Can we create a complete table at this level using Column Definitions?

Yes. At this level we could use some integrity constraints at the column level.
Example 3:

CREATE TABLE Employee ( Emp_No CHAR (5) PRIMARY KEY,
Emp_Name VARCHAR(25) NOT NULL,
Dept_No Number(3) CHECK (Dept_No<>0),
DOB DATE,
Branch_Location VARCHAR(25) DEFAULT ‘CHENNAI’,
Phone Number(10) UNIQUE);

Example 4:

CREATE TABLE Employee ( Emp_No CHAR (5) PRIMARY KEY,
Emp_Name VARCHAR(25) NOT NULL,
Dept_No Number(3) CHECK (Dept_No>0) REFERENCES Dept(Deptno),
DOB DATE,
Branch_Location VARCHAR(25) DEFAULT ‘CHENNAI’,
Phone Number(10) UNIQUE);

CREATE TABLE Dept(Deptno Number(3) PRIMARY KEY,
Dept_Name VARCHAR(20),
Address VARCHAR(20));

Example 3 creates table Employee with all the column level constraints. As you see in the example, the constraints are defined at column level for every single column if necessary. The column Dept_No uses one CHECK constraint. Through this CHECK constraint, the design of the table enforces that the value for this column must be greater than 0. The REFERENCES clause of column Dept_No checks the value you insert into the column against the values stored in the Deptno column of table Dept which is referred by Employee. Here, it is mandatory to have the columns the foreign key and referred attributes with same data types and sizes.

Some of the CHECK constraints;
Emp_no CHAR(5) CHECK (Emp_No LIKE ‘E___’) – this says every value of Emp_no must start with the letter ‘E’ and have exactly 4 characters.
Branch_Location VARCHAR(25) CHECK (Branch_Location IN (‘Chennai’, ‘Mumbai’, ‘New Delhi’)) – this means, the table can permit one of three values given in the CHECK condition for Branch_Location attribute.

Table Level Constraint:

Table level constraint does everything as we did with column level constraint with groups of many attributes. That is the integrity constraints applied collectively on set of attributes. They refer to one or more columns in the table. They specify the names of the attributes to which they apply explicitly. Table-level CHECK constraints can refer to 0 or more columns in the table.

Example:

CREATE TABLE Employee ( Emp_No CHAR (5),
Emp_Name VARCHAR(25) NOT NULL,
Dept_No Number(3),
DOB DATE,
Branch_Location VARCHAR(25) DEFAULT ‘CHENNAI’,
Phone Number(10) UNIQUE,
CONSTRAINT pk PRIMARY KEY (Emp_No),
CONSTRAINT ck1 CHECK (Dept_No>0),
CONSTRAINT fk1 FOREIGN KEY (Dept_No) REFERENCES Dept(Deptno));

CREATE TABLE Dept(Deptno Number(3) PRIMARY KEY,
Dept_Name VARCHAR(20),
Address VARCHAR(20));



In example 5, everything we have done in Example 4 is repeated with a slight modification. In example 5, we have done it in table level. Hence, the column names of all the constraints are repeated inside pair of brackets. And the new thing here is that the keyword CONSTRAINT which is primarily used to name different constraints which in future might be used for Altering the table by modifying or deleting the existing constraints. We need not give a constraint name for PRIMARY KEY, but for all the others use of constraint name is recommended. The very reason is a table can have more CHECK constraints and more FOREIGN KEY constraints.




Basic Data Types:

CHAR(size) - To store alpha-numeric character values. The size represents maximum number of characters. This type stores static values. That is, it occupies the memory as per the size even though the value is of smaller size.
Example – Emp_No CHAR(5) – can store employee numbers of maximum 5 characters. Even, if you store smaller value, say for example, 3 characters, the system reserves and occupies 5 characters in memory (2 gets wasted).
VARCHAR(size) – To store alpha-numeric character values. This type is dynamic, i.e, occupies the memory according to these size.
Example – Emp_Name VARCHAR(25) – can store employee names of maximum length 25. Smaller names will occupy lesser memory.
DATE – To store the date values.
Example – DOB DATE – stores the date of birth in date format (default format – ‘dd-mon-yyyy’)
NUMBER(digits) – To store numbers of size digits.
Example – Phone NUMBER(10) – stores phone number of size 10
NUMBER(digits, decimal point digits) – To store numbers with decimal point values.
Example – Salary NUMBER(6,2) –stores the salary value in the format 1000.50. Here, 6 mean the total length and 2 means the decimal point digits out of 6 digits.

Featured Content

Multiple choice questions in Natural Language Processing Home

MCQ in Natural Language Processing, Quiz questions with answers in NLP, Top interview questions in NLP with answers Multiple Choice Que...

All time most popular contents