search
HomeDatabaseMysql Tutorial[SQL]常用的MySQL基本语句(整理中)

一、数据库的基本操作 数据库是用来存储表的地方,在数据库开始创建的时候,要先创建好DATABASE然后才能在里面继续进行数据表的操作。 1.创建DATABASE CREATE DATABASE 用于创建数据库,基本语法如下: CREATE DATABASE database_name 下面来举个例子: 我们

一、数据库的基本操作

数据库是用来存储表的地方,在数据库开始创建的时候,要先创建好DATABASE然后才能在里面继续进行数据表的操作。

1.创建DATABASE

CREATE DATABASE 用于创建数据库,基本语法如下:

CREATE DATABASE database_name
下面来举个例子:

我们希望创建一个名为 "test_db" 的数据库。

我们使用下面的 CREATE DATABASE 语句:

CREATE DATABASE test_django

可以通过 CREATE TABLE 来添加数据库表。


2.查看DATABASE

创建之后我们可以用以下语法来查看我们的MySQL中有那些数据库(注意最后有一个s)

SHOW DATABASES
这样我们就可以看到MySQL中的所有数据库了:

[SQL]常用的MySQL基本语句(整理中)

这时可以看到除了我们创建的test_django数据库之外还有三个MySQL自带的数据库:

第一个数据库information_schema:提供了访问数据库元数据的方式。
第二个数据库mysql:这个是mysql的核心数据库,主要负责存储数据库的用户、权限设置、关键字等mysql自己需要使用的控制和管理信息。不可以删除,如果对mysql不是很了解,建议不要修改这个数据库里面的表信息。
第三个数据库是test:这个是安装时候创建的一个测试数据库,和它的名字一样,是一个完全的空数据库,没有任何表,可以删除。


3.删除DATABASE
删除数据库,使用的是DROP语法:

DROP DATABASE test
删除之后我们可以再使用SHOW DATABASES查看一下所有的数据库,可以看到test数据库已经被删除了。


4.使用DATABASE

在创建完成了一个数据库之后,接下来的任务就是使用这个数据库,在这个数据库中我们可以进行相关的数据表的操作。使用USE语句进入你创建好的数据库:

USE test_django


二、表的基本操作

表示用来存储数据的地方,下面就可以在前面创建好的数据库里面进行相关的表的操作了。

1.创建TABLE

和创建一个数据库相类似的,我们使用[CREATE TABLE xxxx] 语句来创建一个表,在后面的小括号中声明表中的具体每列的内容。

以一个Persons表为例(注意,一定要先输入USE语句进入一个DATABASE进行操作):

CREATE TABLE Persons
(
Id int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

这样创建好的表格如下:
Id LastName FirstName Address City




 


2.展示TABLE

使用DESCRIBE语句可以展示一个表的结构,以前面创建的表Persons为例:

DESCRIBE Persons
运行之后可以看到每一列的具体信息:

[SQL]常用的MySQL基本语句(整理中)

3.删除TABLE

删除和前面的数据库的删除基本相似,使用[DROP TABLE XXX]语法删除表:

DROP TABLE Persons


4.修改TABLE

ALTER TABLE 语句用于在已有的表中添加、修改或删除列。前面在创建表的时候可以设定好里面的列名,我们可以使用ALTER来修改这个已经创建好的表。

如需在表中添加列,可以使用下列语法(其中column_name和datatype分别为列名和列的类型):

ALTER TABLE table_name
ADD column_name datatype

要删除表中的列,请使用下列语法(其中column_name为列名):
ALTER TABLE table_name 
DROP COLUMN column_name

要改变表中列的数据类型,请使用下列语法(其中column_name和datatype分别为列名和列的类型)
ALTER TABLE table_name
ALTER COLUMN column_name datatype


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
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools