Mysql基本语法
时间:2010-09-05 来源:kennychang05
1.创建表
create table table_name
(column_name data无效 {identity |null|not null},
…)
mysql> CREATE TABLE test (blob_col BLOB, index(blob_col(10)));
create index index_name
2.创建索引 on table_name (col_name[(length)],... )
例:
mysql> CREATE INDEX part_of_name ON customer (name(10));
3.改变表结构
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.执行查询
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] ]
= 检查是否相等
!= 检查是否不等 > (或>=) 检查左边值是否大于(或大于等于)右边值
< (或<=) 检查左边值是否小于(或小于等于)右边值
[not] between 检查左边值是否在某个范围内
[not] in 检查左边是否某个特定集的成员
[not] like 检查左边是否为右边的子串
例:
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函数的组合。
9.列出表字段清单
mysql>show tables;
Tables in database01
Table01
table02
show columns from table01;
Field 无效 Null Key Default Extra
field01 int(11) YES
field02 char(10) YES
create table table_name
(column_name data无效 {identity |null|not null},
…)
mysql> CREATE TABLE test (blob_col BLOB, index(blob_col(10)));
create index index_name
2.创建索引 on table_name (col_name[(length)],... )
例:
mysql> CREATE INDEX part_of_name ON customer (name(10));
3.改变表结构
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.执行查询
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] ]
= 检查是否相等
!= 检查是否不等 > (或>=) 检查左边值是否大于(或大于等于)右边值
< (或<=) 检查左边值是否小于(或小于等于)右边值
[not] between 检查左边值是否在某个范围内
[not] in 检查左边是否某个特定集的成员
[not] like 检查左边是否为右边的子串
例:
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函数的组合。
9.列出表字段清单
mysql>show tables;
Tables in database01
Table01
table02
show columns from table01;
Field 无效 Null Key Default Extra
field01 int(11) YES
field02 char(10) YES
相关阅读 更多 +