MySQL新手入门指南--快速参考
MySQL新手入门指南--快速参考
一、SQL速成以下是一些重要的SQL快速参考,有关SQL的语法和在标准SQL上增加的特性,请查询MySQL手册。
1.创建表
表是数据库的最基本元素之一,表与表之间可以相互独立,也可以相互关联。创建表的基本语法如下:
create table table_name
(column_name datatype {identity |null|not null},
…)
其中参数table_name和column_name必须满足用户数据库中的识别器(identifier)的要求,参数datatype是一个标准的SQL类型或由用户数据库提供的类型。用户要使用non-null从句为各字段输入数据。
create table还有一些其他选项,如创建临时表和使用select子句从其他的表中读取某些字段组成新表等。还有,在创建表是可用PRIMARY KEY、KEY、INDEX等标识符设定某些字段为主键或索引等。
书写上要注意:
在一对圆括号里的列出完整的字段清单。
字段名间用逗号隔开。
字段名间的逗号后要加一个空格。
最后一个字段名后不用逗号。
所有的SQL陈述都以分号";"结束。
例:
mysql> CREATE TABLE test (blob_col BLOB, index(blob_col(10)));
2.创建索引
索引用于对数据库的查询。一般数据库建有多种索引方案,每种方案都精于某一特定的查询类。索引可以加速对数据库的查询过程。创建索引的基本语法如下:
create index index_name
on table_name (col_name[(length)],... )
例:
mysql> CREATE INDEX part_of_name ON customer (name(10));
3.改变表结构
在数据库的使用过程中,有时需要改变它的表结构,包括改变字段名,甚至改变不同数据库字段间的关系。可以实现上述改变的命令是alter,其基本语法如下:
alter table table_name alter_spec [, alter_spec ...]
例:
mysql> ALTER TABLE t1 CHANGE a b INTEGER;
4.删除数据对象
很多数据库是动态使用的,有时可能需要删除某个表或索引。大多数数据库对象可以下面的命令删除:
drop object_name
mysql> DROP TABLE tb1;
5.执行查询
查询是使用最多的SQL命令。查询数据库需要凭借结构、索引和字段类型等因素。大多数数据库含有一个优化器(optimizer),把用户的查询语句转换成可选的形式,以提高查询效率。
值得注意的是MySQL不支持SQL92标准的嵌套的where子句,即它只支持一个where子句。其基本语法如下:
SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [HIGH_PRIORITY] [DISTINCT | DISTINCTROW | ALL]
select_expression,... [INTO {OUTFILE | DUMPFILE} 'file_name' export_options] [FROM table_references [WHERE where_definition] [GROUP BY col_name,...] [HAVING where_definition] [ORDER BY {unsigned_integer | col_name | formula} [ASC | DESC] ,...] [LIMIT [offset,] rows] [PROCEDURE procedure_name] ]
其中where从句是定义选择标准的地方,where_definition可以有不同的格式,但都遵循下面的形式:
字段名操作表达式
字段名操作字段名
在第一种形式下,标准把字段的值与表达式进行比较;在第二种形式下,把两个字段的值进行比较。根据所比较的数据类型,search_condition中的操作可能选以下几种:
= 检查是否相等
!= 检查是否不等
> (或>=) 检查左边值是否大于(或大于等于)右边值
is [not] null 检查左边是否为空值
在这里,可以用通配符_代表任何一个字符,%代表任何字符串。使用关键字、和可以生成复杂的词,它们运行检查时使用布尔表达式的多重标准集。
例:
mysql> select t1.name, t2.salary from employee AS t1, info AS t2 where t1.name = t2.name;
mysql> select college, region, seed from tournament
ORDER BY region, seed;
mysql> select col_name from tbl_name WHERE col_name > 0;
6.修改表中数据
在使用数据库过程中,往往要修改其表中的数据,比如往表中添加新数据,删除表中原有数据,或对表中原有数据进行更改。它们的基本语法如下:
数据添加:
insert [into] table_name [(column(s))]
values (expression(s))
例:
mysql> INSERT INTO tbl_name (col1,col2) VALUES(15,col1*2);
数据删除:
delete from table_name where search_condition
数据更改:
update table_name
set column1=expression1,
column2=expression2,…
where search_condition
7.数据库切换
当存在多个数据库时,可以用下面的命令定义用户想使用的数据库:
use database_name
8.统计函数
SQL有一些统计函数,它们对于生成数据表格很有帮助。下面介绍几个常用的统计函数:
sum (exepression) 计算表达式的和
avg (exepression) 计算表达式的平均值
count (exepression) 对表达式进行简单的计数
count (*) 统计记录数
max (exepression) 求最大值
min (exepression) 求最小值
其中exepression为任何有效的SQL表达式,它可以是一个或多个记录,也可以是别的SQL函数的组合。
二、MySQL使用导引
1.运用MySQL建立新数据库
在shell下运行:
$>mysqladmin create database01
Database "database01" created.
2.启动MySQL
在shell下运行:
$>mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 22 to server version: 3.21. 29a-gamma-debug
Type 'help' for help.
3.更换数据库
mysql>use database01
database changed.
4.创建表
mysql>create table table01 (field01 integer, field02 char(10));
Query OK, 0 rows affected (0.00 sec)
5.列出表清单
mysql>show tables;
Tables in database01
Table01
table02
6.列出表中的字段清单
mysql>show columns from table01;
Field Type Null Key Default Extra
field01 int(11) YES
field02 char(10) YES
7.表的数据填写
插入数据
mysql>insert into table01 (field01, field02) values (1, 'first');
Query OK, 1 row affected (0.00 sec)
8.字段的增加
...一次一个字段
mysql>alter table table01 add column field03 char(20);
Query OK, l row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0
...一次多个字段
mysql>alter table table01 add column field04 date, add column field05 time;
Query OK, l row affected (0.04 sec)
Records: 1 Duplicates: 0 Warnings: 0
注意:每一列都必须以"add column"重新开始。
它运行了吗?让我们看看。
mysql>select * from table01;
field01 field02 field03 field04 field05
1 first NULL NULL NULL
9.多行命令输入
MySQL命令行界面允许把陈述作为一行输入,也可以把它展开为多行输入。这两者之间并没有语法上的区别。使用多行输入,你可以将SQL陈述一步步分解,从而使你更容易理解。
在多行方式下,注释器把每一行都添加到前面的行后,直到你用分号";"来结束这个SQL陈述。一旦键入分号并按回车键,这个陈述即被执行。
下面的例子是同一个严格的SQL陈述的两种输入方法:
单行输入
Mysql>create table table33 (field01 integer, field02 char(30));
多行输入
Mysql>create table table33
->(field01
->integer,
->field02
->char(30));
注意不能将单词断开,如:
正确
mysql>create table table33
->( field01
->integer,
->field02
->char(30));
错误
mysql>create table table33
->( field01 inte
->ger,
->field02
->char(30));
当插入或更改数据时,不能将字段的字符串展开到多行里,否则硬回车将被储存到数据中:
标准操作
mysql>insert into table33 (field02)
->values
->('who thought of foo?');
硬回车储存到数据中
mysql>insert into table33 (field02)
->values
->('who thought
->of foo?');
结果如下:
mysql>select * from table33;
field01 field02
NULL who thought of foo?
NULL who thought
Of foo?
10.表的数据嵌入
mysql>insert into table01 (field01, field02, field03, field04, field05) values
->(2, 'second', 'another', '1999-10-23', '10:30:00');
Query OK, 1 row affected (0.00 sec)
标准日期格式是"yyyy-mm-dd"。
标准时间格式是"hh:mm:ss"。
引号内要求所给的是上述的标准日期和时间格式。
日期也可以"yyyymmdd"形式,时间也可以"hhmmss"形式输入,但其值不需要再加引号。
数字值不需要加引号。这种保存与数据类型无关,这些数据类型都有格式化的专栏来包含(例如:文本,日期,时间,整数等)。
MySQL有一个很有用的命令缓冲区。它保存着你目前已经键入的SQL语句利用它,对于相同的命令,你就不必一遍又一遍地重复输入。下一步我们就来看这样的一个例子。
利用命令缓冲区(及任意的日期和时间格式)增加另一个数据
按两次键盘上的向上箭头键。
回车。
在圆括号内输入新的值,并以分号结尾。
(3, 'a third', 'more', 19991024, 103004);
回车。
新值存在里面了吗?
mysql>select * from table01;
field01 field02 field03 field04 field05
1 first NULL NULL NULL
2 second another 1999-10-23 10:30:00
3 a third more 1999-10-24 10:30:04
11.表的数据更新
一次修改一个字段
再次注意语法。文本需要加引号但数字不要。
mysql>update table01 set field03='new info' where field01=1;
Query OK, 1 row affected (0.00 sec)
一次改变多个字段
记住在每一个更新的字段间用逗号隔开。
mysql>update table01 set field04=19991022, field05=062218 where field01=1;
Query OK, 1 row affected (0.00 sec)
一次更新多个数据
mysql>update table01 set field05=152901 where field04>19990101;
Query OK, 3 rows affected (0.00 sec)
12.删除数据
mysql>delete from table01 where field01=3;
Query OK, 1 row affected (0.00 sec)
13.退出
mysql>quit
Bye
现在你已经了解了一些运行MySQL中的数据库的根本命令。由于MySQL是通过执行SQL调用来操作的,在你的处理过程中需要一个强有力工具的充足的数组。例如,通过联接相关的字段,你可以同时显示几个表中的数据。同样,SQL允许综合显示、更新或者删除多个符合具体标准的数据。如果你还想精通掌握它,下一步就要学习所有SQL的知识。
另外说明一下,MySQL提供了很好的网络操作安全特性。要想了解MySQL的安全及其它特性的详情,请查询MySQL网站:http://www.mysql.com
为了方便大家查询使用MySQL手册, 我们将官方手册(Manaul)做在光盘的/docs/manaul/mysql目录下.大家可以用浏览器浏览查询.

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

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 MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

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.
