@tocmysql
mysql -u root -p; //登陆语句 Enter password: //提示输入密码
show databases;
例如:sql
mysql> show databases; +--------------------+ | Database | +--------------------+ | chatgroup | | information_schema | | mysql | | performance_schema | | sys | | user | +--------------------+ 6 rows in set (0.01 sec)
其中,information_schema、mysq、performance_schema、sys为数据库默认的。<font color=red >千万不要删除</font>数据库
chatgroup、user为咱们建立的数据库。markdown
create database "数据库名";
例如:ide
mysql> create database people; Query OK, 1 row affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | chatgroup | | information_schema | | mysql | | people | | performance_schema | | sys | | user | +--------------------+ 7 rows in set (0.00 sec)
create database "数据库名" character set utf8; 或者: create database "数据库名" charset utf8;
直接建立的数据库的编码方式是MySQLM默认的编码方式latin1(单字节编码),若是要在数据库中存放中文数据,设置数据库的编码方式为utf8。性能
show create database "数据库名"
例如:编码
mysql> show create database people; +----------+----------------------------------------------------------------------------------------------------+ | Database | Create Database | +----------+----------------------------------------------------------------------------------------------------+ | people | CREATE DATABASE `people` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!80016 DEFAULT ENCRYPTION='N' */ | +----------+----------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
create database xxx character set utf8 collate utf8_general_ci;
alter database "数据库名" character set "编码方式"
例如:code
mysql> alter database people character set latin1; Query OK, 1 row affected (0.00 sec) mysql> show create database people; +----------+------------------------------------------------------------------------------------------------------+ | Database | Create Database | +----------+------------------------------------------------------------------------------------------------------+ | people | CREATE DATABASE `people` /*!40100 DEFAULT CHARACTER SET latin1 */ /*!80016 DEFAULT ENCRYPTION='N' */ | +----------+------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
drop database "库名";
use "数据库名"; 或 use "数据库名"
刚链接数据库的时候,没有处于任何一个数据库,若是要是要某个数据库,就须要用 "use" 进入数据库中。orm
<font color=red>use "数据库名" 是SQL语句中惟一能够省略分号的语句。</font>server
select database();
例如:
mysql> use people; Database changed mysql> select database(); +------------+ | database() | +------------+ | people | +------------+ 1 row in set (0.00 sec)
长时间操做数据库时,若是在不少数据库中来回切换后,查看当前的数据库,避免操做错了数据库。
show tables;
例子:
mysql> show tables; Empty set (0.00 sec)
该数据库没有表
create table "表名"(字段1 字段类型, 字段2 字段类型, 字段3 字段类型...); create table chinese(pid INT, name CHAR(20), age INT);
mysql> create table chinese(pid INT, name CHAR(20), age INT); Query OK, 0 rows affected (0.02 sec) mysql> show tables; +------------------+ | Tables_in_people | +------------------+ | chinese | +------------------+ 1 row in set (0.00 sec)
show create table "表名";
显示表的字段信息,MySQL引擎和默认字符编码等信息
mysql> show create table chinese; +---------+----------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------+----------------------------------------------------------------------------------------------------------------------------------------------------+ | chinese | CREATE TABLE `chinese` ( `pid` int DEFAULT NULL, `name` char(20) DEFAULT NULL, `age` int DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +---------+----------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
更好的展现表的信息
describe "表名"; 或 desc "表名";
例如:
mysql> describe chinese; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | pid | int | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | int | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
为已存在的表添加一个新字段
alter table "表名" add "字段名" 数据类型;
例如:
mysql> alter table chinese height INT; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'height INT' at line 1 mysql> alter table chinese add height INT; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> mysql> describe chinese; +--------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+----------+------+-----+---------+-------+ | pid | int | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | int | YES | | NULL | | | height | int | YES | | NULL | | +--------+----------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
alter table "表名" drop "字段名";
例如:
mysql> alter table chinese drop height; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc chinese; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | pid | int | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | int | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
alter table "表名" modify "字段名" 新数据类型;
mysql> alter table chinese modify name varchar(12); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc chinese; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | pid | int | YES | | NULL | | | name | varchar(12) | YES | | NULL | | | age | int | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.01 sec)
alter table "表名" change "原字段名" "新字段名" 新数据类型;
mysql> alter table chinese change name pname CHAR(128); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc chinese; +-------+-----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------+------+-----+---------+-------+ | pid | int | YES | | NULL | | | pname | char(128) | YES | | NULL | | | age | int | YES | | NULL | | +-------+-----------+------+-----+---------+-------+ 3 rows in set (0.00 sec)
rename table "旧表名" to "新表名";
alter table "表名" character set gbk;
select "列" from "表";
例:
mysql> select friend from leo; +--------+ | friend | +--------+ | 张三 | +--------+ 1 row in set (0.00 sec)
insert into "表"("列") values ("内容");
例:
mysql> insert into leo(friend) values ('李四'); Query OK, 1 row affected (0.00 sec) mysql> select friend from leo; +--------+ | friend | +--------+ | 张三 | | 李四 | +--------+ 2 rows in set (0.00 sec)
注意:
若是对每一个数据都依次插入信息,则不须要在表后面的括号内填数据信息,直接依次填其值
insert into "表"(内容一、内容二、内容3....);
delete from "表" where "列" = "内容";
例:
mysql> delete from leo where friend = "李四"; Query OK, 1 row affected (0.00 sec) mysql> select friend from leo; +--------+ | friend | +--------+ | 张三 | +--------+ 1 row in set (0.00 sec)
select "列" from "表" where "列" = "值"
例:
mysql> select friend from leo where friend = '张三'; +--------+ | friend | +--------+ | 张三 | +--------+ 1 row in set (0.00 sec)
update "表" set "列" = "值"
例:
mysql> update leo set friend = "李四"; Query OK, 3 rows affected (0.01 sec) Rows matched: 4 Changed: 3 Warnings: 0 mysql> select friend from leo; +--------+ | friend | +--------+ | 李四 | | 李四 | | 李四 | | 李四 | +--------+ 4 rows in set (0.00 sec)
数据类型 | 数据范围 |
---|---|
TINYINT | -128 ~ 127 |
SMALLINT | -32768 ~ 32767 |
MEDIUMINT | -2^23 ~ 2^23-1 |
INT | -2^31 ~ 2^31-1 |
BIGINT | -2^63 ~ 2^63-1 |
数据类型 | 字符范围 | 用途 |
---|---|---|
CHAR(n) | 0 ~ 255 byte | 定长字符串 |
VARCHAR(n) | 0 ~ 65535 byte | 变长字符串 |
TEXT | 0 ~ 65535 byte | 长文本数据 |
LONGTEXT | 0 ~ 2^32-1 byte | 极大文本数据 |
BLOB | 0 ~ 65535 byte | 二进制长文本数据 |
LONGBLOG | 0 ~ 2^32-1 byte | 二进制极大文本类型 |
数据类型 | 数据用法 | 数据范围 |
---|---|---|
Float | Float(m,n) | 7位有效数 |
Double | Double(m,n) | 15位有效数 |
Decimal | Decimal(m,n) | 28位有效数 |
数据类型 | 数据用法 | 数据范围 |
---|---|---|
DATE | YYY-MM-DD | 日期 |
TIME | HH::MM:SS | 时间 |
YEAR | YYYY | 年份 |
DATETIME | YYYY-MM-DD HH::MM:SS | 日期和时间 |
TIMESTAMP | 10位或13位整数(秒数) | 时间戳 |
enum(枚举值1,枚举值2,...)
枚举类型只能在列出的值中选择一个,如性别。
select * from "表名";
例:
mysql> select * from leo; +----------+--------+-----------+ | password | friend | chatgroup | +----------+--------+-----------+ | 123456 | NULL | NULL | +----------+--------+-----------+ 1 row in set (0.00 sec)
select "列名1", "列名2", "列名3".... from "表名";
例如:
mysql> select * from leo; +----------+--------+-----------+ | password | friend | chatgroup | +----------+--------+-----------+ | 123456 | NULL | NULL | +----------+--------+-----------+ 1 row in set (0.00 sec) mysql> select password from leo; +----------+ | password | +----------+ | 123456 | +----------+ 1 row in set (0.00 sec)
select "列名" from "表名";
例如:
mysql> select password from leo; +----------+ | password | +----------+ | 123456 | +----------+ 1 row in set (0.00 sec)
通常,除非肯定须要表中的每一个列,不然最好别使用 * 通配符。虽然使用通配符可能会使本身省事,不用明确列出所需列,但检索不须要的列一般会下降检索和应用程序的性能。
可是 * 通配符能够检索初名字未知的列。
select "行名" from "表名";
使用此语句会返回全部匹配的行,因此可能会有重复的值,若是只想返回不一样的值,能够用<font color=red>DISTINCT</font>关键字,此关键字指示MySQL只返回不一样的值。
select DISTINCT "行名" from "表名";
select语句返回全部匹配的行,它们多是指定表中的每一个行,为了返回第一行或者前几行,可以使用<font color=red>LIMIT</font>子句
select "列名" from "表名" LIMIT 5;
此语句检索单个列,<font color=red>LIMIT 5 指示了MySQL返回很少于5行</font>。
为了得出下一个5行,可指定要检索的开始行和行数,以下所示:
select "列名" from "表名" LIMIT 5,5;
LIMIT 5,5 指示MySQL返回从行5开始的5行。第一个位开始的行数,第二个数为要检索的行数。
注意:
行0: 检索出来的第一行为行0而不是行1。所以,LIMIT 1,1 将检索出第二行而不是第一行。
在行数不够时: LIMIT 中指定要检索的行数为检索的最大行数,若是没有足够的行(例如,给出LIMIT 10,5,但只有13行),MySQL将只返回它能返回的那么多行。
拓展:
MySQL 5 支持LIMIT的另外一种替代语法:LIMIT 4 OFFET 3 意为从行3开始取4行,就像LIMIT 3,4同样。
select 表名.列名 from 表名;
例:
mysql> select leo.password from leo; +----------+ | password | +----------+ | 123456 | +----------+ 1 row in set (0.00 sec)
表名也能够是彻底限定的,以下所示:
select 表名.列名 from 数据库.表名
例:
mysql> select leo.password from user.leo; +----------+ | password | +----------+ | 123456 | +----------+ 1 row in set (0.00 sec)
数据排序默认升序排序。
检索出的数据并非以纯粹的随机顺序显示的。若是不排序,数据通常将以它在底层表中出现的顺序显示。这能够是数据最初添加到表中的顺序。可是,若是数据后来进行过更新或删除,则此顺序将会收到MySQL重用回收存储空间的影响。所以,若是不明确的话,不能依赖该排序顺序。
SQL语句由子句构成,有些子句是必需的,而有的是可选的。一个子句一般由一个关键字和所提供的数据组成,子句的例子就有 select语句 和 from语句
为了明确地排序用select语句检索出地数据,可以使用order by子句。 ordey by子句取一个或多个列的名字,据此对输出进行排序。
select "列名" from "表名" order by "列名";
<font color=red>以字母顺序进行排序</font>
select "列名1", "列名2" from "表名" order by "列名1","列名2";
select "列名1", "列名2", "列名3" from "表名" order by "列名1","列名2";
仅对列名1和列名2进行排序,列名3不进行排序。
为了进行降序排序,必须指定DESC关键字。
select "列名1","列名2","列名3" from "表名" order by "列名1" DESC;
仅对列名1进行降序排序。
select "列名1","列名2","列名3", from "表名" order by "列名1" DESC, "列名2";
对列名1进行降序排序,对列名2进行默认升序排序。
注意:
在多个列上降序排序: 若是想在多个列上进行降序排序,<font color=red>必须对每一个列指定DESC关键字</font>
Tips:
区分大小写和排序顺序 在对文本性的数据进行排序时,A与a相同吗?a位于B以前仍是位于Z以后?这些问题不是理论问题,其答案取决于数据库如何设置。在字典(dictionary)排序顺序中,A被视为与a相同,这是MySQL
(和大多数数据库管理系统)的默认行为。可是,许多数据库管理员可以在须要时改变这种行为(若是你的数据库包含大量外语字符,可能必须这样作)。这里,关键的问题是,若是确实须要改变这种排序顺序,用简单的ORDER BY子句作不到。你必须请求数据库管理员的帮助。
拓展:
使用order by和LIMIT的组合,可以找出一个列中最高或最低的值。
select "列" from "表" order by "列" DESC LIMIT 1;
由于使用降序排序而且只返回一行,因此为找出了最大值。