The relationship between tables and databases is: a database can contain multiple tables. A table (TABLE) is an object used to store data in a database. It is a collection of structured data and is the basis of the entire database system. Each database is composed of several data tables; in other words, there is no data table without a data table. Unable to store data in database.
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
Table (TABLE) is an object used to store data in a database. It is a collection of structured data and is the foundation of the entire database system. Tables are database objects that contain all the data in a database.
Data tables are an important part of the database. Each database is composed of several data tables. In other words, data cannot be stored in a database without data tables.
For example, create an empty folder on your computer. If you want to store "Hello C Language Chinese Network" in the folder, you must write it in a Word document, Notepad or other document that can store text. middle. The empty folder here is equivalent to the database, and the document storing text is equivalent to the data table.
A table is defined as a collection of columns. Similar to a spreadsheet, data is organized in a table format in rows and columns. Each column in a table is designed to store some type of information (such as a date, name, dollar amount, or number). There are several controls on tables (constraints, rules, default values, and custom user data types) to ensure data validity.
MySQL creates a data table (CREATE TABLE statement)
After creating the database, the next step is to create a data table in the database. The so-called creating a data table refers to creating a new table in the already created database.
The process of creating a data table is the process of specifying the attributes of the data columns, and it is also the process of implementing data integrity (including entity integrity, referential integrity and domain integrity) constraints.
Basic syntax
In MySQL, you can use the CREATE TABLE statement to create a table. The syntax format is:
CREATE TABLE <表名> ([表定义选项])[表选项][分区选项];
Among them, the format of [Table Definition Options] is:
<列名1> <类型1> [,…] <列名n> <类型n>
The CREATE TABLE command syntax is more, which mainly consists of table creation definition (create-definition), Composed of table options (table-options) and partition options (partition-options).
Here we first describe a simple example of creating a new table, and then focus on some main syntax knowledge points in the CREATE TABLE command.
The main syntax and usage instructions of the CREATE TABLE statement are as follows:
CREATE TABLE: used to create a table with a given name, you must have table CREATE permissions.
By default, the table is created in the current database. If the table already exists, there is no current database, or the database does not exist, an error will occur.
MySQL ALTER TABLE: Modify the data table
The prerequisite for modifying the data table is that it already exists in the database The table. Modifying a table refers to modifying the structure of an existing data table in the database. The operation of modifying the data table is also essential in database management. It is just like drawing a sketch. If you draw too much, you can erase it with an eraser. If you draw too little, you can add it with a pen.
Not knowing how to modify the data table is equivalent to throwing away and redrawing as long as we make a mistake, which increases unnecessary costs.
You can use the ALTER TABLE statement in MySQL to change the structure of the original table, such as adding or deleting columns, changing the original column type, renaming columns or tables, etc.
The syntax format is as follows:
ALTER TABLE <表名> [修改选项]
The syntax format for modifying the option is as follows:
{ ADD COLUMN
MySQL删除数据表(DORP TABLE语句)
在 MySQL 数据库中,对于不再需要的数据表,我们可以将其从数据库中删除。
在删除表的同时,表的结构和表中所有的数据都会被删除,因此在删除数据表之前最好先备份,以免造成无法挽回的损失。
下面我们来了解一下 MySQL 数据库中数据表的删除方法。
基本语法
使用 DROP TABLE 语句可以删除一个或多个数据表,语法格式如下:
DROP TABLE [IF EXISTS] 表名1 [ ,表名2, 表名3 ...]
对语法格式的说明如下:
表名1, 表名2, 表名3 ...表示要被删除的数据表的名称。DROP TABLE 可以同时删除多个表,只要将表名依次写在后面,相互之间用逗号隔开即可。
IF EXISTS 用于在删除数据表之前判断该表是否存在。如果不加 IF EXISTS,当数据表不存在时 MySQL 将提示错误,中断 SQL 语句的执行;加上 IF EXISTS 后,当数据表不存在时 SQL 语句可以顺利执行,但是会发出警告(warning)。
(推荐教程:mysql视频教程)
The above is the detailed content of What is the relationship between tables and databases. For more information, please follow other related articles on the PHP Chinese website!