sql:
# windows终端进入mysql
# mysql -u用户名 -p密码
mysql -uroot -proot
# 退出
exit
quit
# sql语句最后都要有分号";"结尾
# 终端查看mysql版本
select version();
# 显示当前时间
select now();
# 查询所有数据库
show databases;
# 库操作
# 创建数据库
# create database 数据库名 charset=utf8; 如果不指定编码格式 默认是latin
create database python_1 charset=utf8;
# 查看创建数据库的语句
# show create database 数据库名
show create database python_1;
# 查看当前使用的数据库
select database();
# 使用数据库
# use 数据库名
use python_1;
# 删除数据库
# drop database 数据库名;
drop database python_1;
# 如果有"-"连接的数据库名要用``括起来 ` tab键上方符号
drop database `python-1`;
# 表操作
# 查看所有数据表
show tables;
# 创建数据表
# auto_increment表示自动增长
# not null 表示不能为空
# primary key 表示主键
# default 默认值
# create table 数据表名 (字段 类型 约束 下一个约束 下一个约束, # 约束 unsigned/auto_increment/primary key not null/default ''
# 字段 类型 约束],
# 字段 类型 约束 # 最后一个没有逗号,写了就报错
# );
create table user(
id int unsigned auto_increment primary key not null,
name varchar(20) default '',
age tinyint unsigned default 18
);
# 查看表结构
# desc 数据表名
desc user;
# 查看表的创建语句
# 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;
# 删除字段
# alter table 表名 drop 字段名;
alter table students drop high;
# 删除表
# drop table 表名;
drop table test;
# 数据-增删改查
# 增加
# 全列插入
# insert into 表名 values(字段1数据,字段2数据...);
# 主键字段 可以用 0 null default 来占位
insert into students values(0,"laowang",18,188.88,"W",0);
insert into students values(default,"laowang",18,188.88,"W",0);
# enum枚举中下标从1开始 可以用设置的性别字符 也可以用下标插入数据
insert into students values(null,"laowang",18,188.88,2,0);
# 部分插入
# insert into 表名(字段1,...) values(值1,...);
insert into students(name,gender) values("xiaoqiao",2);
# 多行插入
# 部分多行插入
# insert into 表名(字段,...) values(第一个数据值1,...),(第二个数据值1,...),...(第N个数据值1,...);
insert into students(name,gender) values("diaochan",2),("daqiao",2),("xishi",2);
# 全列多行插入
# insert into 表名 values(字段1数据,字段2数据...),(字段1,字段2,...),....(字段1,字段2,....);
insert into students values(null,"laowang1",18,2,1,"1991-01-01"),(0,"laowang2",18,1,2,"1990-01-01");
# 修改
# update 表名 set 字段1=值1,字段2=值2... where 条件;
# 注意: 一定要有条件,不然会全部改变
update students set name="xiaowang1",age=22 where name="xiaowang";
# 删除
# 物理删除
# delete from 表名 where 条件;
# 注意:一定要有条件 不然等于删库
delete from students where name="laowang";
# 逻辑删除
# 用一个字段表示 这条信息是否还能使用----推荐
# 给students表添加一个is_del字段 bit 类型
alter table students add is_del bit default 0;
# 逻辑删除一个数据
update students set is_del=1 where name="laowang2";
# 查看表数据
# 查询全部数据
# select * from 数据表名; # * 代表所有字段
select * from students;
# 指定条件查询
# select * from 表名 where 条件;
select * from studens where name="laowang";
# 指定字段查询
# select 要查的字段1,要查的字段2, from 数据表名;
select name,gender from students;
# 可以使用as为列或数据表名指定别名
# select 字段(as 别名),字段2(as 别名2) from 数据表 where 条件;
select name as XM,gender as XB from students;