0% found this document useful (0 votes)
7 views7 pages

MySql CreateDB Tables

The document provides comprehensive notes on creating databases and tables in MySQL, including commands for creating and deleting databases, naming conventions, and data types. It covers the structure of tables, primary and foreign keys, and methods for inserting data into tables. Additionally, it includes exercises for practical application of the concepts discussed.

Uploaded by

ssantos.1717
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views7 pages

MySql CreateDB Tables

The document provides comprehensive notes on creating databases and tables in MySQL, including commands for creating and deleting databases, naming conventions, and data types. It covers the structure of tables, primary and foreign keys, and methods for inserting data into tables. Additionally, it includes exercises for practical application of the concepts discussed.

Uploaded by

ssantos.1717
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Page 1

MySql Notes. Creating Database and Tables..................................................................................2


Create database............................................................................................................................2
Exercise 1: Create a database..................................................................................................2
Create tables.................................................................................................................................3
Data Types in MySQL.............................................................................................................3
Commands for creating tables, primary, composite and foreign keys....................................6
Exercise 2:...............................................................................................................................7
Insert data.....................................................................................................................................7
Page 2

MySql Notes. Creating Database and Tables


Create database

View databases: Show databases;


- to see the existing mysql databases

Select a database: USE name of the database;

Naming conventions for the MySql database, tables and columns

 Limit to 64 characters
 Use underscore (_) to separate words. Do Not use space
 End primary keys and foreign keys with id
 List primary key first in the table, then foreign keys
 Field names must be descriptive
 Field names must be unique across every table, except for keys
 Remain consistent with your naming conventions

Exercise 1: Create a database

Creating Database: CREATE DATABASE name of the database;


E.g. CREATE DATABASE MyDVDStore;

Select a database: USE name of the database;


E.g. USE MyDVDStore;

Deleting Database: DROP DATABASE name of the database;


Page 3

Create tables

To create a table, we have to define the name of the table, the columns and the constraints
(primary key, foreign key, and indexes), the data type, type of table (MyISAM, BDB, InnoDB,
Memory).
If you do not specify table type, MySQL will automatically create the table in default
type: MyISAM. Later we will learn about types of tables in MySQL.

Data Types in MySQL

Three Primary categories

TEXT TYPES

CHAR( ) A fixed section from 0 to 255 characters long.

VARCHAR( ) A variable section from 0 to 255 characters long.

TEXT A string with a maximum length of 65535 characters (64 KB).

BLOB A string /Binary data with a maximum length of 65535 characters (64
KB).

TINYTEXT A string with a maximum length of 255 characters (355 bytes).


TINYBLOB

MEDIUMTEXT A string with a maximum length of 16777215 characters (16 MB).

MEDIUMBLOB A string / Binary Data with a maximum length of 16777215 characters (16
MB).

LONGTEXT A string with a maximum length of 4294967295 characters (4 GB).

LONGBLOB A string / Binary data with a maximum length of 4294967295 characters


(4 GB).

The ( ) brackets allow you to enter a maximum number of characters will be used in the column.
E.g. VARCHAR(20)

CHAR and VARCHAR are the most widely used types. CHAR is a fixed length string and is
mainly used when the data is not going to vary much in it's length. VARCHAR is a variable
length string and is mainly used when the data may vary in length.
Page 4

CHAR may be faster for the database to process considering the fields stay the same length down
the column. VARCHAR may be a bit slower as it calculates each field down the column, but it
saves on memory space. Which one to ultimately use is up to you.

Using both a CHAR and VARCHAR option in the same table, MySQL will automatically
change the CHAR into VARCHAR for compatibility reasons.

BLOB stands for Binary Large OBject. Both TEXT and BLOB are variable length types that
store large amounts of data. They are similar to a larger version of VARCHAR. These types can
store a large piece of data information (images and sound files), but they are also processed
much slower.
e.g. Photo BLOB, (you assume that all photos stored in the database will be less than 64 KB)

NUMBER TYPES

TINYINT( ) -128 to 127 normal


0 to 255 UNSIGNED.

SMALLINT( ) -32768 to 32767 normal


0 to 65535 UNSIGNED.

MEDIUMINT( ) -8388608 to 8388607 normal


0 to 16777215 UNSIGNED.

INT( ) -2147483648 to 2147483647 normal


