操作表

创建表语法

创建一个表,多个字段:
create table 表名(
字段名 列类型 [可选的参数], # 记住要加逗号
字段名 列类型 [可选的参数] # 最后一行不加逗号
...
)charset=utf8; # 后面加;号

列约束

说明:列约束用来保证数据的完整性和一致性,约束条件都是创建表语法的时候可选参数

  • auto_increment:表示自增加1

  • not null:标识该字段不能为空

  • ``default:为该字段设置默认值`

  • foreign key:外键索引

  • unique key:标识字段的值是唯一的,字段值不能重复,可以有多个

  • primary key:主键索引,可以加快查询速度,字段值不能重复,只能有一个

  • unsigned:无符号,默认是有符号的

  • zerofill:使用0填充

例子

# 创建表
mysql> create table t3(
-> id int unsigned auto_increment primary key,
-> name char(10) not null default "xxx",
-> age int not null default 0
-> )charset=utf8;
Query OK, 0 rows affected (0.03 sec) # 向表中插入数据
mysql> insert into t3 (age) values (18);
Query OK, 1 row affected (0.00 sec) # 查询表中数据
mysql> select * from t3;
+----+------+-----+
| id | name | age |
+----+------+-----+
| 1 | xxx | 18 |
+----+------+-----+
1 row in set (0.00 sec) # 查看表结构 desc 表名
mysql> desc t3;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | xxx | |
| age | int(11) | NO | | 0 | |
+-------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

列类型

列类型 说明
数字(整数) 存储年龄、等级、id等
数字(浮点数) 存储薪资、身高、体重等
字符串 存储姓名、性别等
时间日期类型 存储注册时间、入职时间等
枚举 字段的值只能在给定范围中选择

数字(整形)

  • tinyint:小整数,有符号:[-128 ~ 127],无符号:[0 ~ 255]
  • smallint:大整数,有符号:[-32768 ~ 32767],无符号:[0 ~ 65535]
  • mediumint:大整数
  • bigint:极大整数
  • int (推荐使用):大整数,有符号:[-2147483648 ~ 2147483647],无符号:[0 ~ 4294967295]

unsigned 加在列类型后面,代表无符号,不能取负数,默认是有符号,可以是负数

应用场景:根据公司业务场景选择合适类型

例子:

# 我们创建一个t4表,限制使用无符号的
mysql> create table t4(x int unsigned);
Query OK, 0 rows affected (0.02 sec) # 查看表结构,默认int数值长度已经设置为10位
mysql> desc t4;
+-------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+-------+
| x | int(10) unsigned | YES | | NULL | |
+-------+------------------+------+-----+---------+-------+
1 row in set (0.00 sec) # 只能插入无符号:[0 ~ 4294967295]范围值内的数字
# 超过数字的长度也报错
mysql> insert into t4 values (42949672955);
ERROR 1264 (22003): Out of range value for column 'x' at row 1 # 只能插入无符号:[0 ~ 4294967295]范围值内的数字
# 超过范围即报错
mysql> insert into t4 values (4294967296);
ERROR 1264 (22003): Out of range value for column 'x' at row 1 # 在这个范围内则成功
mysql> insert into t4 values (4294967295);
Query OK, 1 row affected (0.00 sec) # 查询插进去的数据,如果数据库配置是非安全模式的话,插进去的数据是这个数据类型限制的最大数值
mysql> select * from t4;
+------------+
| x |
+------------+
| 4294967295 |
| 4294967295 |
+------------+
2 rows in set (0.00 sec)

注意:那么有的同学可能不会报错,能插进去,那是因为你的mysql5.6 没有开启安全模式,mysql5.7 以后的版本默认都是安全模式

# 查看当前数据库模式:
mysql> show variables like "%sql_mode%";
+---------------+--------------------------------------------+
| Variable_name | Value |
+---------------+--------------------------------------------+
| sql_mode | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+---------------+--------------------------------------------+
1 row in set (0.00 sec) sql_model=no_engine_substitution # 非安全性,默认
sql_model=strict_trans_tables # 安全性 # 临时设置为安全模式,服务重启后会被重置
mysql>: set global sql_mode="strict_trans_tables"; # 在root用户登录状态下
# 在设置后,quit断开数据库连接后(服务器不重启)就会进入安全模式,
# 那么现在在插入超过范围内的数据就会报错

数字(浮点型)

  • float:不一定精确
  • decimal(m,d):存精确的数字,m是数字总个数(负号不算),d是小数点后的数字个数

例子:

# 创建表t5 限制salary字段为decimal数据类型,num为float数据类型
mysql> create table t5(
-> id int auto_increment primary key,
-> salary decimal(16,10),
-> num float
-> )charset=utf8;
Query OK, 0 rows affected (0.02 sec) # 查看表结构
mysql> desc t5;
+--------+----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| salary | decimal(16,10) | YES | | NULL | |
| num | float | YES | | NULL | |
+--------+----------------+------+-----+---------+----------------+
3 rows in set (0.00 sec) # 插入数据,salary总数字长度为16,小数点后面正好为10位,插入数据
mysql> insert into t5 (salary,num) values (500000.2312345678,5000.232423523534634);
Query OK, 1 row affected (0.01 sec) # 查询没有问题,精确存,但是num float类型的不精确,存两位小数点,还四舍五入了
mysql> select * from t5;
+----+-------------------+---------+
| id | salary | num |
+----+-------------------+---------+
| 1 | 500000.2312345678 | 5000.23 |
+----+-------------------+---------+
1 row in set (0.00 sec) # 插入数据,salary总数字长度为15,小数点后面小于10位,插入数据mysql> insert into t5 (salary,num) values (500000.231234567,5000.232423523534634);
Query OK, 1 row affected (0.01 sec) # 查询,不精确,缺省的一位用0补齐,但是num float类型的不精确,存两位小数点,还四舍五入了
mysql> select * from t5;
+----+-------------------+---------+
| id | salary | num |
+----+-------------------+---------+
| 1 | 500000.2312345678 | 5000.23 |
| 2 | 500000.2312345670 | 5000.23 |
+----+-------------------+---------+
2 rows in set (0.00 sec) # 插入数据,salary总数字长度为17,小数点后面大于10位,插入数据
mysql> insert into t5 (salary,num) values (500000.23123456789,5000.232423523534634);
Query OK, 1 row affected, 1 warning (0.01 sec) # 查询,不精确,只能存指定的长度,多出来的四舍五入了,但是num float类型的不精确,存两位小数点,还四舍五入了
mysql> select * from t5;
+----+-------------------+---------+
| id | salary | num |
+----+-------------------+---------+
| 1 | 500000.2312345678 | 5000.23 |
| 2 | 500000.2312345670 | 5000.23 |
| 3 | 500000.2312345679 | 5000.23 |
+----+-------------------+---------+
3 rows in set (0.00 sec)

字符串

  • char:定长
  • varchar:变长

两者区别:

char 定长,无论插入字符是多少个,永远固定占规定的长度,使用场景:身份证、手机号、md5加密过后的密码char(32)

varchar 变长,根据插入的字符串长度计算所占的字节数,但是总有一个字节是用来保存字符串大小的,

如果不能确定插入的数据的大小,一般建议使用varchar(255)。

Value CHAR(4) Storage Required VARCHAR(4) Storage Required
'' ' ' 4 bytes '' 1 byte
'ab' 'ab ' 4 bytes 'ab' 3 bytes
'abcd' 'abcd' 4 bytes 'abcd' 5 bytes
'abcdefgh' 'abcd' 4 bytes 'abcd' 5 bytes

例子:

# 创建t6表
mysql> create table t6(
-> id int unsigned auto_increment primary key,
-> name char(10) not null default 'xxx'
-> )charset=utf8;
Query OK, 0 rows affected (0.02 sec) mysql> desc t6;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | xxx | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec) # 创建t7表
mysql> create table t7(
-> id int unsigned auto_increment primary key,
-> name varchar(10) not null default 'xxx'
-> )charset=utf8;
Query OK, 0 rows affected (0.03 sec) mysql> desc t7;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | xxx | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec) mysql> insert into t6 (name) values ("hello");
Query OK, 1 row affected (0.01 sec) mysql> insert into t7 (name) values ("hello");
Query OK, 1 row affected (0.01 sec) mysql> select * from t6;
+----+-------+
| id | name |
+----+-------+
| 1 | hello |
+----+-------+
1 row in set (0.00 sec) mysql> select * from t7;
+----+-------+
| id | name |
+----+-------+
| 1 | hello |
+----+-------+
1 row in set (0.00 sec) mysql> insert into t6 (name) values ("hello32dwdsaffgfrthtrhtr");
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> insert into t7 (name) values ("hello32dwdsaffgfrthtrhtr");
ERROR 1406 (22001): Data too long for column 'name' at row 1

时间日期类型

  • year:保存年份
  • date:保存日期
  • time:保存时间
  • datetime:保存格式化后的时间
  • timestamp:保存时间戳

例子:

mysql> create table t8(
-> d date,
-> t time,
-> dt datetime
-> );
Query OK, 0 rows affected (0.04 sec) mysql> desc t8;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| d | date | YES | | NULL | |
| t | time | YES | | NULL | |
| dt | datetime | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec) # now()表示当前时间
mysql> insert into t8 values(now(),now(),now());
Query OK, 1 row affected, 1 warning (0.00 sec) # 按照指定时间类型存时间
mysql> select * from t8;
+------------+----------+---------------------+
| d | t | dt |
+------------+----------+---------------------+
| 2019-10-29 | 01:23:08 | 2019-10-29 01:23:08 |
+------------+----------+---------------------+
1 row in set (0.00 sec)

枚举

列出所有选项

mysql> create table t9(
-> id int auto_increment primary key,
-> gender enum("male","female")
-> )charset=utf8;
Query OK, 0 rows affected (0.03 sec) mysql> insert into t9 (gender) values ("male");
Query OK, 1 row affected (0.00 sec) mysql> insert into t9 (gender) values ("female");
Query OK, 1 row affected (0.00 sec) # 只能插入枚举类型的数据
mysql> insert into t9 (gender) values ("femal");
ERROR 1265 (01000): Data truncated for column 'gender' at row 1
mysql>
mysql> select * from t9;
+----+--------+
| id | gender |
+----+--------+
| 1 | male |
| 2 | female |
+----+--------+
2 rows in set (0.00 sec)

drop table 表名

修改表名

语法:

alter table 旧表名 rename 新表名

增加字段

语法:

alter table 表名 add 字段名 列类型 [可选的参数];

上面添加的列永远是添加在最后一列之后,如果需要在指定位置添加字段的话,使用下面的语法

alter table 表名 add 字段名 列类型 [可选的参数] first;

alter table 表名 add 字段名 列类型 [可选的参数] after 字段名

修改字段

alter table 表名 modify 字段名 列类型 [可选的参数]

alter table 表名 change 旧字段名 新字段名 新列类型 [可选的参数]

删除字段

alter table 表名 drop 字段名

show tables:查看表名

复制表结构

1、 查看被复制表的创建语句:show create table 表名,然后拷贝sql语句更换表名执行

2、 create table 旧表名 like 新表名

操作表数据

增加数据,语法:

insert into 表名 (列1,列2) values (值1,值2)

delete from 表名 where 条件 ;如果不加条件删除表中所有数据

truncate 表名;没有where条件,删除表中全部数据,速度比delete快

两者区别:

1、 delete删除之后再插入数据,自增id从上一次主键自增,truncate是从1开始

2、 delete删除是一行一行删除,truncate是全选删除,速度比delete快

update 表名 set 列名1=新值1 where 条件

语法:

select 列1,列2 from 表名 [where 条件]

select * from 表名 where id between ... and ...;between...and...取值范围是闭区间

查询去重

select distinct 列名 from 表名

四则运算

select 列表*10 from 表名

in

select * from 表名 where id in (取值范围)

like模糊查询

select * from 表名 where 列名 like 'x%'; 以x开头,%表示通配符

select * from 表名 where 列名 like '%x'; 以x结尾,%表示通配符

select * from 表名 where 列名 like '%x%' 包含x的,%表示通配符

MySQL操作表和表记录的更多相关文章

  1. Mysql之单表记录查询

    数据记录查询: 1.简单数据记录查询: select * from table_name; select allfield from table_name; select distinct(属性名) ...

  2. mysql中数据表记录的增删查改(1)

    数据记录的增删改查 insert into `数据表名称` (`字段名称`, ...) values ('1', ...); delete from `数据表名称` where 子句; update ...

  3. Mysql 触发器 A表记录到B表

    1:查询出需要的列名 备用 #列名 select COLUMN_NAME from information_schema.columns where TABLE_SCHEMA='yunpiaobox_ ...

  4. mysql中数据表记录的增删查改(2)

    select `数据表.字段1`, group_concat(`数据表.字段2`) from `数据表` group by `数据表.字段1` order by `数据表.字段1` desc; sel ...

  5. mysql 删除表记录 delete和truncate table区别

    MySQL中删除表记录delete from和truncate table的用法区别: mysql中有两种删除表中记录的方法: (1)delete from语句, (2)truncate table语 ...

  6. 三十一.MySQL存储引擎 、 数据导入导出 管理表记录 匹配条件

    1.MySQL存储引擎的配置 查看服务支持的存储引擎 查看默认存储类型 更改表的存储引擎 设置数据库服务默认使用的存储引擎 1.1 查看存储引擎信息 mysql> SHOW ENGINES\G ...

  7. Mysql库、表、记录的基本操作

    库的操作 ---> 类似于文件夹 - 增: 创建数据库: create database db1; 创建带字符集的数据库: create database db2 charset=utf8; - ...

  8. mysql支持跨表delete删除多表记录

    前几天写了Mysql跨表更新的一篇总结,今天我们看下跨表删除. 在Mysql4.0之后,mysql开始支持跨表delete. Mysql可以在一个sql语句中同时删除多表记录,也可以根据多个表之间的关 ...

  9. MySQL查询数据表中数据记录(包括多表查询)

    MySQL查询数据表中数据记录(包括多表查询) 在MySQL中创建数据库的目的是为了使用其中的数据. 使用select查询语句可以从数据库中把数据查询出来. select语句的语法格式如下: sele ...

随机推荐

  1. RoadFlow2.7.5 MyController.cs

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  2. Windows系统下安装MySQL 8.0.11数据库

    MySQL数据库是常用的数据库之一,而且该数据库开源免费,所以很多公司在使用.本文记录如何在Windows系统下安装MySQL数据库,本次安装的版本号为8.0.11,这个版本是当前的最新版本,据宣传, ...

  3. 第三天:字典表dict、元组tuple、文件与类型汇总

    1.字典表dict 声明 {键: 值,...} dict(键=值) d = {'isbn':'13123','title':'python入门'} #字典表中的键不能使用诸如列表这种可以改变的,只能使 ...

  4. mysql保存当前时间精确到秒

       用mybatis在mysql中保存字段精确到秒须要两个步骤. 1.如今mysql中将时间字段改为datetime 比如:alter table tablename add pay_date da ...

  5. DLL相关下断点

    加载DLL 的时候断: sxe ld:[dll] 卸载DLL 的时候断: sxe ud:[dll] 比如: sxe ld:wininet.dll  (在wininet.dll 被装载的时候断点) 这里 ...

  6. capserjs-prototype(中)

    evaluateOrDie() 具体样式: evaluateOrDie(Function fn[, String message, int status]) Evaluates an expressi ...

  7. 21-4indexOf

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 三角形的实现和盒模型、层模型、浮动模型、定位、权重、margin问题

    相邻的border会平分所占的区域,出现一个斜线, .my_triangle{ width: 10px; height: 10px; background-color: blue; border-wi ...

  9. 40. 组合总和 II

    题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只 ...

  10. MYSQL - database 以及 table 的增删改查

    MYSQL - database 以及 table 的增删改查 MySQL的相关概念介绍 MySQL 为关系型数据库(Relational Database Management System), 这 ...