数据库的操作
- 连接数据库
mysql -uroot -p
mysql -uroot -p824750
- 退出数据库
quit
exit
ctrl+d
- sql语句最后需要有分号;结尾
- 显示时间
select now();
- 显示版本
select version();
- 查看所有数据库
show databases;
- 创建数据库
create database 数据库名 charset=utf8;create database python4; create database python4new charset=utf8;
- 查看创建数据库的语句
show create database 数据库名;show create database python4;
- 查看当前使用的数据库
select database();
- 使用数据库
use 数据库名;use python4new;
- 删除数据库
drop database 数据库名;drop database python4; drop database `python-04`; # ‘为tab上面的键
数据表操作
-
查看当前数据库中所有的表
show tables;
-
创建数据表
- auto_increment: 表示自动增长
- not null: 表示不能为空
- primary key: 表示主键
- default: 默认值
- create table: 数据表名(字段 类型 约束[,字段 类型 约束])
create table xxxxx(id int, name varchar(30)); create table yyyyy(id int primary key not null auto_increment, name varchar(30)); create table zzzzz( id int primary key not null auto_increment, name varchar(30) );
创建students表(id name age high gender cls_id)
--创建students表(id name age high gender cls_id) create table students( id int unsigned not null auto_increment primary key, name varchar(30), age tinyint unsigned, high decimal(5,2), gender enum("男","女性","中性","保密") default "保密", cls_id int unsigned ); --创建班级表 create table classes( id int unsigned auto_increment primary key not null, name varchar(10) );
-
desc 查看数据表结构
desc 数据表名
desc xxxxx;
-
查看表的创建语句
show create table 表名show create table students
-
修改表;
- 添加字段
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 default "1990--01-01";
- 添加字段
-
删除字段
alter table 表名 drop 列名;alter table students drop high;
-
删除表
drop table 表名;
drop table xxxxx
数据操作
数据的增删改查(crud)
-
增加
全列插入
insert [into] 表名 values(…)
主键字段可以用 0 NULL default 来占位--向classes表中查入一个班级 insert into classes values(0,"菜鸟"); --向students表插入一个学生信息 +--------+-------------------------------------+------+-----+------------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------------------------------+------+-----+------------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(30) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | | high | decimal(5,2) | YES | | NULL | | | gender | enum('男','女','中性','保密') | YES | | 保密 | | | cls_id | int(10) unsigned | YES | | NULL | | | birth | date | YES | | 1990-01-01 | | +--------+-------------------------------------+------+-----+------------+----------------+ insert into students values(0,"小李飞刀",20,155,"男",2123,default); insert into students values(null,"小李飞刀",20,155,"男",2123,default); insert into students values(default,"小李飞刀",20,155,"男",2123,default); insert into students values(0,"小李飞刀",20,155,3,2123,default);
部分插入
insert into 表名(列1,…) values(值1,)insert into students(name,gender) values("小乔",2);
多行插入
insert into students(name,gender) values(("小乔",2),("大乔",2); insert into students values(default,"西施",20,155,2,2123,default),(default,"王昭君",20,155,2,2123,default);
-
修改
update 表名 set 列1=值1,列2=值2… where 条件update students set gender=1; --全部修改 update students set gender=1 where name="小李飞刀"; update students set gender=1 where id=3; update students set gender=1,age=22 where id=3;
-
删除
物理删除(慎用)
delete from 表名 where 条件delete from students; --删除整个数据表示 delete from students where name="小李飞刀";
逻辑删除
用一个字段来表示这条信息是否被已经不能再使用了--给students表添加一个is_delete字段 bit类型 alter table students add is_delete bit default 0; update students set is_delete=1 where id=6;
-
查询的基本用法
查询所有列
select * from 表名;select * from students; select * from students where name="小李飞刀"; select * from students where id>3;
查询指定列
select 列1,列2,… from 表名 where …;select name,gender from students;
可以使用as为列或表指定别名
select 字段1[as 别名1],字段2[as 别名2],…表名 where …;select name as 姓名,gender as 性别 from students;
字段的顺序
select 字段2[as 别名2],字段1[as 别名1],…表名 where …;select gender as 性别,name as 姓名 from students; select id as 序号,gender as 性别,name as 姓名 from students;