Home Database Mysql Tutorial MySQL的语法及其使用指南

MySQL的语法及其使用指南

Jun 07, 2016 pm 06:00 PM
mysql grammar

数据库的选取,创建,丢弃和变更 数据表和索引的创建,变更和丢弃从数据表检索信息

先看看MySQL支持的SQL语句的分类
1, 数据库的选取,创建,丢弃和变更
use
create database
drap database
alter database
2, 数据表和索引的创建,变更和丢弃
create table
drop table
create index
drop index
alter index
3, 从数据表检索信息
select
union
4, 事务处理
begin
commit
rollback
set autocommit
5, 对数据表里面的信息进行修改
delete
insert
load data
replace
update
6, 管理型命令
flush
grant
revoke
一,命名规则
1MySQL允许用在名字中的系统字符.
任何字母数字加上”_” 或 “$”
2名字的长度.
数据库,数据表,数据列,索引等名字最多64个字母
256别名最多256个字母
3名字的限定符
依据不同的上下文,有时需要给某些名字加上某个限制:如数据列的全限定,部分限定,以及无限制.这一点比较容易理解
select * from db_name.tbl_name…
二,MySQL中的大小写问题
关键字和函数名:不区别
数据库名数据表名:根据服务器主机系统而定
数据列名索引名:不区别
别名:区别大小写
一般来说,不管系统是否区分数据库名和数据表名中的字母大小写情况,我们都应该在同一个查询语句里面以前后一致的字母大小写形式来写出这些名字,这是一个非常好的编程习惯。
三,MySQL支持的名种数据表类型详解
1,ISAM数据表
这是3.23版本之前的MySQL支特的唯一一种表类型,目前己经过时,MyIASM处理程库逐步取代了ISAM处理程序,这种老式的表类型己经没有人在用了

2,MyIASM数据表
• 这是目前中MySQL默认使用的数据表类型。其优点是
• 如果主机操作系统支持大尺寸文件,数据表长度就能够很大,就能客纳更多的数据.
• 数据表内容独立于硬件也就是说可以把数据表在机器之间随意拷贝
• 提高了索引方面的功能
• 提供了更好的索引键压缩效果
• auto_incremnet能力加强
• 改进了对数据表的完整性检查机制
• 支持进行fulltext全文本搜索

3,Merge数据表
这是一种把相同结构的MyIASM数据表组织为一个逻辑单元的方法
4,HEAP数据表
这是一种使用内存的数据表,而且各个数据行的长度固定,这两个特性使得这种类型数据表的检索速度非常快,作为一种临时性的数据表,HEAP在某些特定情况下很有用。
5,BDB数据表
这种数据表支持事务处理机制
具有良好的并发性能
6,InnoBDB数据表
这是最近加入MySQL的数据表类型,有许多新的特性
支持事务处理机制
崩溃后能够立刻恢复
支持外键功能,包括级联删除
具有并发功能
7这种数据表在硬盘上的文件存储方式
IASM Frm isd ism
MyISAM Frm myd myi
Merge Frm mrg
Heap Frm
BDB Frm db
InnoBDB frm
8数据表的可移植性
通用方法:吧数据表的内容导出到一个文本文件中,然后拷贝到目的地硬盘上,在用脚本加载到数据库里面,这是首先我们应该掌握的方法。但就文件层次的操作来说,某些数据表是可以单独拷贝的。看表了
ISAM No
MyIASM Yes
BDB No
InnoBDB Yes

四,索引的初步知识
1,索引是加快数据表内容访问性能的基本手段,其基本特性:
为可以索引单独的数据列也可以构造包含多个数据列的复合索引
索引可以包含重复键值
可以为一个数据表建立多个索引
2,不同的数据表有着不同的索引特性使用的时候需要区别对待
3,如何创建索引
①用alter table命令创建索引
②用create index 命令创建索引
③在create table 时创建索引

五,变更数据表的结构
当发现某个数据表的结构己经不能满足我们的使用要求时,就要对其结构进行变更.可能需要这个数据表存放比以前更多的信息;也可能是这个数据表里面的某些信息己经没用;了或许是现有的某个数据列宽度太窄…在这些情况下都要用到alter 语匀
1,重新命名数据表
alter table A rename to B //数据表A改名为B
rename table A to B //数据表A改名为B
rename A toC,B to A,C to A //数据表A和数据表B互换名字
alter table S.A rename to T.A //数据库S里面的表A移动到数据库B里面
rename table S.A to T.A //数据库S里面的表A移动到数据库B里面
2,改变数据列的类型
我们现在要把数据表A里面的一个smallint unsigned类型的数据列I再次改动为 mediumint unsigned 类型
alter table A motify I mediumint unsigned
alter table A change I I mediumint unsigned
注意change子句的特点:不仅能够改变数据列的类型,还能改变数据列的名字。这是modify子句所不能完成的。下面就把这个数据列改名了。
alter table A change I J mediumint unsigned
3,将数据表由可变长度数据行转变成固定长度数据行
有的时候为了提高性能,需要做这样的转变,但有一点需要注意:必须用同一条alter命令来一次改变所有的数据列,不能仅仅改变一个数据列!举例如下:
create table A(name varchar(40),address varchar(80))
我们开始修改命令就应该是:
alter table A modify name char(40),modify address char(80);
4,将数据表由固定长度数据行转变成可变长度数据行
如果觉得空间利用率不高,那就需要再转变回来,这个就很简单了,没有特别要求
alter table A modify name varchar(40)
5,转换数据表类型
我们知道,MySQL数据库存在多种数据表类型,但每一种类型的特性并不相同。
如果你想让你的数据表支持事务处理机制。那就必须把它搞成BDB或innoBDB格式
alter table A type= BDB
alter table A type= InnoBDB
Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

See all articles