Upload
rushdi-shams
View
93
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Citation preview
Rushdi Shams, Dept of CSE, KUET 1
Database Database SystemsSystems
TablesTables
Data TypesData Types
Version 1.0Version 1.0
2Rushdi Shams, Dept of CSE, KUET
Basics of TableBasics of Table
In a data model, table is a bucket In a data model, table is a bucket where you pour data.where you pour data.
Data in a specific table is associated Data in a specific table is associated with all other items in that table.with all other items in that table.
In a table, there are basically 3 In a table, there are basically 3 things-things-
1.1. Rows / Records / TuplesRows / Records / Tuples
2.2. Columns / Attributes / FieldsColumns / Attributes / Fields
3.3. DataData
3Rushdi Shams, Dept of CSE, KUET
Basics of TableBasics of Table
In the table, the column goes on in a In the table, the column goes on in a horizontal fashion.horizontal fashion.
ISBN, AUTHOR, PUBLISHER, TITLE, ISBN, AUTHOR, PUBLISHER, TITLE, GENRE, PRINTED are the column names GENRE, PRINTED are the column names for this tablefor this table
4Rushdi Shams, Dept of CSE, KUET
Basics of TableBasics of Table
In the table, the row goes on in a vertical In the table, the row goes on in a vertical fashion.fashion.
Every row in this table has data for ISBN, Every row in this table has data for ISBN, AUTHOR, PUBLISHER, TITLE, GENRE, AUTHOR, PUBLISHER, TITLE, GENRE, PRINTEDPRINTED
5Rushdi Shams, Dept of CSE, KUET
Basic of TableBasic of Table
Books contain relatively disorganized dataBooks contain relatively disorganized data Organize information using modelOrganize information using model Resulting in a neatly structured set of Resulting in a neatly structured set of
rows and columnsrows and columns
6Rushdi Shams, Dept of CSE, KUET
Relation / TableRelation / Table In relational data model, the table is also called In relational data model, the table is also called
relation. There are set of rules that are applied relation. There are set of rules that are applied on the relations. You must have to know them.on the relations. You must have to know them.
1.1. A database contains many relations. Every A database contains many relations. Every relations in a database must have distinct namesrelations in a database must have distinct names
2.2. Every column in a relation must have distinct Every column in a relation must have distinct namesnames
3.3. Every entries in a column must be in the same Every entries in a column must be in the same domaindomain
4.4. The ordering of columns in a relation is The ordering of columns in a relation is insignificantinsignificant
7Rushdi Shams, Dept of CSE, KUET
Relation / Table Relation / Table (continued)(continued)
5.5. Duplicate rows are not allowed in a Duplicate rows are not allowed in a relationrelation
6.6. The ordering of rows is insignificantThe ordering of rows is insignificant
7.7. Multiple values are not allowed in Multiple values are not allowed in the cells of a relationthe cells of a relation
8.8. 2 rows in a relation may contain the 2 rows in a relation may contain the same value for 1 columns but not in same value for 1 columns but not in all (deviation of 5)all (deviation of 5)
8Rushdi Shams, Dept of CSE, KUET
Characteristics of a Characteristics of a TableTable
9Rushdi Shams, Dept of CSE, KUET
TerminologyTerminology
The number of rows in a table is The number of rows in a table is called Cardinalitycalled Cardinality
The number of columns in a table is The number of columns in a table is called Degreecalled Degree
10Rushdi Shams, Dept of CSE, KUET
Creating TableCreating Table This SQL command This SQL command
will create a table will create a table named Studentnamed Student
Student will have 6 Student will have 6 columns-columns-
1.1. IDID
2.2. NameName
3.3. AddressAddress
4.4. Date_of_BirthDate_of_Birth
5.5. YearYear
6.6. SemesterSemester
CREATE TABLE Student CREATE TABLE Student ((IDID VARCHAR(10)VARCHAR(10)
NOT NULL,NOT NULL,
NameName VARCHAR(20),VARCHAR(20),
AddressAddress VARCHAR(20),VARCHAR(20),
Date_of_BirthDate_of_Birth DATE,DATE,
YearYear INTEGER(1),INTEGER(1),
SemesterSemesterINTEGER(1)INTEGER(1)
););
11Rushdi Shams, Dept of CSE, KUET
Creating Table Creating Table (continued)(continued)
Each of the column Each of the column has a definite Data has a definite Data Type. Different Type. Different vendors of DBMS vendors of DBMS have different data have different data types.types.
In choosing them In choosing them appropriately, you appropriately, you will have to go will have to go through their through their documentations! documentations! They are huge!They are huge!
CREATE TABLE Student CREATE TABLE Student ((IDID VARCHAR(10)VARCHAR(10)
NOT NULL,NOT NULL,
NameName VARCHAR(20),VARCHAR(20),
AddressAddressVARCHAR(20),VARCHAR(20),
Date_of_BirthDate_of_Birth DATE,DATE,
YearYear INTEGER(1),INTEGER(1),
SemesterSemesterINTEGER(1)INTEGER(1)
););
12Rushdi Shams, Dept of CSE, KUET
Creating Table Creating Table (continued)(continued)
Each column has Each column has some predefined some predefined length on data length on data types.types.
As in ID, the ID of As in ID, the ID of a student can be a student can be with maximum with maximum length of 10 and length of 10 and so on.so on.
CREATE TABLE Student CREATE TABLE Student ((IDID VARCHAR(10)VARCHAR(10)
NOT NULL,NOT NULL,
NameName VARCHAR(20),VARCHAR(20),
AddressAddressVARCHAR(20),VARCHAR(20),
Date_of_BirthDate_of_Birth DATE,DATE,
YearYear INTEGER(1),INTEGER(1),
SemesterSemesterINTEGER(1)INTEGER(1)
););
13Rushdi Shams, Dept of CSE, KUET
Creating Table Creating Table (continued)(continued)
The NOT NULL field is The NOT NULL field is the last thing to know the last thing to know here. If you think that here. If you think that any of your columns of any of your columns of your table MUST NOT your table MUST NOT be NULL, then you be NULL, then you specify it. specify it.
If you don’t specify it, If you don’t specify it, during inserting data, during inserting data, anyone may leave that anyone may leave that empty. But if you empty. But if you specify NOT NULL, specify NOT NULL, then the column MUST then the column MUST have a value for each of have a value for each of its row.its row.
CREATE TABLE Student CREATE TABLE Student ((IDID VARCHAR(10)VARCHAR(10)
NOT NULL,NOT NULL,NameName VARCHAR(20),VARCHAR(20),AddressAddress VARCHAR(20),VARCHAR(20),Date_of_BirthDate_of_Birth DATE,DATE,YearYear INTEGER(1),INTEGER(1),SemesterSemester
INTEGER(1)INTEGER(1)););
14Rushdi Shams, Dept of CSE, KUET
Data TypesData Types
Now, we will plunge into data types Now, we will plunge into data types for more.for more.
Though vendor to vendor data Though vendor to vendor data types vary, they have some generic types vary, they have some generic types.types.
1.1. Simple data typesSimple data types
2.2. Complex data typesComplex data types
3.3. Specialized data typesSpecialized data types
15Rushdi Shams, Dept of CSE, KUET
Simple Data TypesSimple Data Types
As the name mentions, they are As the name mentions, they are really simply applied on the data in a really simply applied on the data in a tabletable
Strings:Strings: a string is a sequence of a string is a sequence of characters. It can be fixed length characters. It can be fixed length strings and variable length strings.strings and variable length strings.
16Rushdi Shams, Dept of CSE, KUET
Simple Data Types Simple Data Types (continued)(continued)
If you limit the number of characters in this If you limit the number of characters in this kind of strings, then it takes exactly the kind of strings, then it takes exactly the same number of characters. CHAR is this same number of characters. CHAR is this type of data type. If you say CHAR (3) and type of data type. If you say CHAR (3) and put NY there, then it will store put NY there, then it will store NY<SPACE> in that column NY<SPACE> in that column
In contrast, a variable length string allows In contrast, a variable length string allows you to put data up to a value defined by the you to put data up to a value defined by the user. VARCHAR(10) means this column user. VARCHAR(10) means this column allows you to put 10 characters at mostallows you to put 10 characters at mostTEXT (N) is another variable length string!TEXT (N) is another variable length string!
17Rushdi Shams, Dept of CSE, KUET
Simple Data Types Simple Data Types (continued)(continued)
18Rushdi Shams, Dept of CSE, KUET
Simple Data Types Simple Data Types (continued)(continued)
Numbers:Numbers: numerical data types. numerical data types. Should I say more about them? Should I say more about them?
There are loads of numerical data There are loads of numerical data types-types-
1.1. SMALLINTSMALLINT2.2. INTEGERINTEGER3.3. LONGLONG4.4. FLOATFLOAT5.5. NUMBERNUMBER
19Rushdi Shams, Dept of CSE, KUET
Simple Data Types Simple Data Types (continued)(continued)
Fixed length decimals:Fixed length decimals:
Sometimes, you will need to fix Sometimes, you will need to fix the number of characters in a the number of characters in a decimal like DECIMAL (5,2). decimal like DECIMAL (5,2). From vendor to vendor this may From vendor to vendor this may differ. differ.
With this, it can be like-With this, it can be like-
1.1. It will allow 12345.67 or It will allow 12345.67 or
2.2. It will allow 123.45It will allow 123.45
20Rushdi Shams, Dept of CSE, KUET
Simple Data Types Simple Data Types (continued)(continued)
Date and time:Date and time:
Date and time may be formatted Date and time may be formatted as follows-as follows-
1.1. dd/mm/yyyydd/mm/yyyy
2.2. timstamptimstamp
21Rushdi Shams, Dept of CSE, KUET
Simple Data Types Simple Data Types (continued)(continued)
22Rushdi Shams, Dept of CSE, KUET
About other data typesAbout other data types
Complex and specialized data types- Complex and specialized data types- I will try to discuss them when time I will try to discuss them when time will seem appropriate for me! will seem appropriate for me!
23Rushdi Shams, Dept of CSE, KUET
Data DictionaryData Dictionary
It is called database designer’s It is called database designer’s databasedatabase
During creation of database tables, During creation of database tables, often engineers keep track of their often engineers keep track of their tables by data dictionarytables by data dictionary
It is not mandatory but recommendedIt is not mandatory but recommended Contains every possible information Contains every possible information
about the tables in a databaseabout the tables in a database
24Rushdi Shams, Dept of CSE, KUET
Data Dictionary Data Dictionary (continued)(continued)
25Rushdi Shams, Dept of CSE, KUET
ReferenceReference
Beginning Database Design by Gavin Beginning Database Design by Gavin Powell, Wrox Publications, 2005Powell, Wrox Publications, 2005
Database Systems: Design, Database Systems: Design, Implementation & Management Implementation & Management by Rob & Coronel, 6by Rob & Coronel, 6thth Edition Edition