mysql 0基础 入门
mysql基础(持续更新中)
1.创建名为 tc 的数据库 create database tc
如果数据库不存在则创建,存在则不创建
create database if not exists tc
2.删除数据库 drop database <数据库名>
3.选择数据库 use <数据库名>
4.创建表格
create table if not exists student(
id int not null,
name varchar(10) NOT NULL,
age int NOT NULL,
email varchar(10) not null,
primary key (id)
)
5.删除表格 drop table <表名>
6.向表中插入数据
insert into <表名> (属性1,属性2,……,属性n)
values(value 1,value 2,……,value n)
例如:
insert into student(id,name,age,email)
values(1,"张三",18,"123@qq.com")
7.查询 :select * from student
8.where 字句 select * from student where name = "张三"
select * from student where name = "张三" or name ="李四"
9.update 更新语句 update student set name = "apple " where id = 3
修改多个属性值, update student set age = 22,email = "666@qq.cn" where id = 2
10.delete 删除语句 delete from student where id =4
(delete用于删除一整行数据)
注意:delete 后面不跟 * 号,delete from 表名 如果不跟where条件子句 则删除 整个表中数据
11.like 子句 like 匹配/模糊匹配,会与 % 和 _结合使用
注: % 表示任意 0 个或多个字符。可匹配任意类型和长度的字符
_表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句
select * from student where email like "%qq.com"
12.union 语句
select id,age from student where email like "%qq.com"
select id,age from student where id = 2
使用union后
select id,age from student where email like "%qq.com"
UNION
select id,age from student where id = 2
注:union 连接 的二个查询语句,查询的属性需要相同,不能不相同
13.降序 desc (从大到小)
select * from student order by age desc
升序 asc (从小到大) 系统缺省默认asc排序
14.分组 group 分组
例:查询表中男女性别之和:select sex,sum(age) 性别年龄总和 FROM student group by sex
15.limit 分页
查询表中前2行 数据 select * from student limit 2
查询表中2,3行数据 select * from student limit 1,2
(offset,rows)offset 元素偏移量,rows行数,offset默认为 0
limit 2 等价于 limit 0,2
16.增加一列(即一个属性)alter table student add sex varchar(4)
增加用add
17.删除1列 (即一个属性) alter table student drop COLUMN sex
注:删除一列 用 drop column