25
Rushdi Shams, Dept of CSE, KU ET 1 Database Systems Database Systems Tables Tables Data Types Data Types Version 1.0 Version 1.0

My slide tables & data types

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: My slide  tables & data types

Rushdi Shams, Dept of CSE, KUET 1

Database Database SystemsSystems

TablesTables

Data TypesData Types

Version 1.0Version 1.0

Page 2: My slide  tables & data types

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

Page 3: My slide  tables & data types

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

Page 4: My slide  tables & data types

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

Page 5: My slide  tables & data types

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

Page 6: My slide  tables & data types

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

Page 7: My slide  tables & data types

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)

Page 8: My slide  tables & data types

8Rushdi Shams, Dept of CSE, KUET

Characteristics of a Characteristics of a TableTable

Page 9: My slide  tables & data types

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

Page 10: My slide  tables & data types

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)

););

Page 11: My slide  tables & data types

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)

););

Page 12: My slide  tables & data types

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)

););

Page 13: My slide  tables & data types

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)););

Page 14: My slide  tables & data types

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

Page 15: My slide  tables & 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.

Page 16: My slide  tables & data types

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!

Page 17: My slide  tables & data types

17Rushdi Shams, Dept of CSE, KUET

Simple Data Types Simple Data Types (continued)(continued)

Page 18: My slide  tables & data types

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

Page 19: My slide  tables & data types

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

Page 20: My slide  tables & data types

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

Page 21: My slide  tables & data types

21Rushdi Shams, Dept of CSE, KUET

Simple Data Types Simple Data Types (continued)(continued)

Page 22: My slide  tables & data types

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!

Page 23: My slide  tables & data types

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

Page 24: My slide  tables & data types

24Rushdi Shams, Dept of CSE, KUET

Data Dictionary Data Dictionary (continued)(continued)

Page 25: My slide  tables & data types

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