Share
Explore

F23 October 11: Building the College Enrollment System with MySQL Workbench

Note: Make sure your MySQL SERVER is running and start MySQL Workbench

How to start mysql server

Starting the MySQL server varies depending on the operating system you're using.
Here's how to start the MySQL server for different OS:

1. For Linux:

If you've installed MySQL using a package manager or it's provided by your distribution, it's likely controlled using systemd or init.d.
Using systemd (most modern distributions):
bashCopy code
sudo systemctl start mysqld

Using init.d (older distributions):
bashCopy code
sudo service mysql start

2. For macOS: The best way on Mac is to be logged in as an Administrator.

If you've installed MySQL using the official MySQL package:
bashCopy code
sudo /usr/local/mysql/support-files/mysql.server start

If you've installed MySQL using Homebrew:
bashCopy code
brew services start mysql

3. For Windows:

If you've installed MySQL as a Windows service, which is the default for the MySQL Installer for Windows:
Open the Run dialog box (Windows + R).
Type services.msc and press Enter.
In the services window, locate MySQL. Right-click on it and choose Start.
If you didn't install MySQL as a service, you'd typically start it using the command prompt:
Navigate to your MySQL bin directory, e.g., C:\Program Files\MySQL\MySQL Server 5.7\bin\
Run mysqld.exe

Note:

Always ensure you have the necessary privileges to start services on your computer, especially when using administrative commands like sudo on Linux/macOS.

error no database selected - how to select database in mysql

The error message "no database selected" arises when you're trying to create or modify a table without specifying in which database the operation should take place. To resolve this, you need to select a database before executing your CREATE TABLE command.
Here's how you can select a database in MySQL:

1. Using the USE Command:

Before running your CREATE TABLE statement, you'll want to issue a command like this:
sqlCopy code
USE your_database_name;

Replace your_database_name with the name of your actual database. After running the USE command, you can then execute your CREATE TABLE statement, and it will be executed in the context of the selected database.

2. In MySQL Workbench:

If you're using MySQL Workbench, you can also select a database by:
Opening your connection to the MySQL Server.
In the Navigator panel on the left, you will see a list of schemas (another name for databases in this context).
Double-click the desired database/schema, and it will become the default for any subsequent SQL operations in that tab.
Once you've selected the database using one of the methods above, you can then execute the CREATE TABLE statement:
sqlCopy code
CREATE TABLE students (
studentid varchar(10),
lastname varchar(50),
firstname varchar(50),
address varchar(255),
city varchar(255)
);

Always ensure that you're working in the correct database, especially if you have multiple databases on your MySQL server.

The error message "no database selected" arises when you're trying to create or modify a table without specifying in which database the operation should take place. To resolve this, you need to select a database before executing your CREATE TABLE command.

Here's how you can select a database in MySQL:

1. Using the USE Command:

Before running your CREATE TABLE statement, you'll want to issue a command like this:
sqlCopy code
USE your_database_name;

Replace your_database_name with the name of your actual database. After running the USE command, you can then execute your CREATE TABLE statement, and it will be executed in the context of the selected database.

2. In MySQL Workbench:

If you're using MySQL Workbench, you can also select a database by:
Opening your connection to the MySQL Server.
In the Navigator panel on the left, you will see a list of schemas (another name for databases in this context).
Double-click the desired database/schema, and it will become the default for any subsequent SQL operations in that tab.
Once you've selected the database using one of the methods above, you can then execute the CREATE TABLE statement:
sqlCopy code
CREATE TABLE students (
studentid varchar(10),
lastname varchar(50),
firstname varchar(50),
address varchar(255),
city varchar(255)
);

Always ensure that you're working in the correct database, especially if you have multiple databases on your MySQL server.

show me the sql to list all databases on my server

To list all the databases on your MySQL server, you can use the SHOW DATABASES command:
sqlCopy code
SHOW DATABASES;

Executing this SQL command will return a list of all the databases available on your server. If you're using a GUI tool like MySQL Workbench, PhpMyAdmin, or any other database management tool, you can simply run the above SQL command in the provided SQL/query editor. If you're using the MySQL command-line client, you can enter the command directly after logging in.

show me the DDL sql to create a database for the college enrollment system

The SQL Data Definition Language (DDL) command to create a database is quite straightforward. For your college enrollment system, you might use something like the following:
sqlCopy code
CREATE DATABASE collegeEnrollment;

Executing this command would create a new database named "CollegeEnrollment" on your MySQL server.
Once the database is created, you'd then switch to that database using the USE command (as described in previous answers) to create tables and establish other database structures.
Remember, the naming conventions, character sets, and collations can be adjusted based on your specific needs. If you need to specify a particular character set and collation, the command could be expanded as follows:
sqlCopy code
CREATE DATABASE CollegeEnrollment CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

This would create the database using the utf8mb4 character set and the utf8mb4_unicode_ci collation, which are generally good choices for supporting a wide range of characters and providing case-insensitive comparison.

show me the ddl to create table students
Here's a DDL (Data Definition Language) SQL command to create a basic students table. This table will include an ID for each student (which will serve as the primary key), as well as some general information fields:
sqlCopy code
CREATE TABLE students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
date_of_birth DATE,
email VARCHAR(255) UNIQUE,
phone_number VARCHAR(15),
address VARCHAR(255),
city VARCHAR(100),
state VARCHAR(50),
postal_code VARCHAR(10),
country VARCHAR(100) DEFAULT 'USA'
);

