文章目录
前言
本文主要记录sql基本操作的相关笔记
–操作之前必须首先新建数据库,使用下列语句即可
create database test1;
–建表之前要先使用use语句声明使用了哪个数据库
use test1;
create table table1(
id smallint(5) not null primary key,
first_name varchar(45) not null DEFAULT 'uuu',
last_name varchar(45) not null DEFAULT 'ttt'
);
–更改first_name字符长度为50
ALTER TABLE `test1`.`table1`
CHANGE COLUMN `first_name` `first_name` VARCHAR(50);
–添加数据
insert into table1 values
(1,'a','b'), (2,'a','b'), (3,'a','b')
–清空数据
drop table if exists table1;
–增加一列数据并赋默认值
alter table table1 add create_date datetime not null DEFAULT '2020-10-01 00:00:00'
–建立索引
create index idx_last on table1(last_name)
–强制索引查找
select * from table1 t1 FORCE INDEX (idx_last)
select case when max(s)%2=1 then max(s)/2
select grade,sum(number)over(order by grade)s from class_grade
–删除表table1
drop table if exists table1