Home  >  Article  >  Database  >  What is the relationship between tables and databases

What is the relationship between tables and databases

青灯夜游
青灯夜游Original
2021-06-24 17:34:4920494browse

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.

What is the relationship between tables and databases

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.

  • 722e3d59fd24604761db25f00f9b264f: Specifies the name of the table to be created, given after CREATE TABLE, and must comply with the identifier naming rules. The table name is specified as db_name.tbl_name to create the table in a specific database. It can be created this way regardless of whether there is a current database. db-name can be omitted when creating a table in the current database. If you use quoted distinguished names, the database and table names should be quoted separately. For example, 'mydb'.'mytbl' is legal, but 'mydb.mytbl' is not.

  • f52e9980a8ff3ab048a5a8e987465670: Table creation definition, consisting of column name (col_name), column definition (column_definition), and possible null value specifications, integrity constraints, or table indexes composition.

  • 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 38a5ee44d6da035557fbae6f8a86bf1c 30690cee1a11d5dfbdced93b89f678ee

  • ##| CHANGE COLUMN d5a108d244c95dfc1f93d06c76e4a268 de792e514673c1077a498e8f6aba0987 5be509b072e8a62aa604bb901d2ff1a6

  • | ALTER COLUMN 895a828a5767d0ff83c19d6a93c7da7c { SET DEFAULT 47292eb776f1048422ffa13791917062 | DROP DEFAULT }

  • | MODIFY COLUMN 895a828a5767d0ff83c19d6a93c7da7c d676500b8b078a240ad7c6c4738fa17c

  • | DROP COLUMN 895a828a5767d0ff83c19d6a93c7da7c

  • ##| RENAME TO 482b976f750ee915a344ae526ea63ab1
  • | CHARACTER SET 762c527a1437241e2d9df6561b2a9a7e
  • ##| COLLATE 7986e35b966c8be17ac1da11246800d7 }

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn