表结构操作的SQL
语句
show tables;
create table 表名(
字段名称 数据类型 可选的约束条件,
column1 datatype contrai,
...
);
create table 表名(
id int unsigned primary key not null auto_increment,
name varchar(20) not null,
age tinyint unsigned default 0,
height decimal(5,2),
gender enum('男', '女', '保密')
UNIQUE(name));
drop table 表名;
desc 表名;
describe 表名;
alter table 表名 add 列名 类型 约束;
alter table students add birthday datatime;
alter table 表名 modify 列名 类型 约束;
alter table students modify birthday date not null;
alter table 表名 change 原列名 新列名 类型 约束;
alter table students change birthday birth datetime not null;
alter table 表名 drop 列名;
alter table students drop birthday;
show create table 表名;
- 插入数据:
- 注意:
- 主键列是自动增长,但是在全列插入时需要占位,通常使用空置(
0
或null
或者default
); - 在全列插入时,如果字段列有默认值可以使用
default
来占位,插入后的数据就是之前设置的默认值; - 插入一条数据用
value
,插入多条数据用values
,不过不论插入一条还是多条,用values
肯定没错;
insert into 表名 value(值1, 值2, ...);
insert into 表名(列名1, 列名3, ...) value(值1, 值3, ...);
insert into 表名 values(...),(...)...;
insert into 表名(列1,...) values(值1,...),(值1,...)...;
select * from 表名;
select 列名1, 列名2, ... from 表名;
- 修改数据:
- 注意:不加
where
条件就是修改整张表的所有记录;
update 表名 set 列名1=值1, 列名2=值2, ... where 条件
- 删除数据:
- 注意:
- 不加
where
条件就是修改整张表的所有记录; - 而且此操作称之为物理删除,一旦删除就不容易恢复,我们可以使用逻辑删除来解决这个问题。
delete 表名 where 条件;
alter table 表名 add 列名-isdelete bit default 0;
update 表名 set 列名-isdelete=1 where 条件;