1. 创建表和表结构
1 2 3 4 5 6 7 8 9 10 11 | DROP TABLE IF EXISTS `students`; CREATE TABLE `students` ( `id` int (10) unsigned NOT NULL AUTO_INCREMENT, `stu_name` varchar (20) NOT NULL , `stu_sex` varchar (10) NOT NULL , `class_id` int (10) NOT NULL , `age` int (10) NOT NULL DEFAULT 18, `create_time` datetime NULL DEFAULT CURRENT_TIMESTAMP , `update_time` datetime NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP , PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1000 DEFAULT CHARSET=utf8; |
解释:
如果students表存在就删除它
创建students表,
id 整型 无符号 不为空 自动增长
stu_name 字符型长度20 不为空
stu_sex 字符型长度5 不为空
class_id 整型 不为空
age 整型 不为空 默认18
create_time 时间类型 默认当前时间
update_time 时间类型 默认当前时间,update操作时会自动更新
主键是 id
ENGINE=InnoDB使用innodb引擎
DEFAULT CHARSET=utf8 数据库默认编码为utf-8
AUTO_INCREMENT=1000 自增键的起始序号为1000
2. 插入数据
1 2 3 4 5 | insert into students (stu_name,stu_sex,class_id,age) values ( '张三' , '男' ,1001,18), ( '王晓红' , '女' ,2001,20), ( '李清乐' , '男' ,1001,23), ( '赵倚天' , '男' ,3001,23); |

返回目录:开发与教程