MySQL之一看就懂所有基操

1.数据库分类

关系型数据库(sql),非关系型数据库(nosql)

mysql:关系型数据据库,二维表格,库,表,数据,格式固定,安全性好

Redis,MongoDB:非关系型数据库,灵活,语法不通用

mysql

查看数据库:show databases;

进入某个数据库:use mysql;

判断当前在哪个数据库里:select database();

查看当前用户:select user();

1.库级

#展示库
show databases;
#创建库
create database laotie;
create database if not exists laotie;
#使用库
use laotie;
#删除库
drop database laotie;
drop database if exists laotie;

2.表级

#展示表
show table;
show table from mysql;#展示mysql库里的表
#创建表
create table student(name varchar(20),age int,sex char(10));
create table if not exists student(name varchar(20),age int,sex char(10));
#展示表结构
desc student;
show columns from student;
describe student;
#展示标准sql语句 ,创建表的信息
show create table student;
#删除表
drop table student;

3.表结构

#修改表名
alter table new_table rename to old_table;
#修改字段名
alter table oldtable  change id stu_id int;
#修改字段类型
alter table oldtable  modify stu_id tinyint;
#添加字段
alter table old_table add age tinyint;
alter table old_table add(aa int,bb int,cc int);#添加多个
#删除字段
alter table old_table drop bb;#删除单个
alter table old_table drop aa,drop cc,drop stu_id;#删除多个
#表名和字段名尽量避免修改。

4.数据

#增删改查
#查看表数据
select * from student;
#增
insert into student values("小江",18);#插入一行数据
insert into student values("小王",19),("小慧",20);#插入多行数据
insert into student(name) values("小涛");#指定字段插入。

#查
select *from student;#全字段查询
select name from student;#指定字段查询
select name,age from student;
select *from student where age>14;
select *from student where name like'小_';
select *from student where name like'小%';

#用到and或者or
select *from student where age=18 or age=19;
select *from student where name="小江" and age=18;

#改
update student set age=20;#修改全部数据
update student set age=19 where name="小江";#修改指定条件数据
update student set name="江江",age=12 where name="小江";#修改多个字段。
update student set name="江江",age=12;#修改全部数据
#注意:一定要带where条件,不然就会修改全部。

#删
#条件删除,删除的是符合条件的全部数据。
delete from student where age=13;
#全部删除
delete from student;
#注意:一定要带where条件,不然就是删库跑路

猜你喜欢

转载自blog.csdn.net/qq_51721904/article/details/121705860