1.插入
如果是将所有的字段都插入,那么就直接使用,如果没有具体的数值也可以用null进行代替。
insert into teacher values(8,"啦啦","嗯嗯",8)
对于有限的字段就使用
insert into teacher (name,address,sid) values("啦啦2","来了",9)
对于insert和update和delete语句,可以使用low_priority,这样降低优先级,减少性能损耗、
insert low_priority into teacher (name) values("美妞1")
多条数据插入,中间用逗号隔开。
insert into teacher (name,address,sid) values("啦啦2","来了",9),("啦啦3","来了1",10)
2.修改
update teacher set name ="美妞0" where tid =9
3.删除,只能删除行,不能删除表本身。
delete from teacher where tid=12
如果想删除表中的所有的行,那么就使用 truncate 本身就是截断的意思。其实就是删除整个表然后又重新创建了新表。
truncate table teacher
4.创建表,可以在表名的前面添加if not exists这样进行判断,如果表不存在,那么就创建表。
虽然是设置not null 但是仍然能够插入成功,默认应该是空串。但是应该设置默认的插入值。
create table if not exists teacher
(tid int primary key auto_increment,
name varchar(20) not null default "",
sex varchar(20) not null,
sid int
)
5.可以进行制定引擎,一般有三种引擎
innodb 事务处理引擎,但不支持全文本搜索
memory 存储在内存中,速度很快,适合临时表。
myisam 支持全文本搜索,但不支持事务。
指定默认引擎
create table teacher (tid.....)engine = innodb
6.修改
添加列
alter table teacher add test varchar(20)
删除列
alter table teacher drop test
使用alter table定义外键,这里的ee就是约束名,可以自己取名,这时候在teacher表中必须已经存在sid字段,下面的仅仅是添加外键约束。
alter table teacher add constraint ee foreign key(sid) references student(sid)
7.删除表
drop table teacher
8.重命名表
rename table teacher to teacher1