You are on page 1of 7

MySQL Basics

Log into the MySQL Shell


1. Start XAMPP Control Panel

2. Open the command prompt

3. In the command prompt, type the following:

➔ C:\Users\ACER PC>cd\

1
MySQL Basics

➔ C:\>cd xampp

➔ C:\xampp>cd mysql

➔ C:\xampp\mysql\bin>

➔ C:\xampp\mysql\bin>mysql -uroot

➔ Note that the command line is now in mysql>.


➔ Your are now ready to create your database.

2
MySQL Basics

Create a Database
Let’s create a movies database.

1. Create a database using the CREATE statement:

CREATE DATABASE movies;

2. Next, verify that the database was created by showing a list of all databases. Use
the SHOW statement:

SHOW DATABASES;

The terminal prints out a list of databases and information about the time it took to
perform the query:

3. Select the database to make changes to it by using the USE statement:

USE movies;

Create a Table
We’ll create a table containing information about two movies:

3
MySQL Basics

Title Genre Director Release year

Joker psychological thriller Todd Phillips 2019

The Empire Strikes Back epic space opera Irvin Kershner 1980

In the process of creating a table, you need to specify the following information:

• Column names – We are creating the title, genre, director, and release year columns for
our table.
• Varchar of the columns containing characters – Specifies the maximum number of
characters stored in the column.
• The integer of the columns containing numbers – Defines numeric variables holding
whole numbers.
• Not null rule – Indicates that each new record must contain information for the column.
• Primary key – Sets a column that defines a record.

1. Create a table using the CREATE command. Using the information from
our movies example, the command is:

CREATE TABLE movies(title VARCHAR(50) NOT NULL,genre VARCHAR(30)


NOT NULL,director VARCHAR(60) NOT NULL,release_year INT NOT NULL
,PRIMARY KEY(title));

2. Verify that the table is created using the DESCRIBE command:

DESCRIBE movies;

The terminal prints out information about the table:

• Field – Indicates column name.


• Type – Specifies data type for the column (varchar for characters, int for
numbers).
• Null – Indicates whether the column can remain with null values.
• Key – Displays the primary column.
• Default – Displays the column’s default value.
4
MySQL Basics

• Extra – Indicates additional information about the columns.

3. Insert movie information in column order – title, genre, director,


and release year. Use the INSERT command:

• INSERT INTO movies VALUE ("Joker", "psychological thriller"


, "Todd Phillips", 2019);


• 4. Repeat the previous step with the second movie. Use the SELECT command to
display the table:

• SELECT * FROM movies;

• The terminal prints out the movie table:

5
MySQL Basics

SQL ALTER TABLE Statement


The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.

The ALTER TABLE statement is also used to add and drop various constraints on an
existing table.

ALTER TABLE - ADD Column


To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype;

The following SQL adds an "Email" column to the "Customers" table:

Example
ALTER TABLE tblmovies
ADD Email varchar(255);

ALTER TABLE - DROP COLUMN


To delete a column in a table, use the following syntax (notice that some
database systems don't allow deleting a column):

ALTER TABLE table_name


DROP COLUMN column_name;

The following SQL deletes the "Email" column from the "Customers" table:

Example
ALTER TABLE tblmovies
DROP COLUMN Email;

6
MySQL Basics

ALTER TABLE - ALTER/MODIFY COLUMN


To change the data type of a column in a table, use the following syntax:

The SQL DROP DATABASE Statement


The DROP DATABASE statement is used to drop an existing SQL database.

Syntax
DROP DATABASE databasename;

The SQL DROP TABLE Statement


The DROP TABLE statement is used to drop an existing table in a database.

Syntax
DROP TABLE table_name;

You might also like