MYSQL常用增删改查命令
– 数据库的操作
-- 数据库的链接
1.mysql -uroot -p
2.mysql -uroot -p123456
-- 数据库的退出
1.quit/exit/ctrl+d
-- 查看数据库
show databases;
-- 查看数据库版本
select version();
-- 显示时间
show now();
-- 创建数据库
create database name;
# 如果没有指定编码,会默认使用拉丁文
# 指定编码的方法
create database charset = utf8;
-- 查看创建数据库的语句
show create database python01;
-- 删除数据库
# 物理删除
drop database python01
-- 删除数据表
delete from students;
# 逻辑删除
-- alter table students add is_delete bit default 0;
updata students set is_delete=1 where id=11;
-- 使用数据库
use python01;
-- 查看当前数据库中所有表
show tables
-- 创建数据表
create tables;
# create table students(id int unsigned primary key not null auto_increment,
name varchar(30),
age tinyint unsigned,
high decimal(5,2),
gender enum("男“,"女","中性","保密") default "保密",
cls_id
);
-- 插入数据--
insert into students values(1,"老王","18","188.88","男",0)
insert into students (name,cls_id) values("老陈",1)
-- 多行插入
insert into students values(1,"老王","18","188.88","男",0),(1,"老李","18","188.88","女",0);
-- 查询数据
select * from classes;
-- 修改数据
updata students set gender=1 #(把所有的gender都修改了一遍)
updata students set gender=1 where name="老王";
updata students set gender=1 where id=3; # (单独修改了id=3的数据)
updata students set gender=1 where
-- 查看表
desc xxxx;
select name,gender from students; # (只看nmae和gender列)
select * from students;
select * from students where id<3;#(查询id<3的所有信息)
select * from students where name="小李飞刀"; #(查询name为小李飞刀的所有信息)
select id as 序号,gender as 性别,name as 姓名 from students;
-- 查看表的创建方式
show create table students;
-- 修改表的结构
-- 添加字段
alter table students add birthday datatime;
-- 删除字段
alter table students drop high;
-- 修改字段
-- 不修改字段,修改类型和约束
alter table students modify birthday data;
-- 修改字段
alter table students change birthday birth data default "2008-1-1";