安装mysql
yum -y install mysql mysql-server mysql-devel
原生的msql没有命令补齐,用起来很难受。装下面的包然后使用mycli登录就有命令补齐功能了!!!
pip3 install mycli
设置mysql root账户远程访问权限_mysq中lroot远程访问没有权限-CSDN博客
登录数据库:
mycli -u root -p root密码
基本操作:
- 查看当前系统下的数据库:show databases;
- 创建数据库:create 库名;
- 切换使用数据库:use 库名;
- 查看表:show tables;
- 建表:create table 表名 (字段名+空格+数据类型);
- 查看表结构:desc 表名;
- 删除表:drop table 表名;
create test;
创建数据库叫做test
use test;
切换数据库test
show tables;
查看当前数据库下的所有表
create table student(name varchar(20),id numeric(10),primary key(id)));
创建一张表名为student,有两个属性name和id,数据类型为varchar(20)和numeric(10),主键为id
更新/修改数据
修改单个字段:update 表名 set 字段名=值 where 条件子句;
update student set name="乐乐" where id=1;
修改主键id为1对于的name字段
修改全部数据:
update 表名 set 字段名1=值1,字段名2=值2...,字段名n=值n;
对于NULL不能用=符号,要用is null
修改表结构:
alter table 表名 修改的动作语法;
alter table c1 modify name varchar(20) not null;
设置姓名(属性)不为空
修改属性(增加一列):
alter table score add c_plus_plus numeric(10) not null;
为表score添加一个属性c_plus_plus,数据类型为numeric(10),非空
修改属性(增加一行):
insert into score values("114514",10,20);
也就是增加一个元组,values是对应属性的数值
修改属性(删除一行):
delete from 表名 where 条件;
delete from score where student_id = 114514;
修改数据类型:
alter table 表名 modify 字段 新数据类型;
alter table c1 modify name varchar(20) not null;
修改字段名:
alter table 表名 change 旧字段名 新字段名 新数据类型;
alter table c3 change name name1 varchar(30) not null;
修改表之增加主键:
alter table 表名 add constraint 约束名字 约束类型[字段];
alter table c5 add constraint PK_c5_id primary key(id);
PK_c5_id是约束名(指定主键约束为PK_c5_id,对大部分数据库有效但对于MySql无效,此主键约束名仍为primary)
修改表名:
rename table 旧表名 to 新表名;
rename table c5 to cc55;
建表后添加唯一性约束:
alter table 表名 add unique(字段名)
alter table c9 add unique(id);
建表后添加默认值约束:
alter table 表名 alter 列名 set default’默认值’;
alter table c11 alter name set default “欧”;
建表后添加非空约束:
alter 表名 modify 字段名字段类型not null
alter table c12 modify id int not null;
建表以后添加外键:
alter table 表名称 add foreign key (列名称) references关联表名称(列名称);
alter table stuInfo add foreign key (scode) references score(studentID);
where条件查询
- 精确查询:=、!=、>、<、>=、<=
- 模糊查询:like(像)、not like(不像)
- 通配符:%(任意字符)-(单个字符)
- 逻辑运算符:
- and:同时满足(优先级大于or)
- or:满足任意条件即可
- 区间运算:between a and b (闭区间)
- 集合运算:in 、not in
- 非空运算:is null 、is not null
select查询加上条件查询
desc score;
+-------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| student_id | decimal(10,0) | NO | PRI | <null> | |
| math | decimal(10,0) | NO | | <null> | |
| english | decimal(10,0) | NO | | <null> | |
| chinese | decimal(10,0) | YES | | <null> | |
| chemical | decimal(10,0) | NO | | <null> | |
| c_plus_plus | decimal(10,0) | NO | | <null> | |
+-------------+---------------+------+-----+---------+-------+
显示一个表的逻辑结构,其中的filed,type,null,key字段都是可以修改的
select * from score;
+------------+------+---------+---------+----------+-------------+
| student_id | math | english | chinese | chemical | c_plus_plus |
+------------+------+---------+---------+----------+-------------+
| 114514 | 50 | 60 | 110 | 114 | 514 |
| 1919810 | 10 | 20 | 30 | 40 | 50 |
+------------+------+---------+---------+----------+-------------+
查询表score中的所有信息
select * from student;
+------+--------+
| name | id |
+------+--------+
| 王五 | 1 |
| 李四 | 10 |
| 张三 | 20 |
| 乐乐 | 114514 |
+------+--------+
查询表student中的所有信息
select * from score,student;
+------------+------+---------+---------+----------+-------------+------+--------+
| student_id | math | english | chinese | chemical | c_plus_plus | name | id |
+------------+------+---------+---------+----------+-------------+------+--------+
| 114514 | 50 | 60 | 110 | 114 | 514 | 王五 | 1 |
| 1919810 | 10 | 20 | 30 | 40 | 50 | 王五 | 1 |
| 114514 | 50 | 60 | 110 | 114 | 514 | 李四 | 10 |
| 1919810 | 10 | 20 | 30 | 40 | 50 | 李四 | 10 |
| 114514 | 50 | 60 | 110 | 114 | 514 | 张三 | 20 |
| 1919810 | 10 | 20 | 30 | 40 | 50 | 张三 | 20 |
| 114514 | 50 | 60 | 110 | 114 | 514 | 乐乐 | 114514 |
| 1919810 | 10 | 20 | 30 | 40 | 50 | 乐乐 | 114514 |
+------------+------+---------+---------+----------+-------------+------+--------
查询两个表的笛卡尔乘积
select * from score,student where score.student_id = student.id;
从表score和student的笛卡尔积中查询条件为score.student_id = student.id的信息
Comments NOTHING