登录
$ mysql -u root -p # 使用root用户登录mysql;-u,user登录用户,-p,password密码
Enter password:****** # 登录后进入终端
数据库级别
创建数据库
create database database_name; /*创建数据库,database_name为数据库名*/
删除数据库
drop database database_name; /*删除数据库,database_name为数据库名*/
选择数据库
use database_name; /*选择/切换数据库,database_name为数据库名*/
数据表级别
创建表
语法:create table table_name(column_name column_type);
create table table_name /*创建表*/
create table table_name(column_name column_type)
create table if not exist 'purchaser_order'
(purchaser_order_id INT NOT NULL AUTO_INCREMENT, /* AUTO_INCREMENT定义列为自增的属性,一般用于主键,数值会自动加1 */
puchaser_name VARCHAR(10) NOT NULL, /* 如果你不想字段为 NULL 可以设置字段的属性为 NOT NULL, 在操作数据库时如果输入该字段的数据为NULL ,就会报错 */
total_price FLOAT NOT NULL,
submit_date DATE
PRIMARY KEY(purchaser_order_id) /* PRIMARY KEY关键字用于定义列为主键,主键必须包含唯一的值,主键列不能包含 NULL 值*/)
删除表
语法:drop table table_name
drop table table_name /*删除表*/
修改表
修改表名
语法:alter table table_name rename to new_table_name
alter table testalter_tbl rename to alter_tbl; /*修改表名,新表名为alter_tbl*/
添加字段:
语法:alter table table_name add column_name
,未指定位置默人添加至末尾
alter table testalter_tbl add i INT; /*新增表字段,i字段会自动添加到数据表字段的末尾*/
alter table testalter_tbl add i INT first; /*指定位置新增表字段,设为第一列*/
alter table testalter_tbl add i INT after column_name; /*指定位置新增表字段,设为column_name之后*/
删除字段:
语法:alter table table_name drop column_name
alter table testalter_tbl drop column_name; /*删除表字段,数据表中只剩余一个字段则无法使用DROP来删除字段*/
修改字段:
-
modify
修改字段数据类型或设置字段默认值:alter table table_name modify column_name data_type
alter table testalter_tbl modify c CHAR(10); /*修改表字段数据类型,把字段c的类型从CHAR(1)改为CHAR(10)*/ alter table testalter_tbl modify c INT NOT NULL default 100; /*修改表字段数据类型并设置not null,默认值为100*/
-
change
修改字段数据类型或重命名或设置字段默认值:alter table table_name modify column_name new_column_name data_type
在 CHANGE 关键字之后,紧跟着的是你要修改的字段名,然后指定新字段的类型及名称。
alter table testalter_tbl change i j BIGINT; /*修改字段名,把字段i重命名为j,其数据类型修改为BIGINT*/ alter table testalter_tbl change i i INT; /*修改表字段数据类型,把字段i的类型改为INT*/ alter table testalter_tbl change i j INT NOT NULL default 100; /*修改表字段数据类型并设置not null,默认值为100*/
-
alter
修改字段默认值:alter table table_name alter column_name set[drop] default [default_value]
alter table testalter_tbl alter i set default 1000; /*修改字段默认值,默认值为1000*/ alter table testalter_tbl alter i drop default; /*删除字段默认值*/
创建索引:
语法:create index
create index indexName on mytable(username(length)); /*创建普通索引*/
alter table tableName add index indexName(columnName); /*通过修改表结构添加索引,alter table add index*/
create table mytable(ID INT NOT NULL,username VARCHAR(16) NOT NULL,INDEX [indexName] (username(length)); /*创建表时直接指定索引*/)
create unique index indexName on mytable(username(length)); /*创建唯一索引*/
alter table mytable add unique [indexName] (username(length)) /*通过修改表结构添加唯一索引,alter table add unique*/
create table mytable(ID INT NOT NULL,username VARCHAR(16) NOT NULL,unique [indexName] (username(length)); /*创建表时直接指定唯一索引*/)
删除索引:
语法:drop index [indexName] on table_name
drop index [indexName] on mytable; /*删除mytable的索引*/
alter table testalter_tbl drop index c; /*通过修改表结构删除索引,alter table drop index*/
显示索引:
语法:show index from table_name
show index from table_name;\G /*通过添加 \G 来格式化输出信息*/
添加主键:
语法:alter table table_name add primary key (column_name)
alter table testalter_tbl add primary key (i); /*添加主键索引*/
删除主键:
语法:alter table table_name drop primary key
alter table testalter_tbl drop PRIMARY KEY;
记录级别
插入数据
insert into table_name (column, column1, column2, column3, ...) values (value, value1, value2, value3 ...)
更新数据
update table_name set field1=new-value1, field2=new-value2 [WHERE Clause] /*更新记得带好条件,否则更新全表*/
删除数据
delete from tableName where someColumn = someValue /*删除记得带好条件,否则清空整表*/
查询数据
查询全表/全表查询字段值
select * from table_name;/*对全表搜索*/
select column_name(s) from table_name;/*对全表搜索字段值*/
组合条件查询
and
or
select [*] from [TableName] where [condition1]; /*按条件对全表搜索记录*/
select [*] from [TableName] where [condition1] [and [or]] [condition2]... /*and,or组合条件查询*/
count
计数
count(*)
count(column_name)
count(distinct column)
select count(*) from table_name; /*COUNT(*) 函数返回表中的记录数*/
select count(column_name) from table_name [where condition]; /*COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入)*/
select count(distinct column_name) from table_name; /*COUNT(DISTINCT column_name) 函数返回指定列的不同值的数目*/
查找属性值在某一集合内的记录 in
select column1, column2....columnN from table_name where column_name in (value-1, value-2,...value-N);
查找指定模式的记录 like
select column1, column2....columnN from table_name where column_name like { PATTERN };
分组查询 group by [having]
指定
GROUP BY column_name
时, 属于同一个column_name
的一组数据将只能返回一行值
HAVING
是分组(GROUP BY
)后的筛选条件,分组后的数据组内再筛选,使用聚合函数对属于一组的数据操作WHERE
则是在分组前筛选
select sum(column_name) from table_name where CONDITION group by column_name having (arithematic function condition);
select student_name,SUM(score) from table_name group by student_name having SUM(score) > 800; /*查找全科总分数大于800的学生姓名及总分数*/
按属性排序 order by
按属性进行排序,默认排序方式为升序。单一列属性排序;多属性排序,前面列属性值一样,按后面属性值排序
ASC
升序,默认升序DESC
降序
select column_name from table_name order by column_name ASC[DESC];
select * from students where sex='F' order by score DESC; /*按分数降序对男学生排序*/
查找属性唯一不同的值 distinct
select distinct column1, column2....columnN from table_name;
查找前几条或中间几条记录 limit
语法:select column1,column2,...from table limit offset [count]
- 给定一个参数:表示返回最大的记录行数目,
limit n
等价于limit 0,n
- 给定两个参数:第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目
select * from table limit 5; /*检索前 5 个记录行*/
select * from table limit 5,10; /*检索记录行 6-15*/
select * from table limit 95,-1; /*检索记录行 96-last*/
连接
使用 MySQL 的 JOIN 在两个或多个表中查询数据。
mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| w3cschool_author | w3cschool_count |
+-----------------+----------------+
| mahran | 20 |
| mahnaz | NULL |
| Jen | NULL |
| Gill | 20 |
| John Poul | 1 |
| Sanjay | 1 |
+-----------------+----------------+
6 rows in set (0.01 sec)
mysql> SELECT * from w3cschool_tbl;
+-------------+----------------+-----------------+-----------------+
| w3cschool_id | w3cschool_title | w3cschool_author | submission_date |
+-------------+----------------+-----------------+-----------------+
| 1 | Learn PHP | John Poul | 2007-05-24 |
| 2 | Learn MySQL | Abdul S | 2007-05-24 |
| 3 | JAVA Tutorial | Sanjay | 2007-05-06 |
+-------------+----------------+-----------------+-----------------+
3 rows in set (0.00 sec)
内连接
[inner] join on
:组合两个表中的记录,返回关联字段相符的记录,也就是返回两个表的交集部分。
语法:select \* from a_table a inner join b_table b on a.a_id = b.b_id;
mysql> SELECT a.w3cschool_id, a.w3cschool_author, b.w3cschool_count FROM w3cschool_tbl a INNER JOIN tcount_tbl b ON a.w3cschool_author = b.w3cschool_author;
+-----------+---------------+--------------+
| w3cschool_id | w3cschool_author | w3cschool_count |
+-----------+---------------+--------------+
| 1 | John Poul | 1 |
| 3 | Sanjay | 1 |
+-----------+---------------+--------------+
2 rows in set (0.00 sec)
等价于:
mysql> SELECT a.w3cschool_id, a.w3cschool_author, b.w3cschool_count FROM w3cschool_tbl a, tcount_tbl b WHERE a.w3cschool_author = b.w3cschool_author;
+-------------+-----------------+----------------+
| w3cschool_id | w3cschool_author | w3cschool_count |
+-------------+-----------------+----------------+
| 1 | John Poul | 1 |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
2 rows in set (0.01 sec)
左连接
left [outer] join on
:左表(a_table)的记录将会全部表示出来,而右表(b_table)只会显示符合搜索条件的记录。右表记录不足的地方均为NULL
。
语法:SELECT * FROM a_table a left join b_table b ON a.a_id = b.b_id;
mysql> SELECT a.w3cschool_id, a.w3cschool_author, b.w3cschool_count FROM w3cschool_tbl a LEFT JOIN tcount_tbl b ON a.w3cschool_author = b.w3cschool_author;
+-------------+-----------------+----------------+
| w3cschool_id | w3cschool_author | w3cschool_count |
+-------------+-----------------+----------------+
| 1 | John Poul | 1 |
| 2 | Abdul S | NULL |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)
/*Person表 PersonId 是主键*/
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
/*Address表 AddressId 是主键*/
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
/*编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:FirstName, LastName, City, State*/
mysql> select p.FirstName, p.LastName, a.City, a.State from Person p left join Address a on p.PersonId =a.PersonId
右连接
right [outer] join on
:与左(外)连接相反,右(外)连接,左表(a_table)只会显示符合搜索条件的记录,而右表(b_table)的记录将会全部表示出来。左表记录不足的地方均为NULL。
语法:SELECT * FROM a_table a right outer join b_table b on a.a_id = b.b_id;
mysql> SELECT b.w3cschool_id, b.w3cschool_author, a.w3cschool_count FROM tcount_tbl a RIGHT JOIN w3cschool_tbl b ON a.w3cschool_author = b.w3cschool_author;
+-------------+-----------------+----------------+
| w3cschool_id | w3cschool_author | w3cschool_count |
+-------------+-----------------+----------------+
| 1 | John Poul | 1 |
| 2 | Abdul S | NULL |
| 3 | Sanjay | 1 |
+-------------+-----------------+----------------+
3 rows in set (0.02 sec)
全连接
union
/union all
注意事项:
- 通过
union
连接的SQL它们分别单独取出的列数必须相同;- 不要求合并的表列名称相同时,以第一个sql 表列名为准;
- 使用
union
时,完全相等的行,将会被合并,由于合并比较耗时,一般不直接使用`union 进行合并,而是通常采用union all 进行合并;- 被
union
连接的sql子句,单个子句中不用写order by
,因为不会有排序的效果。但可以对最终的结果集进行排序;union
会自动将完全重复的数据去除掉,union all
会保留那些重复的数据;
语法:(select colum1,colum2...columN from tableA ) union [all] (select colum1,colum2...columN from tableB )
mysql> SELECT * FROM a;
+-----------------+----------------+
| id | num |
+-----------------+----------------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
+-----------------+----------------+
4 rows in set (0.01 sec)
mysql> SELECT * from b;
+-----------------+----------------+
| id | num |
+-----------------+----------------+
| b | 5 |
| c | 15 |
| d | 20 |
| e | 99 |
+-----------------+----------------+
4 rows in set (0.01 sec)
mysql>(SELECT id,num FROM A) UNION (SELECT id,num FROM B); /*union会自动将完全重复的数据去除掉,a、b表中"c"的值都为15,所以只显示一行。*/
+-----------------+----------------+
| id | num |
+-----------------+----------------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
| b | 5 |
| d | 20 |
| e | 99 |
+-----------------+----------------+
7 rows in set (0.03 sec)
mysql>(SELECT id,num FROM A) UNION ALL(SELECT id,num FROM B); /*union all会保留完全重复的数据*/
+-----------------+----------------+
| id | num |
+-----------------+----------------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
| b | 5 |
| c | 15 |
| d | 20 |
| e | 99 |
+-----------------+----------------+
8 rows in set (0.03 sec)
mysql>(SELECT id,num FROM A) UNION ALL(SELECT id,num FROM B) ORDER BY num; /*对结果集进行排序*/
+-----------------+----------------+
| id | num |
+-----------------+----------------+
| a | 5 |
| b | 5 |
| b | 10 |
| d | 10 |
| c | 15 |
| c | 15 |
| d | 20 |
| e | 99 |
+-----------------+----------------+
8 rows in set (0.03 sec)