1.增删改查
创建数据库database2020;
create database database2020;
创建一个最简单的表。
create table tr(a int );
create table student(stu int no null auto_invrement(自动增长) ,primary key(stu));创建主键。
查询student表的信息
select * from student;
插入一条数据,表(属性)+(值)insert into table ()values();
insert into t_user (age,name)values(12,‘waner’);
更新数据,stuid=1的记录,改age=100
update table set age=10 where stuid=1 ;
update student set age=100 where stuid=1;
更新数据,stuid=2的记录,改name=‘ligang’ ,age=10
update table set name=‘I love you’ where stuid=2;
update student set name=‘ligang’ ,age=10 where stuid=2;
删除stuid=2的记录
delete from student where stuid=2;
2.多表关联
主键表是一个,外键与多个(一对多的关联)
设置主键:Primary key(teaid)
设置外键:foreign key(teaid) references teacher2(teaid)
多表之间的关联是通过关联的字段来实现的。两种方式:
弱关联:没有设置外键。用于互联网上
强关联:设置外键。企业内部
3.索引
用索引提高查询的速度,利用索引提高数据库的性能 。(字典效果)
Create index 索引名 on 表名
drop index 索引名 on 表名
create index index1 on teacher(name);
drop index index1 on teacher1
create unique index 索引名 on 表名(字段名)不允许索引的字段值重复
场景:
数据量大,查询多。