码迷,mamicode.com
首页 > 数据库 > 详细

数据库创建表

时间:2018-12-12 00:37:02      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:ima   插入数据   rop   name   nbsp   esc   desc   span   creat   

一、基本形式

create  table  [if not  exists] 表名(

        字段列表, [约束或索引列表],

        字段列表, [约束或索引列表],

        索引

        约束

) [表选项列表];

字段基本形式: 字段名   类型   [字段修饰属性]

二、字段属性设置

not  null:不为空,表示该字段不能放“null”这个值。不写,则默认是可以为空。

auto_increment:  设定int类型字段的值可以“自增长”,即其值无需“写入”,而会自动获得并增加属性必须随同 primary key  或 unique key 一起使用。

[primary] key: 设定为主键。是唯一键“加强”:也不能重复并且不能使用null,并且可以作为确定任意一行数据的“关键值”。

通常,每个表都应该有个主键,而且大多数表,喜欢使用一个id并自增长类型作为主键。

但:一个表只能设定一个主键。

 

unique  [key] : 设定为唯一键:表示该字段的所有行的值不可以重复(唯一性)。

 

default  ‘默认值’: 设定一个字段在没有插入数据的时候自动使用的值。

comment  ‘字段注释’:

三、表的增删改查

查    show tables;
删    drop table 表名;

增    create table 表名(
               id int not null auto_increment primary key comment ‘这是主键‘,
               name varchar(10) comment ‘这是姓名‘,
               sex int default 1 comment ‘这是性别 1代表男 0代表女‘,
               brithday date
        );

改   alter table 表名;

四、其它操作

显示所有表show  tables:

显示表结构desc  表名;

 

数据库创建表

标签:ima   插入数据   rop   name   nbsp   esc   desc   span   creat   

原文地址:https://www.cnblogs.com/tanchangsha/p/10105257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!