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 table stu(
id int primary key auto_increment,
/*primary key auto_increment这句话的意思是id设置为主键,并实现自动增长*/
name varchar(50)
);
desc stu;/*查看表的结构*/
drop table stu;/*删除数据表*/
/*===================================================
2.修改表结构*/
create table users(
id int primary key auto_increment,
name varchar(50),
address varchar(50)
);
desc users;
/*添加列
alter table 表名 add 列名 数据类型 约束*/
alter table users add sex varchar(20); /*修改列(在原有的列上修改属性,修改列的数据类型 约束)
alter table 表名 modify 修改的列名 数据类型 约束*/
alter table users modify sex int;
desc users;
/*修改列名
alter talbe 表名 change 旧列名 新列名 数据类型 约束*/
alter table users change sex password int ;
desc users;
/*修改表:删除列
alter table 表名 drop 要删除的列名*/
alter table users drop password;
desc users;
/*修改表名
rename table 旧表名 to 新表名;*/
rename table users to student;
show tables;
/*=====================================================
3.修改表中数据*/
/*向表中添加数据*/
create table shop(
# 设置主键自动增长
id int primary key auto_increment,
# 设置约束为非空
name varchar(50) not null ,
price double
);
/*向表中加入数据
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 table shop;
/*=================================================
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 * from student;
/*查询表中指定条件的数据
select 列名 from 表名 where 条件*/
select name from student where id=2;
select name,id from student where address='洛阳';
/*查询去掉重复记录
DISTINCT 关键字 跟随列名*/
select distinct address from student;
/*查询列并对结果集重新命名,只是对结果集的列名重新命名,表中的列名并不改变
select 列名 AS '新列名' from 表名; 注意新列名要加''*/
select name AS '姓名' from student;
select * from student;
/*查询数据中直接对结果进行结果计算*/
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;

最新文章

  1. 【EasyUI】combotree和combobox模糊查询
  2. python通过自定义异常,提前退出方法
  3. 在SQL Server 2016里使用查询存储进行性能调优
  4. JavaScript闭包——实现
  5. 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持。
  6. NYOJ-括号配对问题 &lt;技巧性的非栈道法&gt;
  7. 开发Nagios监控passwd文件插件
  8. ios 获取当前ViewController
  9. 【伯乐在线】最值得阅读学习的 10 个 C 语言开源项目代码
  10. DockerToolbox在Win7上的安装和设置
  11. getComputedStyle与currentStyle获取样式
  12. VRRP技术总结和配置实践
  13. rancher2.0部署
  14. [PA2014]Zadanie
  15. MapRdeuce&Yarn的工作机制(YarnChild是什么)
  16. ubuntu 安装JDK1.6(jdk-6u45-linux-x64.bin)
  17. C++ map &amp; set
  18. ajax 二级联动与springmvc 交互
  19. ios开发,app调用资源文件到C++的方法
  20. 20145122《Java程序设计》第七周学习总结

热门文章

  1. Solution about MB STAR C4, MB STAR C5 Update and can not test vehicles problems
  2. Python数据分析Pandas库方法简介
  3. Vue基础进阶 之 过渡效果
  4. Docker+Jenkins+Maven+SVN搭建持续集成环境
  5. mysql 主键外键
  6. mysql常用的查询优化
  7. 第 8 章 容器网络 - 069 - Calico 的默认连通性
  8. VS Code 安装sass插件
  9. 7.7 GRASP原则七: 纯虚构 Pure Fabrication
  10. 小程序 input 组件内容显示不全(显示的长度不满 input 宽度)问题