zoukankan      html  css  js  c++  java
  • 通过SQL创建一个有主键自动递增有默认值不为空有注释的表

    -- create database db_std_mgr_sys;
    // TODO schema都用db开头,表都用t开头,字段都用f开头,存储过程都用proc开头,触发器都用trig开头,函数都用func开头,这样能有效避免用关键字命名,比如班级是class,名字是name这些都是关键字(会显示蓝色的标识符) use db_std_mgr_sys;; create table t_student( f_id bigint(20) not null auto_increment, f_name varchar(10) not null default '', f_code varchar(20) not null default '' comment '学号,值唯一', f_sex varchar(8) not null default '', f_phone varchar(20) not null default '', f_school_id bigint not null default -1 comment '所在学校id', f_grade_id BIGINT not null default -1 comment '所在年级id', f_cls_id bigint not null default -1 comment '所在班级id', primary key(f_id),
    unique key uk_std_code(f_code) -- 和主键一样默认会创建索引(可以有多个列),uk_std_code是索引名
    key idx_phone(f_phone) -- 注意,unique key和key都是会生成索引,而且可以省略key后面的标识符如uk_std_code等,但是不要省略,否则对应索引的名字为第一个字段的名字;primary key的可以省略,因为它的索引名就是PRIMARY(所以explain select* xxx,的是索引名) )engine
    innodb, charset 'utf8', comment '学生表';; -- TODO 就用这种写法了,注意这里先有delimiter ;;

     表二:

    use db_std_mgr_sys;
    create table `user`(
    uid bigint not null auto_increment,
    username varchar(20) not null,
    `password` varchar(40) not null,
    phone varchar(20) not null,
    email varchar(30) not null,
    primary key (uid),
    unique key (username),
    unique key (phone),
    unique key (email)
    )engineINNODB, charset utf8;;
    /*这里要注意key后面一定要有()而不能直接是primary key uid,这里的key就是索引的意思,
    而前面的primary、unique都是修饰,主键也是索引的一种;若只有key没有修饰则表示该索引是普通索引;
    若是unique key uq_username (username),则是给这个索引命名为uq_username,否则索引名和索引的列名是一样的为username*/
  • 相关阅读:
    6th Alpha阶段的postmortem报告
    两种open()函数
    文件操作权限第一位的0是什么意思
    vi的查找与替换
    浅析Linux下的/etc/profile、/etc/bashrc、~/.bash_profile、~/.bashrc文件
    Linux命令总结:file
    Linux下/boot目录
    英语单词简记
    Linux下grep命令
    Linux下find命令
  • 原文地址:https://www.cnblogs.com/silentdoer/p/7404030.html
Copyright © 2011-2022 走看看