Mysql数据库的常用基本语句:极简

一、基本语句

详细教程网站:https://www.w3school.com.cn/sql/sql_select.asp

1、创建数据库、数据表:

show databases;   // 查看当前所有的数据库
create database mysql;   // 创建mysql数据库1
create database publicdb DEFAULT CHARSET utf8 COLLATE utf8_general_ci;  // 创建mysql数据库2
use mysql;   //使用mysql数据库
select User, Host FROM user;   // 查看所有用户名,主机
show databases;   // 查看当前所有的数据库
#创建数据表及格式
create table if not exists `login`(
	`ID` INT UNSIGNED AUTO_INCREMENT,
	`user_name` varchar(20) not NULL,
	`password` varchar(18) not NULL,
	`create_date` date,
	PRIMARY KEY ( `ID` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
alter table login add column login_last char(30);   // 给表增加列

show tables;   //查看mysql库下面的表
show columns from login;  // 查看login数据表的格式
insert into login values('001','a177447','abc123','2022-1-27');   //插入数据到 login 表
select * from login;  // 查看login里面的数据
select * from login order by id;    // 升序查询
select * from login order by id;    // 降序查询  
SELECT * from login WHERE user_name='a177447';  // 在login中查看用户的单条数据
alter table login drop column `user_name`;  // 删除login表中的user_name列
delete from login where user_name='a177447'; //删除login表中的user_name='a177447'的行
truncate table login;    // 删除login表中所有数据,无日志
update login SET password='123456' WHERE user_name='张三'; 
// 在login表中找到 user_name='张三' 项,将其 password 的值修改为 123456
update login set user_info=concat(user_info,'追加的字符串') where user_account='177447';
// 在login表中找到  user_account='177447' 项,将其 user_info 的值追加字符
delete from login where ID='001';
CREATE INDEX count ON table_name (column_list)

2、增删改查

// 增
insert into class values(null,'123');  // 将整项插入到class表
// 删
delete from class where id='001';   // 删除class表中id为001的整行
alter table login drop column user_name;  // 删除login表中user_name的整列
// 改
update class set title='内容' where id='001';  // 修改class中id为001项的title内容
// 查
select title from class where id='001';  // 查看class表中id为001该项的title值

3、修改主键、自增:

desc login;   // 查看login表中各项的属性;
alter table login modify ID int(11);    // 取消login表中ID项的自增;
alter table login drop primary key;    // 取消login表的主键(唯一性);
alter table login add user_name int not null primary key;   // 新增加user_name主键项
alter table login add primary key(user_name);  // 将 user_name 设置为主键,要先取消之前主键才生效;
insert into userInfo values(null,'ddf','8979');   // 含自增键插入法
//取消,设置外键约束
set foreign_key_checks=0;   // 取消
set foreign_key_checks=1;   // 设置

4、连表操作

select students.id,students.name,class.title from students left join class on students.class_id = class.id;  // 看图

Mysql数据库的常用基本语句:极简_第1张图片
5、查看数据库最大连接数

show variables like '%connections';

修改方法:修改ini文件,重启mysql
max-connections = 1000
max_user_connections = 1000
mysqlx_max_connections = 1000

7、设置utf-8格式

//给表设置
alter table `表名` convert to character set utf8;

你可能感兴趣的:(数据库,mysql,sql)