###mysql day02
图形界面工具:Navicat
dos 窗口登录命令(配置好环境变量)
mysql -u root -proot
新建数据库的命令:
create database temp
查看当前数据库下的所有数据库:
show databases;
使用数据库:
use temp;
查看当前数据库下的所有表格:
show tables;
查看建表语句:
show create table stu;
>>建表语句:
CREATE TABLE `stu` (
`id` int(11) NOT NULL,
`name` varchar(20) NOT NULL,
`age` smallint(6) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
###增删改查
插入数据:
工具插入
sql 命令插入:
insert into stu values(7,'Kim',10);
###查询语句:
1 查询表中所有数据:
select * from 表名;
SELECT * FROM `stu`;
2 查询某一行记录:
-1 查询id为1的记录 需要加入条件:where子句
select * from stu where id = 1 ;
-2 查询name为'John'的记录
select * from stu where name = 'John' ;
-3 查询年龄为12岁的学生信息
select * from stu where age = 12 ;
-4 查询年龄大于10岁的学员信息
比较运算符:
= 判断是否相等
>
<
>=
<=
!=
select * from stu where age > 10 ;
-5 查询出年龄大于10岁并且小于12岁的学生姓名
select * from stu where age > 10
and age < 12 ;
--select * from stu where 10 < age <12 ; 错误 的
逻辑运算符: 与 或 非
数据库中 与 并且的意思 用and来表示
假设有两个条件t1 和t2 ,它们两个同时满足,结果才能发生,那么这两个条件就是一种与的关系 串联
或 或者 用 or 来表示
假设有两个条件t1 和t2 ,它们两个只要满足其中之一,结果就会发生,那么这两个条件就是一种或的关系
--查询出年龄等于11岁或者10岁的学生信息
select * from stu where age = 10 or age = 11;
非 不是 not
假如条件不成立,则结果出现,提交处理.结果不出现,那么我们就说提交和结果之间是一种非的关系
--查询出年龄不等于11岁的学生信息
select * from stu where age != 11 ;
select * from stu where not age = 11 ;
与或非是用来连接条件表达式
条件表达式:
a > 10 a = 9
条件查询:根据条件过滤原始的表数据,查询到想要的数据
语法:
select */(字段1,字段2,字段3...)
from 表名
where 条件 ;
查询出年龄为11岁的学员的姓名和学号
select name , id
from stu
where age = 11 ;
15975216886
edge的IT空间
>>>新建student表:
CREATE TABLE `NewTable` (
`id` int NOT NULL ,
`name` varchar(20) NOT NULL ,
`age` int NULL ,
`addr` varchar(200) NOT NULL ,
`phone` varchar(11) NOT NULL ,
`email` varchar(50) NULL COMMENT '电子邮件' ,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8
;
--查询表中所有数据
select * from student;
--查询表中年龄在20到30之间的学员姓名 and
select name from student
where age >=20 and age <=30 ;
between ... and
在...之间
select name
from student
where age between 20 and 30 ;
--查询家住在花果山的学员姓名
select name from student
where addr = '花果山';
--更改字段名的命令
alter table student change column addr
address varchar(50);
--查看表结构:查看表中的字段,数据类型以及一些约束
desc student ;
--查询家住在花果山,高老庄,常山的学员姓名 or
select name from student
where address = '花果山'
or address = '高老庄' or address = '常山';
-- in 关键字 在...里面
select name
from student
where address in('花果山','高老庄','常山');
--查询出姓名为马六的学员的年龄,住址
select age,address
from student
where name = '马六';
--怎么样判断空值
查询出email为空的学员姓名
select name
from student
where email = null ;
使用is null 来判断
select name
from student
where email is null ;
查询出age为空的学员姓名
select name from student where age is null;
--模糊匹配
like 像... [正则表达式]
% 通配符 0到多个任意字符
_ 1个任意字符
--查询邮箱包含163的学生的姓名,年龄
select name,age
from student
where email like '%163%';
--查询出名字包含周的学员姓名,地址
select name,age
from student
where name like '%周%';
--查询邮箱包含tom的学生的姓名,年龄
--起别名
select name '姓名',age '年龄'
from student
where email like '%@tom%';
--查询名字包含'空'的学生姓名,年龄
select name as '姓名',age as '年龄'
from student
where name like '%空%';
* 在查询中代表所有字段
--查询指定字段
1 查询出id为2的学生姓名 name
select name from stu where id = 2 ;
2 查询出所有人的姓名
select name from stu;
--修改字段值得操作:
update student set phone = '13999999999'
where id = 7 ;
--把猪八戒的名字改成二师兄
update student set name = '二师兄'
where id = 7 ;
文章转载自edge的IT空间,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




