MySQL指令
1. 数据可以管理
1.1
查看已有的数据库(文件夹)
>>> show databases;
1.2
创建数据库(文件夹)
>>> create databases 数据库名字;
>>> create database 数据库名字 DEFAULT CHARSET utf8 COLLATE utf8_general_ci; # 以UTF-8的编码格式创建数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| first_web |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
1.3
删除数据库(文件夹)
>>> drop database 数据库名字
1.4
进入数据库(文件夹)
>>> use 数据库名
1.5
查看数据库(文件夹)下的所有数据表
>>> show tables;
2. 数据表管理(文件)
2.1
进入数据库(进入文件夹)
>>> use 数据库名
2.2
查看当前数据库下是所有数据表(文件)
>>> show tables;
2.3
创建数据表
MySQL指令要见;
才会执行,所以一下代码直接复制到命令窗口即可;
create table 表名称(
列名称 数据类型,
列名称 数据类型,
列名称 数据类型
)default charset=utf-8; -- UTF-8编码格式
create table tb1(
id int,
name varchar(16),
age int
)default charset=utf8; -- UTF-8编码格式
create table tb1(
id int primary key, -- 主键,不允许为空,且不允许重复
name varchar(16) not null, -- 指定该列不允许为空
age int null # 允许为空
)default charset=utf8; -- UTF-8编码格式
create table tb1(
id int primary key auto_increment, # 主键+自增
name varchar(16),
age int default 3 -- 设置默认值为3,当未给该列传值时,默认为3
)default charset=utf8; -- UTF-8编码格式
mysql> desc tb1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(16) | YES | | NULL | |
| age | int(11) | YES | | 3 | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
-
MySQL常用数据类型
-
整型
-
tinyint
有符号,取值范围:-128 ~ 127(有正有负)【默认】 无符号,取值范围:0 ~ 256(正)
create table tb1( id int primary key auto_increment, age tinyint unsigned -- 无符号,取值范围:0 ~ 256 )default charset=utf8;
create table tb1( id int primary key auto_increment, age tinyint -- 有符号,取值范围:-128 ~ 127 )default charset=utf8;
-
int
int 有符号,取值范围:-2147483648 ~ 2147483647 int unsigned 无符号,取值范围:0 ~ 4294967295
-
bigint
bigint 有符号,取值范围:-9223372036854775808 ~ 9223372036854775807 bigint unsigned 无符号,取值范围:0 ~ 18446744073709551615
-
-
小数类型
-
float
-
double
-
decimal(常用)
-- 准确的小数值,m是数字总个数(负号不算),d是小数点后个数。m最大值为65, d最大值为30。 例如: create table tb1( id int not null primary key auto_ increment, salary decimal(8,2) )default charset=utf8;
-
字符串
-
char(m),查询速度快
定长字符串,m代表字符串长度,最多可容纳255个字符; char(11),固定用11个字符串进行存储,即使没有1个字符也会按11个字符存储。
-
varchar(m),节省空间
变长字符串,m代表字符串长度,最大65535字节 varchar(11),按真实数据的长度存储(前提是:长度小于11)
-
text
储存变长的大字符串,最多可以储存65535(2**16-1)个字符
-
mediumtext
最多可以储存2**24-1个字符
-
longtext
最多可以储存2**32-1个字符
-
-
时间格式
-
datetime(年月日 时分秒)
YYYY-MM-DD HH:MM:SS
-
date(年月日)
YYYY-MM-DD
-
-
2.4
删除数据表
>>> drop talbe 表名称;
2.5
插入数据
-- 插入一列
insert into 表名称(列名1,列名2,列名3) values(数据1,数据2,数据3);
-- 插入多列
insert into 表名称(列名1,列名2,列名3) values(数据1,数据2,数据3),(数据1,数据2,数据3),....;
2.6
删除数据
delete from 表名; -- 删除整个数据表
delete from 表名 where 条件; -- 删除符合条件的数据
delete from tb1;
delete from tb1 where id = 1; -- 删除id=1的行
delete from tb1 where id = 1 and name='写'; -- 删除id=1并且name='写'的行
delete from tb1 where id in (1,3); -- 删除id=1和3的行
delete from tb1 where id > 1;
2.7
修改数据
update 表名 set 列=值;
update 表名 set 列=值, 列=值;
update 表名 set 列=值 where 条件; -- 满足条件时,修改某列的值
update dt1 set name='hhh'; -- 将name列全部修改为’hhh‘
update dt1 set age=age+20 where id > 23; -- 将id>23的所有行中的age列加20
2.8
查询数据
select * from 表名; -- 查看表格所有数据
select 列名1,列名2 from 表名; -- 从表中仅查看某几列的数据
select 列名1,列名2 from 表名 where 条件; --查看符合条件的某几列数据