#添加字段
alter table user_info
add mobile char(11) not null comment'手机号' alter user_name;
select *from user_info
#删除字段
alter table user_info
#修改字段
alter table user_info
modify mobile char(11) default '00000000'
#删除表
drop table publisher
#截断标意味着清空表中数据
truncate table user_info
select *from user_info
#重命名
rename table user_info to users
#添加数据
insert into user_xxx (id,user_name,password,mobile)
values ('12','Tom','123456','12345678901');
#如果没有字段列表,则值列表顺序必须和表中字段一致
insert into user_xxx values ('12','Tom','123456','12345678901');
insert into user_xxx as select id,user_name,password from users;
select *from user_info
#修改数据
update users set mobile='1234567890',sex=1 where user_name='12312';
insert into name () values ()
update name set name=value,name=value,name=value,.....[where ]
delete from name [where ]
表
create table name (
name type default 约束 comment ,
name type default 约束 comment ,
)
name:字段名 一般情况下,如果有多个单词构成,用下划线间隔
type:int double(5,2) char(长度不变 UUID 手机号 身份证号) varchar(长度不确定 姓名) blob(电影,图片) clob(小说)
default 添加数据的时候如果不指定值,则使用默认值
约束:主键(不重复且不为null) unique(不重复,但可以多个null) not null foreign key(name) references table_name(name)——保证数据完整性,不会产生无用的垃圾数据、check MySQL数据不支持
alter table name
add after
modify 了解
drop name
drop table name
rename table name to new_name
truncate table name
约束
MySQL数据库通过约束(constraints)防止无效的数据进入到表中,以保护数据的实体完整性。
约束分为字段级别约束和表级别约束:
字段级约束:只为单个字段添加约束;
表级约束:为一个或多个字段添加约束
①primary key:指定主键。 不允许重复,且不为null
②auto_incremenrt:设置表字段自增长,默认从1开始
③not null:指定字段不能为空,只能定义为字段级约束;
④unique 不允许重复,可以为null
⑤foreign key:指定一个字段或字段组合作为一个外键
⑥check:指定列值必须满足check中条件,否则无法进行添加或修改;一个字段可以定义多个check约束。注意:MySQL数据库不支持该约束!