Contents:
Creating a Database
Firstly let create a new database by using the following SQL statement through MySQL Command Line Client Tool. In this exercise, our database name will beMycompany. Take note that MySQL is not case sensitive. If you want to retain the case sensitiveness, try using the double quotes. We found that using the double quotes also not working all the times.
|
CREATE DATABASE Mycompany;
Making a Connection
For the next steps, we are using the NetBeans. Launch NetBeans. Click Services tab > expand Database node > expand Drivers node > select MySQL (Connector/J driver) > right click mouse > select Connect Using.
Use the following connection string and key in the username as root and its password. Click OK.
jdbc:mysql://localhost:3306/mycompany
If the connection to the database was established successfully, it will be confirmed as shown in the following Figure. Click OK.
Next, expand the connection node as shown below. You can see the Tables, Views and Procedures folders. These are common database objects that act as containers for other database objects that will be created later.
Creating a Table
Next, execute the following SQL script to create a table. Select the database connection node > right click mouse > select Execute Command to launch SQL query editor.
The following table summarized the table that we want to create. You can create the table shown in the Table using the following SQL script. Copy, paste and execute the following SQL script.
The structure for a Employees table
| ||
Column name
|
Data type
|
Constraints
|
userID
|
INT UNSIGNED
|
NOT NULL PRIMARY KEY
|
name
|
CHAR(10)
|
NOT NULL UNIQUE INDEX
|
lastName
|
VARCHAR(30)
|
-
|
firstName
|
VARCHAR(30)
|
-
|
office
|
CHAR(2)
|
NOT NULL
|
CREATE TABLE IF NOT EXISTS Employees(
empl_id INT,
empl_first_name VARCHAR(32) NOT NULL,
empl_last_name VARCHAR(32) NOT NULL,
empl_addr_street VARCHAR(32) NOT NULL,
empl_addr_city VARCHAR(32) NOT NULL,
empl_addr_zip VARCHAR(10) NOT NULL,
empl_addr_state VARCHAR(2) NOT NULL,
empl_dept_id VARCHAR(10) NOT NULL,
empl_start_date DATE NOT NULL,
empl_position VARCHAR(5) NOT NULL,
empl_birth_date DATE NOT NULL,
PRIMARY KEY (empl_id)
) ENGINE=innodb;
The following Figure shows the SQL script in the SQL query editor/SQL Command editor.
Make sure the connection to the correct database as shown in the following Connection: text field.
Next, click the Run SQL button (
) or Ctrl+Shift+E to execute the SQL script and notice the progress in the Output window at the bottom.
Next, expand the Tables > employees. MySQL is not case sensitive. If you want to retain the case sensitiveness, use double quotes.
The Employees table contains the following columns:
- empl_id: The employee identifier number, which uniquely identifies each employee and is the primary key for these records
- empl_first_name and empl_last_name: The employee's first and last names
- empl_addr_street, empl_addr_city, empl_addr_zip, and empl_addr_state: The employee's complete address
- empl_dept_id: The identifier, a foreign key reference to a row in the Departments table which will be used for the department in which the employee works
- empl_start_date: The employee's start date with the company
- empl_position: The identifier, a foreign key reference to a Positions table record which will be used for the employee's current job or position
- empl_birth_date: The employee's date of birth
Verify our task by issuing the following SQL statement (describe a table).
DESC Employees;
The following Tables list MySQL numerical and character data types information. A complete information can be found in MySQL documentation and you can also download it for offline reading.
Numeric types in MySQL | |
TINYINT
|
Whole 7-bit numbers in the range -128 to 127.
|
SMALLINT
|
Whole 8-bit numbers in the range -32758 to 32,757.
|
MEDIUMINT
|
Whole 16-bit numbers in the range -8,388,608 to 8,388,607.
|
INT
|
Whole 32-bit numbers in the range -2,147,483,548 to 2,147,483,547.
|
BIGINT
|
Whole 64-bit numbers in the range -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
|
DECIMAL(p,s)
|
Decimal values with s as the scale and p as the precision.
|
DOUBLE(p,s)
|
Double-precision values with s as the scale and p as the precision.
|
FLOAT(p)
|
Floating point numbers with a precision of 8 or less.
|
Character types for MySQL | |
CHAR(n)
|
Fixed-length character type that holds exactly n characters. Shorter strings are padded with spaces to n characters.
|
NCHAR(n)
|
Same as CHAR, except for Unicode strings.
|
VARCHAR(n)
|
Variable-length strings that may store up to n characters. Any excess characters are discarded.
|
NVARCHAR(n)
|
Same as VARCHAR, except for Unicode strings.
|
Inserting a Sample Data
Next, let insert a record by running the following SQL statement.
INSERT INTO Employees VALUES('1234','Jodie','Foster','45th Street, Level 3','New York','32000','NY','IT','2008-07-04','Dir','1970-08-05');
Then, verify our task by issuing the following SQL statement.
SELECT * FROM Employees;