数据表操作(三)

一、数据表的操作

1、创建表

create table 表名(
    列名  类型  是否可以为空,
    列名  类型  是否可以为空
   )ENGINE=InnoDB DEFAULT CHARSET=utf8

2、清空表

delete from 表名
truncate table 表名

3、删除表

drop table 表名

二、数据表约束

1、可空约束

not null    - 不可空
null        - 可空

2、默认值约束

create table tb1(
nid int not null defalut 2,
num int not null
)

3、主键约束

    #一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。
            create table tb1(
                nid int not null auto_increment primary key,
                num int null
            )
            或
            create table tb1(
                nid int not null,
                num int not null,
                primary key(nid,num)
            )

4、外键约束

   #一种特殊的索引,只能是指定内容
            creat table color(
                nid int not null primary key,
                name char(16) not null
            )

            create table fruit(
                nid int not null primary key,
                smt char(32) null ,
                color_id int not null,
                constraint fk_cc foreign key (color_id) references color(nid)
            )

三、数据表操作

1、增加数据

insert into 表 (列名1,列名2...) values (值1,值2,值3...)
insert into 表 (列名1,列名2...) values (值1,值2,值3...),(值1,值2,值3...)
insert into 表 (列名1,列名2...) select (列名1,列名2...) from 表

2、删除数据

delete from 表
delete from 表 where id=1 and name='alex'

3、修改数据

update 表 set name = 'alex' where id>1

  

  

 

猜你喜欢

转载自www.cnblogs.com/cloud-sj/p/10949426.html