1. Basic DATABASE Operations the DATABASE is used to store tables. When creating a DATABASE, you must first create a DATABASE before continuing the operations on the data table. 1. Create DATABASECREATEDATABASE to create a database. The basic syntax is as follows: CREATEDATABASEdatabase_name. Here is an example:
1. Basic DATABASE Operations the DATABASE is used to store tables. When creating a DATABASE, you must first create a DATABASE before continuing the operations on the data table. 1. create database is used to CREATE a DATABASE. The basic syntax is as follows: create database database_name: Let's take an example:
I. Basic Database Operations
A database is used to store tables. When creating a DATABASE, you must first create a DATABASE before continuing to operate the data table.
1. Create a DATABASE
Create database is used to CREATE a DATABASE. The basic syntax is as follows:
CREATE DATABASE database_name
The following is an example:
We want to create a database named "test_db.
We use the following create database statement:
CREATE DATABASE test_django
You can use create table to add database tables.
2. view the DATABASE
After the creation, we can use the following syntax to view the databases in MySQL (note that there is a last s)
SHOW DATABASES
In this way, we can see all the databases in MySQL:
At this time, we can see that in addition to the test_django database we created, there are three databases that come with MySQL:
The first database information_schema: provides a way to access database metadata.
Mysql is the core database of mysql. It is mainly responsible for storing the control and management information required by mysql, such as database users, permission settings, and keywords. It cannot be deleted. If you are not familiar with mysql, we recommend that you do not modify the table information in the database.
The third database is test: A test database created during installation. It is named like a completely empty database and can be deleted without any tables.
3. delete a DATABASE
Delete a database using the DROP Syntax:
DROP DATABASE test
After deletion, we can use show databases to view all the DATABASES. We can see that the test database has been deleted.
4. Use DATABASE
After a database is created, the next task is to use this database. In this database, we can operate related data tables. USE the USE statement to enter the database you created:
USE test_django
Ii. Basic table operations
Indicates the place where data is stored. You can perform related table operations in the database you created earlier.
1. Create a TABLE
Similar to creating a database, we use the [create table xxxx] statement to CREATE a TABLE and declare the content of each column in the TABLE in parentheses.
Take a Persons table as an example (note that you must first enter the USE statement into a DATABASE for Operation ):
CREATE TABLE Persons(Id int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255))
The created table is as follows:
| Id |
LastName |
FirstName |
Address |
City |
|
|
|
|
|
2. display TABLE
The DESCRIBE statement can be used to display the structure of a table. The preceding table Persons is used as an example:
DESCRIBE Persons
After running, you can see the details of each column:
3. delete a TABLE
Deleting a TABLE is similar to deleting a database. You can use the [drop table xxx] syntax to delete a TABLE:
DROP TABLE Persons
4. Modify TABLE
The alter table statement is used to add, modify, or delete columns in an existing TABLE. When creating a table, you can set the column name. We can use ALTER to modify the created table.
To add columns to a table, you can use the following syntax (column_name and ype are column names and column types respectively ):
ALTER TABLE table_nameADD column_name datatype
To delete columns in a table, use the following syntax (column_name is the column name ):
ALTER TABLE table_name DROP COLUMN column_name
To change the data type of a column in a table, use the following syntax (column_name and ype are column names and column types respectively ):
ALTER TABLE table_nameALTER COLUMN column_name datatype