一、数据库的操作
-- 连接数据库
mysql -u root -proot -- 不推荐 密码直接展示
mysql -uroot -p
--退出数据库
exit/quit
-- sql语句最后需要有分号;结尾
-- 显示数据库版本
select version();
-- 显示时间
select now();
-- 查看所有数据库
show databases;
-- DDL(数据定义语句)
-- 创建数据库
create database python04;
create database python04 charset=utf8;
-- 查看创建数据库的语句
show create database python04; -- `` 有特殊字符 ''
-- 查看当前使用的数据库
select database();
-- 使用数据库
-- use 数据库的名字
use python04;
-- 删除数据库
drop database 数据库名;
二、数据表的操作
-- 查看当前数据库中所有表
show tables;
-- 创建表
-- auto_increment表示自动增长
-- not null 表示不能为空
-- primary key 表示主键
-- default 默认值
-- create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);
create table demo1 (id int,name varchar(30));
create table demo2(
id int primary key not null auto_increment,
name varchar(30)
);
-- 查看表结构
-- desc 数据表的名字;
desc demo2;
-- 创建students表(id、name、age、high、gender、cls_id)
create table students(
id int not null primary key auto_increment,
name varchar(30),
age tinyint unsigned default 18,
high decimal(5,2),
gender enum('男','女','保密') default '保密', -- 存数据的时候 只能存 男或者女
cls_id int
);
-- 创建classes表(id、name)
create table classes(
id int primary key not null auto_increment,
name varchar(30)
);
-- 查看表的创建语句
-- show create table 表名字;
show create table students;
-- 修改表-添加字段
-- alter table 表名 add 列名 类型;
alter table students add birthday date;
-- 修改表-修改字段:不重命名版
-- alter table 表名 modify 列名 类型及约束;
alter table students modify birthday date default '1990-1-1';
-- 修改表-修改字段:重命名版
-- alter table 表名 change 原名 新名 类型及约束;
alter table students change birthday birth date default '1990-1-1';
-- 修改表-删除字段
-- alter table 表名 drop 列名;
alter table students drop high;
-- 删除表
-- drop table 表名;
-- drop database 数据库;
三、增删改查
-- 增加
-- 全列插入
-- insert [into] 表名 values(...)
-- 向classes表中插入 一个班级 id name
insert into classes values(1,'大神班');
-- 向students表插入 一个学生信息
+--------+-------------------------------+------+-----+------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------------------------+------+-----+------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
| gender | enum('男','女','中性','保密') | YES | | 保密 | |
| cls_id | int(10) unsigned | YES | | NULL | |
| birth | date | YES | | 1997-01-01 | |
+--------+-------------------------------+------+-----+------------+----------------+
-- 主键字段 0 null default 来占位
insert into students values(0,'居然',18,'男',1,'1990-1-1'); -- id是多少?
insert into students values(null,'juran',19,'男',1,'1990-1-1');
insert into students values(default,'juran',19,'男',1,'1990-1-1');
-- 枚举类型插入 下标是从1开始的
insert into students values(default,'juran',19,1,1,'1990-1-1');
-- 部分插入
-- insert into 表名(列1,...) values(值1,...) -- 非空字段
insert into students(`gender`) values(2);
insert into students(`name`,`gender`) values('juran',2);
-- 多行插入
insert into students values
(default,'juran',19,'男',1,'1990-1-1'),
(default,'666',20,'男',1,'1990-1-1')
;
-- 修改
update 表名 set 列1=值1,列2=值2... where 条件;
-- 删除
-- 物理删除(在内存中删除)
delete from 表名 where 条件
-- 逻辑删除(不是真的删除):is_delete标识 0 为 没删除 , 1 为 已删除
select * from students where is_delete=0; --未删除,相当于隐藏了
select * from students where is_delete=1; --删除
-- 查询基本使用
select 去重选项 字段列表[as 字段名] from 数据表 where [group by 子句] [having子句] [order by 子句] [limit子句];
-- 查询所有列
select * from students;
-- 去除重复字段的查询
select distinct * from students;
-- 查询指定列
select 列1,列2,... from 表名;
-- 可以使用as为列或表指定别名
select distinct name as 名字 from students as s;