mysql语句基础

137 阅读2分钟

环境支持:

npm(7.6.0)、node(v15.11.0)

Express(4.18.2)

Mysql(2.18.1)

mysql 数据库的

mysql 官网下载链接downloads.mysql.com/archives/co… Sequel Ace -MacOS系统免费使用的可视化数据库表

项目链接数据库
mysql -u用户名 -p密码   回车    
退出数据库
quit/exit
查看当前使用的数据库
select databases();
查看所有数据库
show databases;

01 创建数据库(database)

create database 数据库名 charset=utf8;
-- create database qd charset=utf8;
使用数据库
use 数据库的名字
-- use qd;
删除数据库
drop database 数据库名;
--  drop database test;

02 数据表的操作(tables)

查看当前数据库中所有表
show tables;
创建表(create)
create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);     可选
-- create table test(name varchar(30) not null, age int unsigned);

-- int unsigned 无符号整形
-- auto_increment 表示自动增长跟主键在一起
-- not null 表示不能为空
-- primary key 表示主键
-- default 默认值
-- create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);     可选
查看表结构(查看表的字段信息)
desc 数据表的名字;
-- desc test;
创建 classes 表

(有id、name两个字段):

-- create table classes(id int unsigned primary key auto_increment, name varchar(30) not null);
创建 students 表

(有id、name、age、high (decimal)、gender (enum)、cls_id这些字段):

create table students(
​        id int unsigned primary key auto_increment, 
​        name varchar(30) not null, 
​        age int unsigned, 
​        high decimal(3, 2), 
​        gender enum("男", "女", "保密", "中性") default "保密", 
​        cls_id int unsigned
​    );
删除表
drop table 表名;
-- drop table test;

03 增删改查(curd)

增加(insert)

​ -- 全列插入

insert into 表名(字段1,字段2) values(值1,值2);
-- 主键字段 可以用 0  null   default 来占位

​ -- 向classes表中插入 一个班级

 insert into classes(id, name) values(1, "qd22");

​ -- 全部插入

insert into 表名 values(值1,值2,值3...)
-- insert into students values(1, "张三", 18, 1.83, '男', 1);

​ -- 部分插入

insert into 表名(列1,...) values(值1,...)
--  insert into students(name) values("李四");
--  insert into students(name,age) values("王五", 20);

​ -- 多行插入

insert into 表名(列1) values(值), (值);
-- insert into students(name) values("赵六"),("李七");
修改(update)
update 表名 set 列1=值1,列2=值2... where 条件;

​ -- 全部修改

update students set age = 16;

​ -- 按条件修改

update students set age = 20 where id=3;
update students set age = 18 where name="赵六";

​ -- 按条件修改多个值

update students set gender ="",age = "" where name="xxx";
--  update students set age = 15, high=1.80 where id=2;
查询基本使用(select)

​ -- 查询所有列

select * from 表名;
-- 查询students表中的所有数据信息
-- select * from students;

​ ---定条件查询

 select * from students where id = 1;
 --查询id为1的学生所有信息  

​ -- 查询指定列

select 列1,列2,... from 表名;
-- select name, age from students;
-- select name, age from students where id = 1;