How to create a table in mysql with SQL statement

  在日常的软件开发过程中需要创建大量的表来存储各类业务信息,通常情况下,大家都是通过Mysql的图形化客户端工具来建表的,不过这种方式虽然方便,但是效率并不高,如果掌握了常用的建表语句,用语句建表也是相当快的。

  我们今天给大家分享一个建表模板语句,基于这个语句就可以快速完成表的创建,接下来,我们来看一看这个建表语句:

DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `id` int(20) NOT NULL DEFAULT '0' COMMENT '用户ID',
  `user_name` varchar(64) NOT NULL DEFAULT '' COMMENT '用户名',
  `password` varchar(32) NOT NULL DEFAULT '' COMMENT '登陆密码',
  `nick_name` varchar(32) NOT NULL DEFAULT '' COMMENT '昵称',
  `real_name` varchar(32) NOT NULL DEFAULT '' COMMENT '真实姓名',
  `mobile` varchar(11) NOT NULL DEFAULT '' COMMENT '手机号',
  `created_by` int(20) NOT NULL DEFAULT '-1' COMMENT '创建者',
  `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_by` int(20) NOT NULL DEFAULT '-1' COMMENT '修改者',
  `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '已经被删除吗,0:未删除,1:已删除,其值决定记录是否可见',
  `disabled` bit(1) NOT NULL DEFAULT b'0' COMMENT '已经被禁用吗,0:未禁用,1:已禁用,其值决定记录是否可用',
  `version` int(20) NOT NULL DEFAULT '0' COMMENT '版本号',
  `remark` varchar(254) NOT NULL DEFAULT '' COMMENT '备注',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户基本信息表';

  其中以下字段是现代互联网软件开发平台对数据管理的通用字段,我们来看一下。

  `created_by` int(20) NOT NULL DEFAULT '-1' COMMENT '创建者',
  `created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_by` int(20) NOT NULL DEFAULT '-1' COMMENT '修改者',
  `updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '已经被删除吗,0:未删除,1:已删除,其值决定记录是否可见',
  `disabled` bit(1) NOT NULL DEFAULT b'0' COMMENT '已经被禁用吗,0:未禁用,1:已禁用,其值决定记录是否可用',
  `version` int(20) NOT NULL DEFAULT '0' COMMENT '版本号',
  `remark` varchar(254) NOT NULL DEFAULT '' COMMENT '备注',

  字段deleted用于逻辑删除。

  字段version用于乐观锁。

  经常看到大家在日期时间类型的选择上会选择timestamp类型,我个人认为这也是可以的,不过,最合适的还是datatime类型,如果业务上只需要精确到日期,也可以选择date类型。timestampdatatime有什么区别呢,依据MySQL官方文档The DATE, DATETIME, and TIMESTAMP Types一节的介绍,我们发现两者有如下一些区别。

项目datetimetimestamp
时间范围1000-01-01 00:00:00~9999-12-31 23:59:591970-01-01 00:00:01~2038-01-19 03:14:07
时区不可以可以
默认值CURRENT_TIMESTAMPCURRENT_TIMESTAMP

参考文献

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
To match Kafka real-time data with a regular expression in Java, you can use the following steps: 1. First, connect to your MySQL database using the JDBC driver. 2. Retrieve the Kafka real-time data from the Kafka topic. 3. Create a regular expression pattern using the java.util.regex.Pattern class. 4. Loop through the Kafka data and use the Matcher class to match the regular expression pattern with the data. 5. If the pattern matches, then you can insert the data into the MySQL table. Here's an example code snippet that demonstrates how to use regular expression matching with Kafka real-time data using Java: ```java import java.sql.*; import java.util.regex.*; public class KafkaRegexMatchExample { public static void main(String[] args) throws SQLException { String url = "jdbc:mysql://localhost:3306/mydatabase"; String username = "root"; String password = "password"; // Connect to MySQL database Connection connection = DriverManager.getConnection(url, username, password); // Retrieve Kafka real-time data // TODO: Implement Kafka consumer // Create regular expression pattern Pattern pattern = Pattern.compile("regex_pattern_here"); // Loop through Kafka data and match with pattern while (true) { String kafkaData = getKafkaData(); // Get Kafka data Matcher matcher = pattern.matcher(kafkaData); if (matcher.matches()) { // Insert data into MySQL table PreparedStatement statement = connection.prepareStatement("INSERT INTO mytable (column1, column2) VALUES (?, ?)"); statement.setString(1, matcher.group(1)); statement.setString(2, matcher.group(2)); statement.executeUpdate(); } } } private static String getKafkaData() { // TODO: Implement Kafka consumer logic to retrieve data } } ``` Note that this is just an example, and you'll need to modify it based on your specific requirements.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qwfys200

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值