database
1. View all database names
show databases;
As shown in the figure below, infornation_schema, mysql, performance_schema, and sys are all system-level databases that come with mysql.
2. Create a database
create database if not exists databaseName character set = utf8;
As shown in the figure below, after creating the mydb database, use commands to view all databases contained in the current system.
3. View the statement to create the database
show create database databaseName;
As shown in the figure below, view the creation statement named mydb database.
4. Use the database
use databaseName;
As shown in the figure below, use the command named mydb database.
5. View the currently used database
select database();
As shown in the figure below, query the currently used database command.
6. Delete the database
drop database if exists databaseName;
As shown in the figure below, after deleting the student database, only four system-level databases are left.
data sheet
1. Data type
When creating a data table, it is very important to accurately define the data type of the field. MySQL supports a variety of data types, but can be roughly divided into three categories: numeric, date and string types.
Types of | Number of bytes occupied | Description |
---|---|---|
tinyint | 1 | Small integer value |
smallint | 2 | Large integer value |
mediumint | 3 | Large integer value |
int | 4 | Large integer value |
bigint | 8 | Very large integer value |
float | 4 | Single precision floating point value |
double | 8 | Double precision floating point value |
decimal | Max(D+, M+) | Include decimal values, such as amounts |
Types of | Number of bytes occupied | Description |
---|---|---|
date | 3 | YYYY-MM-DD |
time | 3 | HH:MM:SS |
year | 1 | YYYY |
datetime | 8 | YYYY-MM-DD HH:MM:SS |
timestamp | 8 | YYYYMMDDHHMMSS |
Types of | Number of bytes occupied | Description |
---|---|---|
char | 0~255 | Fixed-length field string |
varchar | 0~65535 | Variable length string |
text | 0~65535 | Long text data |
blob | Text data in binary form |
2. View all data table names
show tables;
As shown in the figure below, select the mydb database and view the data tables it contains. Because the data table has not been created, the query result is empty.
3. Create a data table
create [temporary] table [if not exists] tableName
[(definition,…)]
[toptions]
[statement]
- temporary: means to create a temporary table, which will automatically disappear after the current session ends.
- if not exists: Before building the table, first determine whether the table exists, and only create it when the table does not exist.
- definition: The key part of the table building statement, used to define the attributes of each column in the table.
- options: table configuration options, such as the default storage engine and character set of the table.
- statement: Create a table through the select statement.
As shown in the figure below, create the contacts data table, and then query the table name contained in the database, the query result is contacts.
4. View the statement to create a data table
show create table tableName;
As shown in the following figure, view the creation statement named contacts data table.
5. Delete the data table
drop table tableName;
As shown in the figure below, after deleting the data table named contacts, use the show tables command to view the data table owned by the current database, and the result is empty.
6. Add a single column
alter table tableName add column columnName defination first | after columnName;
As shown in the figure below, add a column of age after the name column of the contacts data table, the data type is int.
7. Modify the column name
alter table tableName change older newer defination first | after columnName;
As shown in the figure below, change the phone column name of the contacts data table to mobile and the data type to varchar.
8. Modify the column definition
alter table tableName modify columnName defination first | after columnName;
As shown in the figure below, modify the length of the mobile column of the contacts data table from 11 to 16.
9. Modify the table name
alter table tableName rename newName;
As shown in the figure below, change the name of the contacts data table to contact.
Data integrity
1. Data integrity
Data integrity refers to the data stored in the database, which should maintain consistency and reliability.
The relational model allows three types of data constraints to be defined. They are entity integrity, referential integrity, and user-defined integrity constraints. The first two integrity constraints are automatically supported by the relational database system.
2. Entity integrity An
entity is an object in the real world, and a row in RDBMS represents an entity. Entity integrity is to ensure that every entity can be distinguished. Entity integrity requires that each table has a unique identifier, and the primary key field in each table cannot be empty and cannot be repeated.
3. Referential integrity is
mainly the relationship between tables and can be achieved through foreign keys. Referential integrity requirements do not allow references to non-existent entities in relationships.
4. Domain integrity
Domain integrity is mainly required for column input, which is achieved by limiting the data type, format or value range of the column. Domain integrity is a constraint for a specific relational database. It ensures that invalid values cannot be entered in certain columns in the table. Domain integrity refers to the integrity of the value range of the column, such as data type, format, range of value, whether to allow null values, etc.
5. User-defined integrity is
achieved with the help of stored procedures and triggers. User-defined integrity is a constraint for a specific relational database, which reflects the semantic requirements that the data involved in a specific application must meet.
6. Constraints
- Primary key constraint
alter table tableName add constraint primary key(...); alter table tableName drop primary key;
- Foreign key constraint
alter table tableName add contraint foreign key(...) references tablename; alter table tableName drop foreign key ...;
- Default value constraints
alter table tableName alter colimnname set default value; alter table tableName alter columbbane drop default;
- Unique key constraint
alter table tableName add constraint unique key(...); alter table tableName drop index ...;