SQL常用语句
对库操作
use testbase; -- 使用数据库
alter database testbase character set utf8; -- 更改数据库字符集
alter table t_table convert to character set utf8; -- 更改表字符集
show databases; -- 显示所有数据库
show create database testbase; -- 查询数据库
drop database testbase; -- 删除数据库
select database(); -- 查询当前数据库
create database if not exists qq; -- 如果不存在qq就创建数据库
-- 导出导入数据
mysqldump -u 用户名 -p 数据库名 > 导出的文件名 -- 导出
mysql -u用户名 -p密码 数据库名 < 数据库名.sql -- 导入
source 路径 -- 导入
对表结构操作
desc student; -- 查看表列信息
alter table student character set utf8; -- set字符集
alter table student modify name varchar(20) not null; -- 修改列属性
alter table student change math english varchar(15) not null; -- 改变列名和属性
alter table student drop english; -- 删除列
alter table student add chinese varchar(20) not null; -- 添加列
alter table test_one rename test_two; -- 修改列名
CREATE TABLE newuser LIKE user; -- 复制表结构
INSERT INTO newauser SELECT * FROM user; -- insert表数据
create table new_nickname select * from nickname; -- 直接创建并复制
create table new_nickname select name, age from nickname; -- 复制需要部分
对表数据操作
select distinct name from student; -- distinct 去除重复
select * from student; -- select
delete from student where id=0; -- delete
insert into student values (5,'陈洁令',null,'男',70,70); -- insert
update student set chinese=100 where id=1; -- update
进阶select
-- order by
select * from student order by english; -- 默认升序
select * from student order by english desc; -- 降序
select * from student order by english asc; -- 升序
-- group by 和 as 和 having
select sex 性比,count(*) as 数量 from student group by sex having sex='男';
select english as 分数,count(*) 数量 from student group by english;
-- limit
select * from student limit 1,1;
-- || && < > = isnull
select * from student where id < 3;
select * from student where id >= 3;
select * from student where address is null;
select * from student where id between 2 and 4;
select * from student where id >=2 and id <= 4;
select * from student where name='陈洁令' or address='山东';
select * from student where name='陈洁令' and address='山东';
-- min max avg sum count
select min(english),max(english),avg(chinese),sum(chinese) from student;
-- count(*) 返回表中所有存在行的总数包括null,然而count(1) 返回的是去除null以外的所有行的总数。
select count(*) from student;
select count(1) from student;
select 1,2,3 from student; -- sql注入占位
约束
1)not null :非空约束,保证字段的值不能为空
s_name VARCHAR(10) NOT NULL, #非空
2)default:默认约束,保证字段总会有值,即使没有插入值,都会有默认值!
age INT DEFAULT 18, #默认约束
3)unique:唯一,保证唯一性但是可以为空,比如座位号
s_seat INT UNIQUE,#唯一约束
4)check:检查性约束【MySQL不支持,语法不报错,但无效】
s_sex CHAR(1) CHECK(s_sex=‘男’ OR s_sex=‘女’),#检查约束(Mysql无效)
5)primary key :主建约束,同时保证唯一性和非空
id INT PRIMARY KEY,#主建约束(唯一性,非空)
6)foreign key:外键约束,用于限制两个表的关系,保证从表该字段的值来自于主表相关联的字段的值!
teacher_id INT REFERENCES teacher(id) #这是外键,写在列级,Mysql无效
7)auto_increment:自增长列
id int primary key auto_increment,
一个表中有且只能有一个自增长列,自增长列一般和主键搭配
修改约束
show keys from account; -- 查看表约束
alter table student modify name varchar(20) not null; -- 修改列属性和约束
alter table account add unique(id); -- 添加unique约束
alter table account drop index id; -- 移除unique约束
alter table account drop primary key; -- 删除主键
alter table account drop foreign key fk_account ; -- 移除外键
这篇博客详细介绍了MySQL中的SQL常用语句,包括对库、表结构和表数据的操作,以及进阶的SELECT查询。重点讲解了各种约束类型,如非空约束、默认约束、唯一约束、主键约束、外键约束和自增约束,帮助读者快速掌握MySQL数据库的基本操作。

1553





