The TABLE will take you through the steps to create a new database with MySQL/phpMyAdmin, then create a table. Once we have a table created, we will add some data.
If you want to download a completed version of the SQL script for The TABLE, you can find it here.
Creating a Database. With either MAMP or XAMPP started up, open phpMyAdmin. There are a couple of ways to do this:
Click the "Databases" tab or find the "New" link on the left-side column and click it to show the form for creating a new database.
Type in a name for the database. We will use "ampjam_db" for the exercise. Adopt a convention for using upper/lowercase characters to reduce errors/frustration later. We will use all lower-case here.
Creating a table. Once you click the "Create" button, you will see a form for creating the first table.
Give the table a name. We will use "list", since that's what we will use the table for. The form gives you the option to say how many columns you want, but the default 4-column starting point is fine. You can add or delete columns later on. Click the "Go" button to proceed.
Define the columns. Now you must define the columns for your table.
Almost always you will use the first column for an I.D., a unique identifier for the row of data. Name the first column "id" and give it a data type of "INT". Because you want each record to have a unique identifier, check the "A_I" (AUTO INCREMENT) checkbox for the ID row. When you check the A_I box, you will prompted to add an index. Just click "Go" in the dialog box. That will create a "PRIMARY KEY".
Note: If you forget this step, you will not be able to go back and set the A_I property unless you first create an index (PRIMARY KEY) on this column.
The second column should be named "item". You will place a list item name in this column. Use the dropdown menu for "Type" and select "VARCHAR". You must define the length of a VARCHAR column. Older versions of MySQL were limited to 255 characters for a VARCHAR column, but current versions allow 65,535 characters. Let's use 255.
Add a third column and name it "description". Use the Type dropdown and select "TEXT". TEXT columns do not require that you define the length.
The fourth column will be named "image". We will use it to hold the URL for an image file. Set the type to either VARCHAR or TEXT.
Now that we have our columns defined, click "Save" to save the table.
The STRUCTURE Screen. Once you save the table, you will see the Structure screen. It will show you the definitions for each column, along with links to change, drop (delete), add keys, and indexes (The "More" dropdown).
The lower part of the form will allow you to add columns and create an index if you need to.
Note: There is no "Undo" feature in MySQL, so be careful with commands like "Drop". phpMyAdmin is pretty good about asking you to confirm commands like that, but realize that a mistake can be permanent.
Inserting Records. Insert a few records into your list. For our example, lets make a list of dinosaurs, starting with Tyrannosaurus Rex.
Click the "Insert" tab to see a form that allows you to create a record. You will only fill in the fields in the "Value" column. The first field is "id" and you should leave it blank. The AUTO INCREMENT feature will create a number for this field, incrementing it each time. Then, for the "item", enter the name of your first item. Likewise, enter the "description" and the filename for an "image" if you have one. (You can download a set of images used for the exercise here.)
Click the "Go" button to insert your record into the table. Repeat this process for a few addtional items. You can enter two records at a time on the Insert screen if you wish.
Browse. Click the "Browse" tab to see your records. You can see the id's that were auto-inserted and edit any fields you want to change by double-clicking on the fields. There are also other options for editing, copying, and deleting records. The "Export" option will export the table as an SQL script.
Completed Project. Contratulations! You have now created a table and put some records into it! Next, you will build a simple full-stack project to draw a list in the browser using your table's data.
If you had problems, or you want to use the same data I used in the example, here is completed version of the SQL script used for this project:
(list.sql)-- phpMyAdmin SQL Dump -- version 4.8.3 -- https://www.phpmyadmin.net/ -- -- Host: localhost:8889 -- Generation Time: Apr 01, 2020 at 04:21 PM -- Server version: 5.7.23 -- PHP Version: 7.2.10 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Database: `ampjam_db` -- -- -------------------------------------------------------- -- -- Table structure for table `list` -- CREATE TABLE `list` ( `id` int(11) NOT NULL, `item` varchar(255) NOT NULL, `description` text NOT NULL, `image` varchar(255) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- Dumping data for table `list` -- INSERT INTO `list` (`id`, `item`, `description`, `image`) VALUES (1, 'Tyrannosaurus Rex', 'Tyrannosaurus, meaning \"tyrant lizard\", from the Ancient Greek tyrannos, \"tyrant\", and sauros, \"lizard\" is a genus of coelurosaurian theropod dinosaur. It also had a tremendous bite force, the strongest of any Dinosaur and living terrestrial animal. Its bite force reached up to 12,800 pounds (roughly 5805 Kilograms).', 't_rex.jpg'), (2, 'Spinosaurus', 'Spinosaurus is a genus of theropod dinosaur that lived in what now is North Africa, during the lower Albian to lower Cenomanian stages of the Cretaceous period, about 112 to 97 million years ago.', 'spinosaurus.jpg'), (3, 'Velociraptor', 'Velociraptor is a genus of dromaeosaurid theropod dinosaur that lived approximately 75 to 71 million years ago during the later part of the Cretaceous Period.', 'velociraptor.jpg'), (4, 'Triceratops', 'Triceratops is a genus of herbivorous ceratopsid dinosaur that first appeared during the late Maastrichtian stage of the late Cretaceous period, about 68 million years ago in what is now North America.', 'triceratops.jpg'), (5, 'Stegosaurus', 'Stegosaurus is a type of armored dinosaur. Their fossil bones have been found in rocks dated to the Late Jurassic period, between 155 and 150 million years ago, in the western United States and Portugal.', 'stegasaurus.jpg'), (6, 'Parasaurolophus', 'Parasaurolophus is a genus of ornithopod dinosaur that lived in what is now North America during the Late Cretaceous Period, about 76.5-74.5 million years ago.', 'parasaurolophus.jpg'), (7, 'Ankylosaurus', 'Ankylosaurus is a genus of armored dinosaur. Fossils of Ankylosaurus have been found in geological formations dating to the very end of the Cretaceous Period, between about 68-66 million years ago, in western North America, making it among the last of the non-avian dinosaurs.', 'ankylosaurus.jpg'), (8, 'Brachiosaurus', 'Brachiosaurus is a genus of sauropod dinosaur that lived in North America during the Late Jurassic, about 154-153 million years ago.', 'brachiosaurus.jpg'), (9, 'Carcharodontosaurus', 'Carcharodontosaurus is a genus of carnivorous carcharodontosaurid dinosaurs that existed between 100 and 94 million years ago, during the Cenomanian stages of the mid-Cretaceous Period.', 'carcharodontosaurus.jpg'), (10, 'Allosaurus', 'Allosaurus is a genus of large theropod dinosaur that lived 155 to 150 million years ago during the late Jurassic period.', 'allosaurus.jpg'); -- -- Indexes for dumped tables -- -- -- Indexes for table `list` -- ALTER TABLE `list` ADD PRIMARY KEY (`id`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `list` -- ALTER TABLE `list` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=11; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
(list.sql)INSERT INTO `list` (`id`, `item`, `description`, `image`) VALUES (1, 'Tyrannosaurus Rex', 'Tyrannosaurus, meaning \"tyrant lizard\", from the Ancient Greek tyrannos, \"tyrant\", and sauros, \"lizard\" is a genus of coelurosaurian theropod dinosaur. It also had a tremendous bite force, the strongest of any Dinosaur and living terrestrial animal. Its bite force reached up to 12,800 pounds (roughly 5805 Kilograms).', 't_rex.jpg'), (2, 'Spinosaurus', 'Spinosaurus is a genus of theropod dinosaur that lived in what now is North Africa, during the lower Albian to lower Cenomanian stages of the Cretaceous period, about 112 to 97 million years ago.', 'spinosaurus.jpg'), (3, 'Velociraptor', 'Velociraptor is a genus of dromaeosaurid theropod dinosaur that lived approximately 75 to 71 million years ago during the later part of the Cretaceous Period.', 'velociraptor.jpg'), (4, 'Triceratops', 'Triceratops is a genus of herbivorous ceratopsid dinosaur that first appeared during the late Maastrichtian stage of the late Cretaceous period, about 68 million years ago in what is now North America.', 'triceratops.jpg'), (5, 'Stegosaurus', 'Stegosaurus is a type of armored dinosaur. Their fossil bones have been found in rocks dated to the Late Jurassic period, between 155 and 150 million years ago, in the western United States and Portugal.', 'stegasaurus.jpg'), (6, 'Parasaurolophus', 'Parasaurolophus is a genus of ornithopod dinosaur that lived in what is now North America during the Late Cretaceous Period, about 76.5-74.5 million years ago.', 'parasaurolophus.jpg'), (7, 'Ankylosaurus', 'Ankylosaurus is a genus of armored dinosaur. Fossils of Ankylosaurus have been found in geological formations dating to the very end of the Cretaceous Period, between about 68-66 million years ago, in western North America, making it among the last of the non-avian dinosaurs.', 'ankylosaurus.jpg'), (8, 'Brachiosaurus', 'Brachiosaurus is a genus of sauropod dinosaur that lived in North America during the Late Jurassic, about 154-153 million years ago.', 'brachiosaurus.jpg'), (9, 'Carcharodontosaurus', 'Carcharodontosaurus is a genus of carnivorous carcharodontosaurid dinosaurs that existed between 100 and 94 million years ago, during the Cenomanian stages of the mid-Cretaceous Period.', 'carcharodontosaurus.jpg'), (10, 'Allosaurus', 'Allosaurus is a genus of large theropod dinosaur that lived 155 to 150 million years ago during the late Jurassic period.', 'allosaurus.jpg');
Or, you can download the SQL script here.