Breakdown:
student_id: A unique identifier for each student. It auto-increases as new students are added.
first_name and last_name: The student's first and last names.
date_of_birth: The student's date of birth.
email: The student's email. The UNIQUE constraint ensures that no two students can have the same email.
phone_number: The student's phone number.
address, city, state, postal_code, and country: Address information for the student. The country field defaults to 'USA' but can be changed as needed.
Of course, this is a basic schema. Depending on the specific requirements of the college enrollment system, you might need to add more fields or even additional related tables (e.g., for courses, enrollments, etc.).

show me the sql to show all tables in a database

To list all the tables in a specific database, you can use the SHOW TABLES command. However, before doing that, you should specify which database you're referring to using the USE command.
Here's the sequence:
sqlCopy code
USE your_database_name;
SHOW TABLES;

Replace your_database_name with the actual name of your database.
After executing the USE command, the SHOW TABLES command will list all tables present in the selected database. If you're using a tool like MySQL Workbench or the MySQL command-line client, you can simply run the above SQL commands in the provided SQL/query editor.
To list all the fields (columns) in a specific table, along with their data types and other properties, you can use the DESCRIBE command (often abbreviated as DESC).
Here's how to use it:
sqlCopy code
DESCRIBE your_table_name;

Replace your_table_name with the actual name of your table.
For example, if you have a table named students and you want to see its fields, you would use:
sqlCopy code
DESCRIBE students;

This will provide an overview of each column in the table, including the data type, whether NULL values are allowed, key information, default values, and extra attributes.

Lab Workbook to create a basic college enrollment database using MySQL Workbench. (Assignment 2)

The Setup: We have setup the database, and the tables.
Next Steps:
CRUD into the data for students, classes, and enrollments.
Write the SQL statement for the Schedule.
image.png

College Enrollment Database: A MySQL Workbench Tutorial

Objective:

Upon completion, you will understand the fundamentals of creating a relational database for college enrollment using MySQL Workbench. We will have tables for students, classes, and enrollments.

Prerequisites:

MySQL Workbench installed and a connection to MySQL Server established.

1. Database Creation

1.1 Open MySQL Workbench and connect to your local MySQL server using the previously established connection.
1.2 Create the Database:
In the Navigator pane, find and click on the “Create a new schema” icon (a cylinder with a + sign).
Name your schema college_enrollment.
Click "Apply" then "Finish".

2. Creating the students Table

2.1 In the Navigator pane, click on the college_enrollment schema to set it as the default.
2.2 Open a new SQL tab and enter the following SQL to create the students table:
sqlCopy code
CREATE TABLE students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
date_of_birth DATE,
email VARCHAR(100) UNIQUE
);

2.3 Execute the SQL by clicking the lightning bolt icon.

3. Creating the classes Table

3.1 In the same SQL tab, enter:
sqlCopy code
CREATE TABLE classes (
class_id INT AUTO_INCREMENT PRIMARY KEY,
class_name VARCHAR(255) NOT NULL,
instructor_name VARCHAR(100)
);

3.2 Execute the SQL.

4. Creating the enrollments Table

4.1 Enter the following SQL:
sqlCopy code
CREATE TABLE enrollments (
enrollment_id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
class_id INT,
enrollment_date DATE DEFAULT CURRENT_DATE,
grade VARCHAR(2),
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (class_id) REFERENCES classes(class_id)
);

4.2 Execute the SQL.

5. Inserting Sample Data

5.1 Students:

sqlCopy code
INSERT INTO students (first_name, last_name, date_of_birth, email)
VALUES
('John', 'Doe', '2000-05-15', 'john.doe@college.edu'),
('Jane', 'Smith', '1999-08-22', 'jane.smith@college.edu');

Execute the SQL.

5.2 Classes:

sqlCopy code
INSERT INTO classes (class_name, instructor_name)
VALUES
('Database Design', 'Prof. Adams'),
('Intro to Programming', 'Dr. Brown');

Execute the SQL.

5.3 Enrollments:

sqlCopy code
INSERT INTO enrollments (student_id, class_id, grade)
VALUES
(1, 1, 'A'),
(1, 2, 'B'),
(2, 2, 'A');

Execute the SQL.

6. Basic Queries

To ensure our data is correctly inputted, let's execute some basic queries.
6.1 To retrieve all students:
sqlCopy code
SELECT * FROM students;

6.2 To retrieve all classes:
sqlCopy code
SELECT * FROM classes;

6.3 To retrieve all enrollments:
sqlCopy code
SELECT * FROM enrollments;

7. Joins

A basic introduction to see which students are enrolled in which classes.
7.1 Execute:
sqlCopy code
SELECT students.first_name, students.last_name, classes.class_name, enrollments.grade
FROM students
JOIN enrollments ON students.student_id = enrollments.student_id
JOIN classes ON enrollments.class_id = classes.class_id;

This query will show a list of students alongside the classes they're enrolled in and their grades.
Conclusion:
You've now designed a simple college enrollment database. This structure can be expanded with more tables, fields, and relationships as required. As you progress, consider adding more functionalities, like a search function, updating grades, adding more details to students or classes, etc. Practice frequently to solidify your understanding and to explore the possibilities of database design.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.