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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

How to execute sql in navicat How to execute sql in navicat Apr 08, 2025 pm 11:42 PM

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

See all articles