0 to 4294967295 UNSIGNED.

BIGINT( ) -9223372036854775808 to 9223372036854775807 normal


0 to 18446744073709551615 UNSIGNED.

FLOAT A small number with a floating decimal point. Uses 4 bytes of storage.

DOUBLE( , ) A large number with a floating decimal point. Uses 8 bytes of storage.
Supports greater precision or more decimal places than Float.
Double and float are approximate data types – values are rounded
according to the length of the column and decimal specifications.

DECIMAL( , ) A DOUBLE stored as a string , allowing for a fixed decimal point with
(length) and (Decimals) specifications.
Only used when you need storage accurate information – Financial info.
Price Decimal (7,2) – Price Decimal (length =7, Decimals = 2).
Weight Float (8,4) – Weight Float (length = 8, decimals = 4)

Number types can be UNSIGNED – limiting the column to positive numbers or zero, or be
defined as ZEROFILL – any extra room will be padded with zeros. ZEROFILLS are also
automatically UNSIGNED.
Page 5

The integer types have an extra option called UNSIGNED. Normally, the integer goes from a
negative to positive value. Using an UNSIGNED command will move that range up so it starts at
zero instead of a negative number.

DATE TYPES

DATE - YYYY-MM-DD.

TIME - HH:MM:SS.

DATETIME - YYYY-MM-DD HH:MM:SS.

TIMESTAMP- YYYY-MM-DD HH:MM:SS.

To record transactions that occur at a particular time, use TIMESTAMP.


E.g. Order_Date TIMESTAMP – automatically records the current date and time when a
particular record was inserted or updated.

MISC TYPES
 ENUM ( ) Short for ENUMERATION. This column can only store one of the values
that are declared in the specified list contained in the ( ) brackets.
 ENUM('y','n') You can list up to 65535 values in an ENUM list. If a value is
inserted that is not in the list, a blank value will be inserted.

 SET similar to ENUM except each column may have more than one of the specified
possible values.
 SET may contain up to 64 list items and can store more than one choice.

 NOT NULL: NULL value means that the field has no value or it is unknown. NOT
NULL enforces that a value has to be added to the column.

 DEFAULT: When a large portion of your database will have the same value, preset a
default value. Example: gender ENUM (‘M’, ‘F’) DEFAULT ‘F’

 AUTO_INCREMENT: sets the value to the next logical value in the series
Page 6

Commands for creating tables, primary, composite and foreign keys

Use name of the database;

Create table tablename (


column1name definition of column1,
column2name definition of column2,
column3name definition of column3,
…….
Primary key (column1name),
index (ColumnName) );

Example: CREATE TABLE invoices (


invoice_id SMALLINT(4) UNSIGNED NOT NULL AUTO_INCREMENT,
client_id SMALLINT(3) UNSIGNED,
invoice_date DATE NOT NULL,
invoice_amount DECIMAL(10,2) UNSIGNED NOT NULL,
invoice_description TINYTEXT,
PRIMARY KEY (invoice_id),
INDEX (invoice_date))
Engine = InnoDB;

For composite keys: PRIMARY KEY (Order_ID, Model_ID)

Foreign Keys: You have to enforce referential integrity.

FOREIGN KEY (Model_ID) REFERENCES Models (Model_ID)


ON DELETE CASCADE ON UPDATE CASCADE

View tables: Show tables;

View columns: Show columns from name of the database;


Page 7

The DVD rentals database keeps a track of inventory of DVDs, provides information about
DVDs, records rental transactions and stores the names customers and employees

Exercise 2:

See create_tables_dvdrentals1.txt

Insert data

Two methods for inserting data in the MySql database

Method 1: Insert into tablename (columnname1, columnname2, columnname3,….)


Values (‘value 1’, ‘value 2’, ‘value 3’,……);

With this method you will populate only columns that matter. For any column not
given a value, the value will be treated as NULL or the established default value). This method
is preferable for populating the database.

Method 2: Insert into tablename Values (‘value 1’, ‘value 2’, NULL, ‘value 4’, ……);

With this method, you must specify a value for every column, even if any column
has a null or default value. You will cause an error if the values do not match the number of
columns.

String values must be entered with quotation marks and numbers must be entered without
any quotation marks.

Exercise: Insert data in the tables

See insertdata_dvdrentals

You might also like