mariadb数据类型

mariadb数据类型分为数字、日期、时间以及字符串值。

适用类型原则:够用就行,尽量使用范围小的,而不用大的

常用数据类型:

1、整数 int,bit   #例如 年纪 适用于必须是整数的

2、小数 decimal   # 例如 身高、收入                                #decimal(5,2)  5位数保留小数点后两位   decimal(7,1)7位数保留小数点后两位

3、字符串 char,varchar  (可定义字符) #例如  姓名 、班级、籍贯等等,也可以整数、小数等数据类型 但是不能函数运算

4、日期时间 date,time,datetime

5、枚举类型 enum  #可指定的类型  例如 性别

约束:

主键primary key:物理上存储的顺序

非空not null:此字段不能为空

唯一unique:此字段不允许重复

默认default:当不填写此值时会使用默认值,如果填写则已填写为准

外键foreign key:对关系字段进行约束,当为关系字段填写值时,会到关联的表中查询此值是否存在,如果存在则填写成功,如果不存在则填写失败并抛出异常

创建一张class表 (id,name,age)

MariaDB [testdb]> create table class(id int primary key not null auto_increment,name varchar(20), age tinyint unsigned);
Query OK, 0 rows affected (0.00 sec)

在class表中插入数据 insert into

MariaDB [testdb]> insert into class(name,age) values('李志锋',29);
Query OK, 1 row affected (0.00 sec)

查看class表 select

MariaDB [testdb]> select * from class;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | 李志锋 | 29 |
+----+-----------+------+
1 row in set (0.00 sec)

查看表结构 desc

MariaDB [testdb]> desc class;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

修改表-添加字段  --alter table 表名 add 列名 类型;

MariaDB [testdb]> alter table class add gender enum ('男','女','保密');
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0 MariaDB [testdb]> desc class;
+--------+----------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| gender | enum('男','女','保密') | YES | | NULL | |
+--------+----------------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

修改表-修改表字段类型不重名版本  --alter table 表名 modify 列名 类型及约束;

MariaDB [testdb]> alter table class modify gender enum('男','女','保密') default'保密';
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0 MariaDB [testdb]> desc class;
+----------+----------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| gender | enum('男','女','保密') | YES | | 保密 | |
| birthday | datetime | YES | | NULL | |
+----------+----------------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

修改表-修改表字段名称、类型  --alter table 表名 change 原名 新名 类型及约束;

MariaDB [testdb]> alter table class change birthday birth date;
Query OK, 1 row affected (0.02 sec)
Records: 1 Duplicates: 0 Warnings: 0 MariaDB [testdb]> desc class;
+--------+----------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| gender | enum('男','女','保密') | YES | | 保密 | |
| birth | date | YES | | NULL | |
+--------+----------------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec) 

修改表-删除字段 -- alter table 表名 drop 列名;

MariaDB [testdb]> alter table class drop gender;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0 MariaDB [testdb]> desc class;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | NULL | |
| birth | date | YES | | NULL | |
+-------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

 

 修改表内容

 --update 表名 set 列1=值1, 列2=值2... where 条件;
例: 将class表中的绿帽子改成红帽
MariaDB [testdb]> select * from class;
+----+-----------+------+-------+------+
| id | name | age | birth | high |
+----+-----------+------+-------+------+
| 1 | 李志锋 | 29 | NULL | NULL |
| 2 | 绿帽子 | 19 | NULL | NULL |
| 3 | 王晶 | 29 | NULL | NULL |
+----+-----------+------+-------+------+
3 rows in set (0.00 sec) MariaDB [testdb]> update class set name=('红帽') where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [testdb]> select * from class;
+----+-----------+------+-------+------+
| id | name | age | birth | high |
+----+-----------+------+-------+------+
| 1 | 李志锋 | 29 | NULL | NULL |
| 2 | 红帽 | 19 | NULL | NULL |
| 3 | 王晶 | 29 | NULL | NULL |
+----+-----------+------+-------+------+
3 rows in set (0.00 sec)

  

删除表内容

 delete from 表名 where 条件

例:将class表中的红帽删除

MariaDB [testdb]> delete from class where name=('红帽');
Query OK, 1 row affected (0.00 sec) MariaDB [testdb]> select * from class;
+----+-----------+------+-------+------+
| id | name | age | birth | high |
+----+-----------+------+-------+------+
| 1 | 李志锋 | 29 | NULL | NULL |
| 3 | 王晶 | 29 | NULL | NULL |
+----+-----------+------+-------+------+
2 rows in set (0.00 sec)

  

												

最新文章

  1. 学习笔记之MVC级联及Ajax操作
  2. C# 显示问题
  3. 【Go入门教程8】总结(25个关键字)
  4. 20150625_Andriod_01_ListView1_条目选中
  5. 定向转发和重定向实现 <select >下拉表单数据传送
  6. PHP学习——数据类型
  7. Centos7 安装redis3.2.3 过程
  8. Java [leetcode 6] ZigZag Conversion
  9. JobDeer 的《程序员必读的职业规划书》
  10. startActivityForResult不返回结果
  11. 管道通信(使用popen和pclose功能简单的控制管道)
  12. 通过扩大IE使用内存,解决skyline在IE下模型不能加载的方法
  13. S7-200和S7-300profibus-DP通信
  14. Struts2(六) 用Struts完成客户列表显示
  15. 在CentOS 7+ 安装Kubernetes入门
  16. js 在iframe子页面获取父页面元素,或在父页面 获取iframe子页面的元素的几种方式
  17. bootstrap tooltips在 angularJS中的使用
  18. deviceMotion.userAcceleration加速度方向
  19. Centos不能上外网解决
  20. Difference between MB Star C3 and MB Star C4

热门文章

  1. vue 学习 css第四天
  2. Unity 性能分析小工具
  3. 腾讯云静态资源放到cos存储桶里,并开启CDN自定义域名加速
  4. 遍历dom节点
  5. Unity中UGUI图片跟随文本自适应方法
  6. Java基础-注释、标识符和关键字、数据类型及拓展
  7. Selenium私房菜系列8 -- 玩转Selenium Server【OO】
  8. HelloWorld (用记事本写,在dos窗口里运行)
  9. SpringMVC的常用操作汇总
  10. solve--NAT模式下配置静态IP地址