创建表的语句:
create table 创建一个表
注意:创建表的时候,可以给约束,也可以不给.
insert into: 插入语句
insert into 第一种插入方式:给所有字段添加值
insert into wuhan15(id,name,phone,age,time)
values(2,"niuyi",13500000000,18.888,"2022-04-28");
第二种insert into:插入值的方式
insert into wuhan15 values()
注意:这种插入值的方式,需要填上所有字段的值
第三种:插指定字段的值 只想给time 字段增加值
insert into wuhan15(time)values("1990-01-30")
约束:
create table wuhan15(id int(4)primary key auto_
increment,name char(4),phone bigint(10)default
15602989612,age float(10,2)not null,time date);
主键约束: primary key
在一个表当中只能给一个字段添加主键约束,他是唯一
的,被约束的字段,在进行增加值的时候,值不能重复
自增长约束: auto_increment
在我插入数据,没有给当前字段值的时候,如果改值设置
了自增长约束,他会自己默认增加
注意:自增长只能针对数值number类型进行添加使用
非空约束: not null
但前没有给值,他的数据值不能为null
默认值约束: default
给了默认值约束时,添加数据的时候没有给这个字段值
他会使用我创建表时给的默认值属性,如果给了新的属
性值,就不会使用默认值
number 数值 字符串类型
(4)是我实际存储的字节大小. 1个字的大小是8bit
8bit是二进制数.
我们现在操作的是10进制,在存储的时候会将10进制转换成
2进制数进行存储.
mysql 当中删除表的方法:
drop table : 删除表
mysql当中针对表结构的操作:
首先创建一个表:
create table
针对表结构操作的语法:
修改表增加表内容:alter table :
我想要更改 wuhan15这张表的名称为:dcs
rename:是用来修改表名称的.
alter table wuhan15 rename dcs;
针对表结构增加字段:
add 增加表字段:
alter table wuhan15 add age int(4);
alter table wuhan15 add phone bigint(20)default 13500000000;
not null
针对表结构字段进行修改:
changge 修改字段结构:
我要将wuhan15 这个表中的id 修改成s_id同时增加主键约束.
alter table wuhan15 change id s_id int(8)primary key;
alter table wuhan15 change name name char(20);
增加自增长: #必须该字段有主键约束才可以增加自增长约束
alter table wuhan15 change id id int(4)auto_increment;
删除自增长:
alter table wuhan15 change id id int(4);
增加主键约束,你当前表中如果存在的值可能无法创建.
drop的用法:
drop可以删除表字段,也是删除主键约束.
alter table wuhan15 drop age;
想要想要删除两个字段.
alter table wuhan15 drop age,drop phone;
删除主键约束?
alter table wuhan15 drop primary key;
增加主键?
change
alter table wuhan15 change id id int(4)primary key;
modify : 用来调整字段的顺序:
把name字段放在第一个.
alter table wuhan15 modify name varchar(20)first;
将新添加的age放在第一个位置
alter table wuhan15 add age int(4)first;
first将字段放在第一位:
after将字段放在指定字段后面:
将age放在 id的后方:
alter table wuhan15 modify age int(4)after id;
create table
drop 针对表结构操作
alter table 针对表结构操作的语法.
针对表数据操作的语法:
insert into (插入数据)针对表数据进行操作
insert into 一次性插入多条数据
select (查询数据)
select * from wuhan;
*表示查询所有信息
只想要查询 id字段
select id from wuhan;
select id,phone wuhan;
where 条件 (比较运算符: = 等于)
1.(= 等于)
需求:我想查询id=6的整行信息?
where 条件语句.
select * from wuhan where id = 6
我想查询id=6的name 名称?
2.(!=不等于)
查询id不等于6的所有信息
select * from wuhan where id != 6;
查询 id 不等于6 只想要name 和 time信息 怎么去查询?
select name,time from wuhan where id !=6;
3.(大于>)
查询所有id 大于3信息
select * from wuhan where id > 3;
4.(大于<)
查询所有id 大于3信息
select * from wuhan where id < 3;
5.大于等于: >=
select * from wuhan where id >= 3;
6小于等于 :
select * from wuhan where id <= 3;
( = != > < >= <=)
7.or 与
查询出 id=3 以及id = 4
查询id 34 所有内容信息
select * from wuhan where id=3 or id = 4;
select name,time from wuhan where id=3 or id = 4;
8.and
想要查询出来 phone = 156029896 且 id = 1 这行的所有
内容
select * from wuhan where phone = 15602989612
and id = 1;
phone 条件
id 条件 一旦用上 and 这个条件. 这两个条件(id,phone)
必须都满足才能达到我筛选条件.
查询区间指定的内容:and 进行筛选
select * from wuhan WHERE id >=2 and id <= 5;
select * from wuhan WHERE id >1 and id < 6;
第二种方法:between
select * from wuhan where id between 2 and 5;
小练习:
有个需求:我需要查找出,id =3 phone 为15602989612
的name字段.
select name from wuhan where id = 3 and phone = 15602989612
我要在当前这个表当中查询出 日期为 2022-05-01 信息
只需要显示这个日期就可以
select time from wuhan where name = "niuba";
select time from wuhan where time = "2022-05-01";
小练习:
"null" 也是具体的一个属性,不要用 比较运算符去操作
他 = !=
查询 为null 的属性
is null
select * from wuhan where name is null;
is not null :查询不为 null 的属性
select * from wuhan where name is not null;
in : 就是符合条件的
id 这个字段包含 1,2,3,4,5,7
1 in(id) :查询当前的值在不在这个 id当中.
如果在就显示该值
1 给的值, in id 系统会根据我实际给1 在id 这个字段
去匹配相对等的值.
in 找符合的条件:
select * from wuhan where 7 in(id);
not in 找不在这个集合当中内容
不符合的.
select * from wuhan where id not in(1,3,5,7);
工作当中c:怎么结合数据库来验证数据测试?
1.我们平台系统当中的数据都会存储数据库当中表
比如我们淘宝页面展示的商品这些商品都是存储在
数据库表当中,在我们数据库当中去插入数据,看前
台页面进行展示.
2.我们在页面上操作,操作完成之后生成的数据,又没
有写入到数据库当中的表.
|
|