建表

  1. -- 切换数据库
  2. use practise;
  3. -- 创建表
  4. CREATE TABLE `student` (
  5. `id` bigint(64) NOT NULL COMMENT '学生id',
  6. `name` varchar(255) DEFAULT NULL COMMENT '学生姓名',
  7. `sex` char(1) DEFAULT NULL COMMENT '性别',
  8. `age` int(3) DEFAULT NULL COMMENT '年龄',
  9. `address` varchar(255) DEFAULT NULL COMMENT '地址',
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB DEFAULT CHARSET=utf8

插入数据
关键字 insert into

  1. --插入单条数据
  2. insert into 表名 (字段名1,字段名2,字段名3,..........) value(值1,值2,值3,........)
  3. --插入多条数据
  4. insert into 表名 (字段名1,字段名2,字段名3,..........) values(值1,值2,值3,........),(值1,值2,值3,........),(值1,值2,值3,........),(值1,值2,值3,........),................

例子

  1. score表中插入一条成绩,s_id 08,c_id 03,s_score 80
  2. insert into score (s_id,c_id,s_score) value ('08','03',80);
  3. score表中插入多条成绩,s_id 08,c_id 03,s_score 80
  4. insert into score (s_id,c_id,s_score) values ('08','04',100),('09','03',90),('10','05',80);

数据类型讲解

数字
  • 整数 int
  • 小数(浮点数)
    float
    double
    decimal

    字符串
  • varchar 变长字符串

  • char 定长字符串
  • text 长文本

    时间日期
  • datetime 日期时间

  • date 日期
  • time 时间
  • timestamp 时间戳

    布尔值
  • true

  • false

    空值
  • null

    字段约束

  • 主键约束(primary key) —— 非空且唯一

  • 外键约束(foreign key)—— 必须在关联主键内存在
  • 非空约束(not null) —— 非空
  • 唯一约束 (unique) —— 唯一
  • 无约束 (null) —— 无任何限制