V2版本: 改进操作界面,统一表单样式,更多工具支持批量生成,欢迎使用 到达~
ICON助手 JS助手 CSS助手 SQL助手 Semi设计变量

SQL建表示例


一、创建数据库


简单写法
 CREATE DATABASE tp_chat; 
指定字符集、排序
 CREATE DATABASE tp_chat character set utf8mb4 collate utf8mb4_general_ci; 

一、一个标准建表语句


包括表名、列名、列的数据类型、列的约束条件;设置 comment 备注、SET 字符集、COLLATE 子句指定排序规则;utf8mb4 是一个用于存储 Unicode 字符的字符集,例如 emoji 表情符号;
 CREATE TABLE `user` (
     `id` int not null auto_increment comment '用户ID',
     `username` varchar(50) not null comment '用户名',
     `password` varchar(50) not null comment '密码',
     `email` varchar(100) not null comment '邮箱',
     `age` int comment '年龄',
     `gender` enum('male', 'female') comment '性别',
     `created_at` timestamp DEFAULT CURRENT_timestamp comment '创建时间',
     `updated_at` timestamp DEFAULT CURRENT_timestamp ON UPDATE CURRENT_timestamp comment '更新时间',
     primary key (`id`),
     unique (`username`) comment '用户名唯一',
     unique (`email`) comment '邮箱唯一'
 ) character set utf8mb4 collate utf8mb4_general_ci comment '用户表';     

混合表(多类型)


create table `mix` (
     `id` int not null auto_increment comment '文章id',
     `title` varchar(100) not null comment '文章标题',
     `content` text comment '文章内容',
     `author` varchar(50) not null comment '作者',    
     `user_id` int not null comment '用户id',
     `goods_id` int not null comment '商品id',
     `price` decimal(10,2) not null comment '订单价格',    
     `created_at` timestamp default current_timestamp comment '创建时间',
     `updated_at` timestamp default current_timestamp on update current_timestamp comment '更新时间',
     primary key (`id`),    
     index (`user_id`) comment '用户id索引',
     index (`goods_id`) comment '商品id索引',
     fulltext index `idx_content`(`content`) comment '文章内容全文索引'
) comment '字段混合示例表' character set utf8mb4 collate utf8mb4_general_ci;

一、字段值范围

     // Int
    `tinyint`:	     非常小的整数类型,有符号范围为 -128 到 127,无符号范围为 0 到 255
    `smallint`:     小整数类型,有符号范围为 -32768 到 32767,无符号范围为 0 到 65535
    `mediumint`:    中等大小的整数类型,有符号范围为 -8388608 到 8388607,无符号范围为 0 到 16777215
    `int`:          整数类型,有符号范围为 -2147483648 到 2147483647,无符号范围为 0 到 4294967295
    `bigint`:       大整数类型,有符号范围为 -9223372036854775808 到 9223372036854775807,无符号范围为 0 到 18446744073709551615
  
    // 浮点
    `float`:        单精度浮点数类型,最小值为 -3.402823466E+38,最大值为 3.402823466E+38
    `double`:       双精度浮点数类型,最小值为 -1.7976931348623157E+308,最大值为 1.7976931348623157E+308
    `decimal`:      精确数值类型,存储固定精度和比例的小数值

      // 日期
    `date`:         日期类型,存储从 '1000-01-01' 到 '9999-12-31' 的日期值
    `time`:         时间类型,存储从 '-838:59:59' 到 '838:59:59' 的时间值
    `datetime`:     日期和时间类型,存储从 '1000-01-01 00:00:00' 到 '9999-12-31 23:59:59' 的日期和时间值
    `timestamp`:    时间戳类型,存储从 '1970-01-01 00:00:01' UTC 到 '2038-01-19 03:14:07' UTC 的时间戳值
    `year`:         年份类型,存储从 1901 到 2155 年的年份值
    
      // 字符串
    `char`:         定长字符串类型, 255 个字符
    `varchar`:      可变长度字符串类型, 65535 个字符
    `text`:         文本类型, 65535 个字符
    `tinytext`:     非常小的文本类型, 255 个字符
    `mediumtext`:   中等大小的文本类型, 16777215 个字符
    `longtext`:     大型文本类型, 4294967295 个字符
    `enum`:         枚举类型,存储指定的枚举值之一
    `set`:          集合类型,存储指定的集合值之一     
    
    // 二进制
    `binary`:       定长二进制类型, 255 个字节
    `varbinary`:    可变长度二进制类型, 65535 个字节
    `tinyblob`:     非常小的二进制类型, 255 个字节
    `blob`:         二进制类型, 65535 个字节
    `mediumblob`:   中等大小的二进制类型, 16777215 个字节
    `longblob`:     大型二进制类型, 4294967295 个字节          
首页 喜欢 我的 定制
定制咨询
微信二维码
扫一扫上面的二维码,加我为朋友。
微信扫码周一至周六服务
接定制开发需求