Mysql数据库day1
mysql 数据库的操作
-- 链接数据库
sudo mysql -uroot -p123456
-- 退出数据库
quit exit ctrl + d
-- 显示时间
show databases;
-- 创建数据库(python_db)
-- create database 数据库名 charset=utf8;
create database python_db;
-- 指定编码的数据库创建
create database python_db charset=utf8;
-- 使用数据库
-- use 数据库的名字
use python_db;
-- 查看当前使用的数据库
select database();
-- 删除数据库
-- drop database 数据库名;
drop database python_db;
数据表的操作
- 查看当前数据库中所有表
show tables;
-- 创建表
-- int unsigned 无符号整形
-- auto_increment 表示自动增长
-- not null 表示不能为空
-- primary key 表示主键
-- default 默认值
-- create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);
-- 创建 classes 表(id、name)
create table classes( id int unsigned primary key auto_increment not null, name varchar(20) not null);
-- 查看表结构
-- desc 数据表的名字;
desc table;
-- 创建 students 表(id、name、age、high (decimal)、gender (enum)、cls_id)
create table students( id int unsigned primary key auto_increment not null, name varchar(20) not null, age tinyint unsigned default 0, high decimal(5,2), gender enum('男', '女', '中性', '保密'), cls_id int unsigned );
-- 修改表-添加字段 生日 datetime
-- alter table 表名 add 列名 类型;
alter table students add birthday datetime;
-- 修改表-修改字段:不重命名版
-- alter table 表名 modify 列名 类型及约束;
alter table students modify birthday date;
-- 修改表-修改字段:重命名版
-- alter table 表名 change 原名 新名 类型及约束;
alter table students change birthday birth date;
-- 修改表-删除字段
-- alter table 表名 drop 列名;
alter table students drop birth;
-- 查看表的创建语句
-- show create table 表名字;
show create table students;
-- 删除表
-- drop table 表名;
-- drop database 数据库;
drop table students;
增删改查(curd)
-- 全列插入
-- insert [into] 表名 values(...)
-- 主键字段 可以用 0 null default 来占位
-- 向classes表中插入 一个班级
insert into classes values(0, "python_01");
-- 部分插入
-- insert into 表名(列1,...) values(值1,...)
insert into students values(0, "小明", 28, 175.12, '男', 1); insert into students(name, age) values("小芳", 20);
-- 多行插入
insert into students(name, age) values("张三", 21),("李四", 22); insert into students values(0, "马六", 23, 170.12, '男', 1),(0, "王五", 22, 177.12, '男', 1);
-- 修改
-- update 表名 set 列1=值1,列2=值2... where 条件;
-- 全部修改
update students set age=19;
-- 按条件修改
update students set age=29 where id=1;
-- 查询基本使用
-- 查询所有列
-- select * from 表名;
select * from students;
---定条件查询
-- 查询指定列
-- select 列1,列2,... from 表名;
select id,name from students;
-- 可以使用as为列或表指定别名
-- select 字段[as 别名] , 字段[as 别名] from 数据表;
select id as 序号, name as 姓名 from students;
-- 删除
-- 物理删除
-- delete from 表名 where 条件
delete from students where id=6;
-- 逻辑删除
-- 用一个字段来表示 这条信息是否已经不能再使用了
-- 给students表添加一个 is_delete 字段 bit 类型 默认为0
alter table students add is_delete bit default 0;
-- bit 类型,智能保存 0 或者 1
-- is_delete = 1 逻辑删除
update students set is_delete=1 where id=5;
-- 查询练习
-- 查询所有字段
-- select * from 表名;
select * from classes;
-- 查询指定字段
-- select 列1,列2,... from 表名;
select id,name from students;
-- 使用 as 给字段起别名
-- select 字段 as 名字.... from 表名;
select students.id, students.name from students;
-- 可以通过 as 给表起别名
-- select 别名.字段 .... from 表名 as 别名;
select s.id, s.name from students as s;
-- 消除重复行(查性别)
-- distinct 字段
select distinct gender from students;