java 基础做增删改查教学_Java学习笔记34(sql基础 :增删改查1)
create database qy97;/*创建数据库*/
use qy97; /*使用数据库 use 数据库名*/show tables;/*查看所有的表*/
select database();/*查看当前所在的数据库*/
/*==================================================
1.创建表*/
/*创建表stu*/
/*创建表格式:
create table 表名(
列名1 数据类型 约束 ,
列名2 数据类型 约束,
列名3 数据类型 约束
)
PRIMARY KEY 设置为主键,确保数据的唯一性*/
/*创建表stu,列名1为id,列名2为name,并设置id为主键
设置为主键的列数据不能为空*/
create tablestu(
idint primary keyauto_increment,/*primary key auto_increment这句话的意思是id设置为主键,并实现自动增长*/namevarchar(50)
);desc stu;/*查看表的结构*/
drop table stu;/*删除数据表*/
/*===================================================
2.修改表结构*/
create tableusers(
idint primary keyauto_increment,
namevarchar(50),
addressvarchar(50)
);descusers;/*添加列
alter table 表名 add 列名 数据类型 约束*/
alter table users add sex varchar(20);/*修改列(在原有的列上修改属性,修改列的数据类型 约束)
alter table 表名 modify 修改的列名 数据类型 约束*/
alter table users modify sex int;descusers;/*修改列名
alter talbe 表名 change 旧列名 新列名 数据类型 约束*/
alter table users change sex password int;descusers;/*修改表:删除列
alter table 表名 drop 要删除的列名*/
alter table users droppassword;descusers;/*修改表名
rename table 旧表名 to 新表名;*/renametable users tostudent;
show tables;/*=====================================================
3.修改表中数据*/
/*向表中添加数据*/
create tableshop(
# 设置主键自动增长
idint primary keyauto_increment,
# 设置约束为非空
namevarchar(50) not null,
pricedouble);/*向表中加入数据
insert into 表名(列名1,列名2,列名3) value (值1,值2,值3);
应注意值与列数据类型,位置对应*/
insert into shop (id,name,price) value (1,'小米',999.99);insert into shop (id,name,price) value (2,'华为',989.89);/*当表中的每一列都给出数据时,可以不写列名
insert into 表名 value (值1,值2,...);*/
insert into shop value (3,'电脑',5555.55);/*因为表中id设置为主键自动增长,因此不写主键的值时,会自动增加
添加的数据值会与前面的列名匹配,当没有列没有设置约束非空,也没有添加值时,默认为null*/
insert into shop(name, price) value('电视',33.3);/*向表中批量添加多组数据
insert into 表名(列名...) values
(值...),
(值...),
(值...);*/
insert into shop(name,price) values('冰箱',43.44),/*这里id自动增长*/('空调',643.9);/*修改表中数据
update 表名 set 列名=值 where 条件
where 条件 数据中的唯一性*/
update shop set price=50 where id=3;update shop set name='洗衣粉',price=5 where id=5;/*修该表中数据条件的写法
id=6; 等于 不要写==
id<>6;不等于
id>=6;
与或非&|!
&&要写成and
||要写成or
!要写成not
id in(1,3,5,7);包含*/
update shop set price=3000 where id=2 or id=6;update shop set name='康帅博' where id in(1,4,7,5,6);/*删除表中数据
delete from 表名 where 条件
drop table 表名 删除整个数据表
还有一个truncate方法也是删除,区别在于删除后重新建表自增重新开始,而delete方法不影响自增*/
delete from shop where id=8;drop tableshop;/*=================================================
4.查询表中数据*/show tables;insert into student values(1,'张','河南'),
(2,'赵','郑州'),
(3,'王','洛阳'),
(4,'李','洛阳');insert into student(id,name) value (5,'孙');insert into student(id,name) value (6,'胡');insert into student value (7,'sun','南阳');/*查询表中所有数据
select * from 表名*/
select * fromstudent;/*查询表中指定条件的数据
select 列名 from 表名 where 条件*/
select name from student where id=2;select name,id from student where address='洛阳';/*查询去掉重复记录
DISTINCT 关键字 跟随列名*/
select distinct address fromstudent;/*查询列并对结果集重新命名,只是对结果集的列名重新命名,表中的列名并不改变
select 列名 AS '新列名' from 表名; 注意新列名要加''*/
select name AS '姓名' fromstudent;select * fromstudent;/*查询数据中直接对结果进行结果计算*/
select id+100 as 'sum' from student where name='张';/*查询满足条件的所有数据*/
select * from student where address='洛阳';/*查询id在2-4之间的数据*/
select * from student where id>=2 and id<=4;/*上面语句还可以用between来查询*/
select * from student where id between 2 and 4;/*查询id不是3的名字*/
select name from student where id !=3;/*查询id在1,2,4中任意一个的所有信息*/
select * from student where id=1 or id=2 or id=4;/*上面语句用in语句*/
select * from student where id in (1,2,4);/*like模糊查询 配合通配符
只要地址有洛字的都查出来*/
select * from student where address like '%阳%';/*查询名字 三个字符的*/
select * from student where name like'___';/*插询地址不为空的所有信息*/
select * from student where address is not null;
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
