sql语法
2016-09-13 15:18
141 查看
一、SQL语句
注意:SQL 对大小写不敏感
二、基本概念:
SQL DML 和 DDL
可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL)。
SQL (结构化查询语言)是用于执行查询的语法。但是 SQL 语言也包含用于更新、插入和删除记录的语法。
查询和更新指令构成了 SQL 的 DML 部分:
SELECT - 从数据库表中获取数据
UPDATE - 更新数据库表中的数据
DELETE - 从数据库表中删除数据
INSERT INTO - 向数据库表中插入数据
SQL 的数据定义语言 (DDL) 部分使我们有能力创建或删除表格。我们也可以定义索引(键),规定表之间的链接,以及施加表间的约束。
SQL 中最重要的 DDL 语句:
CREATE DATABASE - 创建新数据库
ALTER DATABASE - 修改数据库
CREATE TABLE - 创建新表
ALTER TABLE - 变更(改变)数据库表
DROP TABLE - 删除表
CREATE INDEX - 创建索引(搜索键)
DROP INDEX - 删除索引
sdf
sdf
--------------------------------------------------- // 创建数据库 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'; ---------------------------------------------------
注意:SQL 对大小写不敏感
二、基本概念:
SQL DML 和 DDL
可以把 SQL 分为两个部分:数据操作语言 (DML) 和 数据定义语言 (DDL)。
SQL (结构化查询语言)是用于执行查询的语法。但是 SQL 语言也包含用于更新、插入和删除记录的语法。
查询和更新指令构成了 SQL 的 DML 部分:
SELECT - 从数据库表中获取数据
UPDATE - 更新数据库表中的数据
DELETE - 从数据库表中删除数据
INSERT INTO - 向数据库表中插入数据
SQL 的数据定义语言 (DDL) 部分使我们有能力创建或删除表格。我们也可以定义索引(键),规定表之间的链接,以及施加表间的约束。
SQL 中最重要的 DDL 语句:
CREATE DATABASE - 创建新数据库
ALTER DATABASE - 修改数据库
CREATE TABLE - 创建新表
ALTER TABLE - 变更(改变)数据库表
DROP TABLE - 删除表
CREATE INDEX - 创建索引(搜索键)
DROP INDEX - 删除索引
sdf
sdf
相关文章推荐
- SQL语法全接触
- SQL 语法参考手册
- 学会看oracle SQL语法图
- SQL 语法参考手册
- 标准查询语法的SQL语句
- ORACLE 常用的SQL语法和数据对象
- SQL 语法参考
- SQL的详细语法介绍——对于学习数据库最基础知识一
- 一个sql语句,包含有几乎所有标准查询语法
- MySQL的distinct group by SQL语法用法说明
- T-SQL 中 CREATE TABLE 语法
- T-SQL 中 CREATE PROCEDURE 语法
- SQL语法参考手册
- SQL语法
- Yukon 中的 T-SQL:T-SQL强大的新语法给予 SQL Server 进一步的可编程性
- SQL语法大全
- SQL 语法参考手册
- SQL语法查询文档
- SQL语法参考
- 动态sql语句基本语法