mysql的基本操作

MySQL数据库是当前应用非常广泛的一款关系型数据库

主要需要掌握 数据库信息的增删改查

数据库的操作主要包括:

数据库的操作,包括创建、删除

表的操作,包括创建、修改、删除

数据的操作,包括增加、修改、删除、查询,简称crud

数据库操作

创建,删除,查询

创建:

create database 数据库名 default charset=utf8

删除:

drop database 数据库名

查询:

show databases

使用:

use 数据库名

查看当前数据库:

select database()

表操作

展示所有:

show tables

创建:

#省略号是剩余属性
create table 表名(
id int 
...
);

修改:

alter table 表名 add|modify|drop 列名 类型;

删除:

drop table 表名

查看表结构:

desc 表名

更改表的名字:

rename table 旧表名 to 新表名;

数据操作

增加:

#增加一行数据
insert into 表名 values()
#给一行数据中缺少的数据补全
insert into 表名(列名...) values(值...)
#一次性插入多行数据
insert into 表名 values()()()

删除:

delete from 表名 where 条件

修改:

update 表名 set 列名 =列值 where 条件

查询:

#查看所有
select * from 表名

逻辑删除:

#添加一个新的列作为标志位,默认0 表示未删除
alter table students add isdelete bit default 0;
#如要删除 修改标志位就行
update 表名 isdelete=1 where 条件

举例:创建一个学生表(包括姓名,年龄,性别,学号)

drop table if exist Student(
sid int auto_increment primary key,
sanme varchar(10) not null,
ssex char(1) not null,
snum varchar(20) not null unique
);

若要创建带外键的表,即完成一对多映射

drop table if exist Student;
drop table if exist Grand;

create table Student(
sid int auto_increment primary key,
sanme varchar(10) not null,
ssex char(1) not null,
snum varchar(20) not null unique
sgrand int not null,
foreign key(sgrand) reference Grand(id)
);

create table Grand(
id int auto_increment primary key,
grand varchar(5) not null unique
);
原文地址:https://www.cnblogs.com/Zhao01/p/11994186.html