一、数据库的基本操作 数据库是用来存储表的地方,在数据库开始创建的时候,要先创建好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中的所有数据库了:
这时可以看到除了我们创建的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运行之后可以看到每一列的具体信息:
3.删除TABLE
删除和前面的数据库的删除基本相似,使用[DROP TABLE XXX]语法删除表:
DROP TABLE Persons
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

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 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 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 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.

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.

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.

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.

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


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools