Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Chapters.

Similar presentations


Presentation on theme: "Database Chapters."— Presentation transcript:

1 Database Chapters

2 Data Types Data type selection is usually dictated by nature of data and by intended use MySQL Supported data type categories: Numeric Date and time String Database Systems, 10th Edition

3 MySQL Numeric Data Types from http://www. tutorialspoint
INT - You can specify a width of up to 11 digits. FLOAT(M,D) - A floating-point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). This is not required and will default to 10,2, where 2 is the number of decimals and 10 is the total number of digits (including decimals). Decimal precision can go to 24 places for a FLOAT. DOUBLE(M,D) - A double precision floating-point number that cannot be unsigned. You can define the display length (M) and the number of decimals (D). This is not required and will default to 16,4, where 4 is the number of decimals. Decimal precision can go to 53 places for a DOUBLE. REAL is a synonym for DOUBLE. DECIMAL(M,D) - An unpacked floating-point number that cannot be unsigned. In unpacked decimals, each decimal corresponds to one byte. Defining the display length (M) and the number of decimals (D) is required. NUMERIC is a synonym for DECIMAL. Database Systems, 10th Edition

4 MySQL String Data Types from http://www. tutorialspoint
CHAR(M) - A fixed-length string between 1 and 255 characters in length (for example CHAR(5)), right-padded with spaces to the specified length when stored. Defining a length is not required, but the default is 1. VARCHAR(M) - A variable-length string between 1 and 255 characters in length; for example VARCHAR(25). You must define a length when creating a VARCHAR field. BLOB or TEXT - A field with a maximum length of characters. BLOBs are "Binary Large Objects" and are used to store large amounts of binary data, such as images or other types of files. Fields defined as TEXT also hold large amounts of data; the difference between the two is that sorts and comparisons on stored data are case sensitive on BLOBs and are not case sensitive in TEXT fields. You do not specify a length with BLOB or TEXT. ENUM - An enumeration, which is a fancy term for list. When defining an ENUM, you are creating a list of items from which the value must be selected (or it can be NULL). For example, if you wanted your field to contain "A" or "B" or "C", you would define your ENUM as ENUM ('A', 'B', 'C') and only those values (or NULL) could ever populate that field. Database Systems, 10th Edition

5 MySQL Date & Time Data Types from http://www. tutorialspoint
DATE - A date in YYYY-MM-DD format, between and For example, December 30th, 1973 would be stored as DATETIME - A date and time combination in YYYY-MM-DD HH:MM:SS format, between :00:00 and :59:59. For example, 3:30 in the afternoon on December 30th, 1973 would be stored as :30:00. TIMESTAMP - A timestamp between midnight, January 1, 1970 and sometime in This looks like the previous DATETIME format, only without the hyphens between numbers; 3:30 in the afternoon on December 30th, 1973 would be stored as ( YYYYMMDDHHMMSS ). TIME - Stores the time in HH:MM:SS format. YEAR(M) - Stores a year in 2-digit or 4-digit format. If the length is specified as 2 (for example YEAR(2)), YEAR can be 1970 to 2069 (70 to 69). If the length is specified as 4, YEAR can be 1901 to The default length is 4. Database Systems, 10th Edition

6 Creating Table Structures
Use one line per column (attribute) definition Use spaces to line up attribute characteristics and constraints Table and attribute names are case sensitive NOT NULL specification UNIQUE specification Database Systems, 10th Edition

7 SQL Constraints AUTO_INCREMENT constraint NOT NULL constraint
Applied to INT: assign the next available integer NOT NULL constraint Ensures that column does not accept nulls UNIQUE constraint Ensures that all values in column are unique Database Systems, 10th Edition

8 Creating tables General format: CREATE TABLE tablename ( column1 datatype, column2 datatype, PRIMARY KEY (…..), FOREIGN KEY (column1 REFERENCES tablename) );

9 Example 1 CREATE TABLE CUSTOMER ( CUS_CODE INT PRIMARY KEY, CUS_LNAME VARCHAR(15) NOT NULL, CUS_FNAME VARCHAR(15) NOT NULL, CUS_INITIAL CHAR(1), CUS_AREACODE CHAR(3) DEFAULT '615' NOT NULL CHECK(CUS_AREACODE IN ('615','713','931')), CUS_PHONE CHAR(8) NOT NULL, CUS_BALANCE DECIMAL(9,2) DEFAULT 0.00 ); Database Systems, 10th Edition

10 Example 2 CREATE TABLE SF_users ( user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, username VARCHAR(30) NOT NULL, pass CHAR(40) NOT NULL, first_name VARCHAR(20) NOT NULL, last_name VARCHAR(40) NOT NULL, VARCHAR(60) NOT NULL, registration_date DATETIME NOT NULL, PRIMARY KEY (user_id), UNIQUE (username), UNIQUE ( ), INDEX login (pass, ) ) ENGINE = INNODB; Notes: Default storage type is MyISAM: Doesn't support foreign key constraints, transactions, or row-level locking. Does support FULLTEXT indexes and searches Use storage type INNODB unless you need transactions or FULLTEXT.

11 Inserting data INSERT INTO SF_users (username, pass, first_name, last_name, , registration_date) VALUES ('troutster', 'Larry', 'Ullman', NOW()), ('funny man', 'David', 'Brent', NOW()), ('Gareth', 'Gareth', 'Keenan', NOW()); SHA1() : Secure Hash Algorithm 1 – calculates a hash value for the string and returns a 40-character hex value. It provides one-way encryption. **This is not a recommended function for real-world encryption but is simple enough for learning purposes only. Adding, say, the address to the password improves the password seed by making it longer. Now() is a handy function for getting the current date and time from the server.


Download ppt "Database Chapters."

Similar presentations


Ads by Google