1.navicat基本使用
连接到mysql
![在这里插入图片描述](https://img-blog.csdnimg.cn/797e15b869674705940e093b27c6681a.png
- 连接到mysql之后首先要创建数据库
- 用鼠标右键选择新建数据库
- 打开数据库,新建查询
看到如下结果,代表navicat连接mysql设置成功
2.sql语言中的注释
- – 行注释
- /* 注释内容 */
3.mysql常用数据类型
4.数据库中的元素
- 数据库-------database
- 表-----table
- 字段(列)-----field
- 记录(行)------record
5.创建表
- 语法:create table表名(字段名 字段类型,字段名 字段类型…)
--例1:创建表 a,字段要求:name(姓名),数据类型:varchar(字符串),长度为10
CREATE TABLE a(name varchar(10));
- 创建两个字段的表
--例2:创建表 b,字段要求:name(姓名),数据类型为varchar(字符串),长度为10;
--height(身高),数据类型为decimal(小数),一共5位,其中3位整数,2位小数。
CREATE TABLE b(
name varchar(10),
height DECIMAL(5,2)
);
CREATE TABLE c(
id int,
name VARCHAR(20),
age TINYINT UNSIGNED
);
6.插入数据
- 语法:insert into 表名 values(值,值,值);
insert into 表名 values(...);
例1.往表c插入一条记录
INSERT into c VALUES(0,'张飞',30);
- 指定字段插入,语法:insert into 表名(字段名,字段名) values(值,值);
例2.表c插入一条记录,只设置id和姓名name
INSERT into c(id, name) values(3,'曹操');
7.插入多条记录
- 多条insert语句,用分号隔开
例3.表c插入三条记录,写三条insert语句,语句之间用分号隔开
INSERT into c values(5,'周瑜',50);
INSERT into c(id, name) values(6,'鲁肃');
INSERT into c(name) values('诸葛亮');
- 一条insert插入多条记录
- 语法:insert into 表名 values (值,值),(),();
例4.表c插入多条记录,用一条insert语句,数据之间用逗号隔开
insert into c values(10,'张三',10),(11,'李四',20),(12,'王五',30);
- 一条insert指定字段插入多条记录
- 语法:insert into 表名(字段名,字段名) values(值,值)(值,值)(值,值);
insert into c(id,name) values(13,'光绪'),(14,'康熙'),(15,'雍正');
8.select查询表
- 语法: select * from 表名;
查询表的所有字段
--查询表c的所有字段
select * from c;
- 指定字段名查询
- 语法:select 字段名,字段名 from 表名;
--查询表c的id字段
select id from c;
--查询表c的id和age字段
select id,age from c;
--查询表c的所有字段,但顺序自定义
select name,id,age from c;
9.update修改数据
- 语法:update 表名 set 字段=值,字段=值 where 条件
- 如果没有where 条件 代表修改表中所有的记录
例1. 修改表c,所有人的年龄(age 字段)改为50
update c set age=50;
- 带有条件的update语句
例2.修改表从,id为2的记录,姓名(name 字段)改为狄仁杰,年龄(age 字段)改为20
update c set name='狄仁杰',age=20 where id=3;
select * from c;
修改name为刘备的记为李白
update c set name ='李白' where name ='刘备'
id大于10的记录,长一岁
update c set age=age+1 where id>10;
10.delete删除表中记录
- 语法:delete from 表名 where 条件;
例:删除表c中id为6的记录
DELETE from c where id=6;
例:删除表c中name为诸葛亮的记录
DELETE from c where name='诸葛亮';
例:删除年龄大于50的记录;
DELETE from c where age>50;
例:删除id小于3的记录;
DELETE from c where id<3;
例:删除所有记录
DELETE from c;
11.truncate table 删除表的数据
- truncate table 表名
删除表c中所有的记录
truncate table c;
12.小结
- 增 insert
- 删 delete
- 改 update
- 查 select
13.删除表
- 语法一:drop table 表名
删除表a
drop table a;
- 语法二:drop table if exists 表名
//如果表a存在就删除
drop table if exists a;
14.字段的约束
15.主键
- 主键的值不能重复
- 自增长 auto_increment (值会系统自动维护和增长)
create table d(
id int unsigned primary key auto_increment,
name varcar(10),
age int);
insert into d values (1,'张飞',30);
//插入的时候指定了id的值
insert into d (id,name,age) values (6,'曹操',30);
//不指定id的值
insert into d (name,age) values ('周瑜',30);
select * from d;
//如果不指定字段,主键自增长字段的值可以用占位符,0或者null
insert into d values (0,'康熙',30);
insert into d values (null,'溥仪',50);
DELETE from d;
truncate table d;
insert into d values(0,'张三',10);
select * from d;
16.非空约束
- 非空 not null
- 这个字段必须有值,如果没有,insert会插入失败
- 语法
create table 表名(
字段名 数据类型 not null,
...
);
create table e(
id int unsigned,
name varchar(10) not null,
age int);
insert into e values(1,'张三',20);
insert into e (id,age) values (1,20);
select * from e;
17.唯一
- 唯一unique
- 字段的约束为唯一,表示字段的值不能重复
- 语法
create table 表名(
字段名 数据类型 unique,
...
);
create table f(
id int,
name varchar(10) unique,
age int);
insert into f values(1,'张三',20);
insert into f values(2,'李四',20);
select * from f;
18.默认值
- 默认值default
- 当一个字段有默认值约束,插入数据时,如果指定了值,那么默认值无效,如无指定值,会使用默认值
语法:
create table 表名(
字段名 数据类型 default 值,
...
);
create table g(
id int,
name varchar(10),
age int default 30);
insert into g values(1,'张三',20,);
--插入的时候不指定age的值
insert into g (id,name) values(2,'李四');
select * from g;
)
查询student中所有字段
select * from students;
查询某个字段
select name,sex from students;
1.字段的别名
- 通过 字段名 as 别名的语法,可以给字段起一个别名,别名可以是中文
- as 可以省略
- 字段名 as 别名和 字段名 别名 结果一样
--通过as 给字段起一个别名
select card as 身份证,name as 姓名,sex as 性别 from students;
--别名的as可以省略
select card 身份证,name 姓名,sex 性别 from students;
2.表的别名
- 通过表名 as 别名 可以给表起一个别名
- as可以省略
--通过as 给表students起一个别名
select * from students as stu;
--可以省略as
select * from students stu;
3.distinct过滤重复记录
- 通过select distinct 字段名,字段名 from 表名来过滤select查询结果中的重复记录
select distinct sex,class from students;