基础sql语句
时间:2021-07-01 10:21:17
帮助过:2人阅读
-------------------------------------------------
// 创建数据库
create database databaseName;
e.g:create database sample;
// 显示数据库状态
status;
// 使用数据库
use databaseName;
// 删除整个数据库
drop database databaseName;
---------------------------------------------------
// 创建数据库表
create table tableName(cols);
e.g: create table students
(
id int unsigned
not null auto_increment
primary key,
name char(
8)
not null,
sex char(
4)
not null,
age tinyint unsigned
not null,
tel char(
13)
null default "
-"
);
// 重命名表
alter table tableName rename new_tableName;
e.g:alter table students rename workmates;
// 删除表
drop table tableName;
// 删除表中的数据
delete from tableName
where [condition];
e.g:delete from students
where id
=2;
// 显示表
show tables;
// 显示表结构
describe tableName;
e.g:describe students;
---------------------------------------------------
// 插入表数据
insert into tableName
values(cols);
e.g: insert into students
values(
NULL, "王刚", "男",
20, "
13811371377");
// 查询表数据
select colName1, colName2
from tableName
where [conditions];
e.g: select name, age
from students
where age
>18;
// 更新表数据
update tableName
set colName
=new_value
where [condition];
e.g:update students
set age
=23 where id
=2;
// 修改表结构
// 添加列
alter table tableName
add colName col_dataType
[after colName];
e.g:alter table students
add birthday date after age;
e.g:alter table students
add address
char(
60);
// 修改列
alter table tableName change colName new_colName col_dataType;
e.g:alter table students change name name
char(
16)
not null;
// 删除列
alter table tableName
drop colName;
e.g:alter table students
drop birthday;
---------------------------------------------------
// 日期操作:date_add和date_sub.
mysql> select * from students;
+----+--------+-----+-----+------------+-------------+--------------+
| id
| name
| sex
| age
| birthday
| tel
| address
|
+----+--------+-----+-----+------------+-------------+--------------+
| 1 | 杨家将
| 男
| 26 | 2016-07-28 | 12345678901 | 喝粥大学
|
| 2 | 刘姥姥
| 女
| 25 | 2016-06-03 | 12121212121 | 吃饭大学
|
+----+--------+-----+-----+------------+-------------+--------------+
// 查询以当前时间为原点,以birthday为查询标的,向前向后延伸150days的所有记录.
select name
from students
where date_sub(now(), interval
150 day)
<=birthday;
// 查询以2016
-07-01为原点,以birthday为查询标的,向前向后延伸20days的所有记录.
select name
from students
where date_sub(
‘2016-07-01‘, interval
20 day)
<=birthday;
// 以2016
-07-23为原点,以birthday为查询标的,向前向后延伸30days的所有记录Records, 查询不在Records记录中的其他记录.
select name
from students
where date_sub(
‘2016-07-01‘, interval
30 day)
>birthday;
// 以2016
-06-01增加30days所得到的时间点为OriginDay,查询在OriginDay时间之后的所有记录.
select name
from students
where date_add(
‘2016-06-01‘, interval
30 day)
<=birthday;
// 以2016
-06-01增加30days所得到的时间点为OriginDay,查询在OriginDay时间之前的所有记录.
select name
from students
where date_add(
‘2016-06-01‘, interval
30 day)
> birthday;
// 查询两个日期之间的记录.
select * from students
where birthday
between ‘2016-06-11‘ and ‘2016-07-29‘;
---------------------------------------------------
sdf
sdf
基础sql语句
标签: