一、启动服务器:net start mysql
连接服务器:mysql -u root -p
关闭服务器:net stop mysql
二、查询已有数据库:show databases;
创建数据库:create database day1 default charset utf8 collate utf8_general_ci;
删除数据库:drop database day1;
进入数据库:use day1;
查看表:show tables;
创建表:create table tb1(
id int not null auto_increment key primary key, #主键(不允许为空,不能重复),自增
name varchar(16) not null, #可变长度16,不为空
email varchar(32) null, #可变长度32,可为空
age int default 3 #插入数据时,如果不给age设值,默认值3
)default charset=utf8;
三、1、查看表结构 desc tb1;
2、添加表数据 insert into tb2(name,email,age) values('x','x',19);
添加多个insert into tb1(name,email,age) values('x1','x1',19),('x2','x2',20),('x3','x3',30);
3、 查看表具体的数据 :select * from tb1;
指定查看:
select *from tb2 where id=2;
select *from tb2 where id>2 and age>20;
select *from tb2 where id>2;
select *from tb2 where id=2 or id=3;
4、删除:
delete from tb1;
delete from tb1 where id=1;
5、修改:
updata tb1 set age=10;
updata tb2 set age=10 where id=2;
四、常见SQL
1、条件
2、通配符
%代表任意字符:
%沛% :任意字符有沛可查到;%沛:以沛结尾的可查到。
一个_代表一个字符
“_@qq.com”:前面一个字符后面为@qq.com的可以找到
3、映射
Select id,name as n1 from tb1 ;把表 tb1中的name替换为n1
4、排序
asc 从小到大 ,顺序 desc倒序;
五、部分数据
select * from tb1 where id>2 order by id desc limit 3;筛选出id>2,然后按id从大到小排,再取前3条数据
从2开始 select * from tb1 limit 2 offset 2;
六、分组
把同一个年龄的聚合,每个年龄有多少人。select age, count(id) from tb1 group by age;
sum(age)总和 avg(age)平均 max(age)最大值 min(age)最小值 count(age)总个数
七、连表
主表选择不同:
不分主表:
八、表关系
1、三种结构关系
2、约束问题
九、授权
day07db.* 对数据集的任何表,内容都可以操作。