RDBMS is an acronym for relational database management system (Relational Database Management System) is. SQL RDBMS is the basis
Open the database use mysql
Navicat recommend installing database operation
The following value, character type with "" 'except the digital type
select basic grammar
select now() # 获取当前时间
select * from table # *表示查询所有
select column from table # 查询一列(column必须在table中存在)
select column1,column2 from table # 查询多列(column必须在table中存在)
select * from table where column is null # 查询列名为空的数据
select * from table where column is not null # 查询列名部位空的数据
select sum(if(column=1,column,0)) from table # 查询列名为1的总数
select sum(case when column=1 then column end) from table
SQL arithmetic operators
select column+1 from table # + 相加 - 相减 * 相乘 / 相除 % 取余
Comparison Operators
select * select demo where column > 3 and column < 5 # 查询大于3小于5的数据
select * from demo where age between 3 and 5 # 和上面一样
select * from table where column = “zhangsan” # 查询列名为zhangsan的数据
select * from table where not column = “zhangsan” # 查询列名部位zhangsan的数据
select * from table where column != “zhangsan” # 和上面一样
> 大于 < 小于 = 等于 >= 大于等于 <= 小于等于 between 在某个范围内
limit specified
select * from demo limit 2 # 查询前俩行
# 其他SQL 用 top 等
Wildcards
_ 下划线表示单个字符
% 百分号表示零个,一个或多个字符
L IKE operator
select * from table column like 's%' # 查询已s开头的数据
select * from table column like '%s' # 查询已s结尾的数据
select * from table column like '%s%' # 查询数据中有s的数据
Aliases Aliases
select * from table as t
# 重命名t就代表了tatble表
In Operators
select * from table where column in (value1,value2)
# 查询列名是value1、value2的值
select distinct (de-emphasis)
select distinct column from table
# 去除重复的列名
and and or operators
select * from table where 条件 and 条件 # 条件都为true执行
select * from table where 条件 or 条件 # 条件都一个为true执行
Order by sort field
select * from table where order by column desc # 通过列名排序降序
select * from table where order by column asc # 通过列名排序升序
inner join (en) can also be written only join
select t1.varlue,t2.value from table1 t1 inner join table2 t2 on 条件
# 以俩表中心建立连接
left join (left join)
select t1.varlue,t2.value from table1 t1 left join table2 t2 on 条件
# 以左表为中心连接右表
rigth join (right connection)
select t1.value,t2.value from table1 t1 rigth join table2 t2 on 条件
# 以右表为中心连接左表
full join (external connection)
select t1.varlue,t2.value from table1 t1 full join table2 t2 on 条件
# 全表查询
self join (from connector)
select * from table1,table2
union all command
select column from table1 union all select column from table2
# 把俩个查询结合在一起,但是列名必须一致
ifnull
select (column1+ifnull(column2,0)) from student
# 假如column为空,设置为0
Computing grammar
select avg(column) from table # 平均值
select sum(column) from table # 计算总和
select count(column) from table # 查询列条数 null不计
Queries sort column function
select first(column) from table # 查询第一行的值
select last(column) from table # 查询最后一行
Max / small function
select max(column) from table # 查询最大值
select min(column) from table # 查询最小值
Group BY
select sum(column) from table group by column
# 对结果进行分组查询
H AVING ( WHERE not be used with aggregate functions )
select sum(column) from table group by column having sum( column )
# 在函数后面添加条件
R & lt ound function
select round(column) from table
# 四舍五入
Insert into statement (add)
insert into table values(value1,value2,value3) # 添加一条数据
insert into table(column1,column2) values(value1,value2) # 添加指定列
# 必须和创建表的字段一致
update statements (modified)
updata table set column1=value where column2=value # 通过列列名2的条件修改列列名1的值
delete statement (Delete)
delete from table # 删除整个表,但是不会删除表结构
delete from table where column='value' # 删除条件的行
Subqueries
select * from table where 1 = (select column from table where column=1)
# 查询1等于子查询
Data Format
NOW() | Returns the current date and time |
CURDATE () | Returns the current date |
CURTIME () | Returns the current time |
DATE() | Section extracts date date or date / time expression |
EXTRACT() | Separate portions return date / time |
DATE_ADD() | Adds the specified time interval to date |
DATE_SUB () | From the date minus a specified time interval |
DATEDIFF() | Returns the number of days between two dates |
DATE_FORMAT() | Date / time in different formats |
DATE | YYYY-MM-DD |
DATETIME | YYYY-MM-DD HH:MM:SS |
TIMESTAMP | YYYY-MM-DD HH:MM:SS |
YEAR | YY or YYYY |
----------------------------------------------------------------------------------------------
CREATE
creader database 库 # 创建库
creader table 表 # 创建表
CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT, # 自动增长
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (ID) #主键
)
ALTER
ALTER TABLE table_name ADD column_name datatype; 添加新列
ALTER TABLE table_name DROP COLUMN column_name; 删除列
ALTER TABLE table_name MODIFY COLUMN column_name datatype; 修改列的数据类型
ALTER TABLE table_name MODIFY column_name datatype NOT NULL; NOT NULL 约束
ALTER TABLE table_name DROP CONSTRAINT MyPrimaryKey; 删除约束
drop
alter table tabke drop index index_name # 删除索引
drop table 表 # 删除表
drop database 库 # 删除库
TRUNCATE
TRUNCATE TABLE table_name;
# drop会删除表结构 truncate只会删除数据
Views
create view view_name AS select * from table
constraint
NOT NULL 约束强制列不接受 NULL 值。
UNIQUE 约束唯一标识数据库表中的每条记录。
PRIMARY KEY 约束唯一标识数据库表中的每条记录。唯一、不为空
FOREIGN KEY 外键约束。一个表中的 FOREIGN KEY 指向另一个表中的 PRIMARY KEY。
DEFAULT 约束用于向列中插入默认值。
CHECK 约束用于限制列中的值的范围。
ALTER TABLE table_name ADD CONSTRAINT MyUniqueConstraint CHECK (CONDITION); # 添加约束
index
Equivalent to the book catalog faster query
It will not affect the data, but the rate will have an impact
The index is only
CREATE INDEX index_name ON table(column); # 创建索引
CREATE UNIQUE INDEX index_name on table(column); # 创建唯一索引不允许重复值
CREATE INDEX index_name on table(column1, column2); # 聚集索引 WHERE 多列的适合
Affairs
- Atomicity: to ensure that all operational tasks were completed; otherwise, the transaction will terminate when an error occurs, and roll back all operations to its original state before.
- Consistency: If the transaction is successful, the state of the database has been carried out correct change.
- Isolation: to ensure that different transaction independently of one another, transparently executed.
- Persistence: even if a system failure occurs, the result of a transaction prior to the successful implementation will persist.
Code
- COMMIT: submit changes;
- ROLLBACK: Rollback change;
- SAVEPOINT: create a restore point in the series may ROLLBACK internal affairs;
- SET TRANSACTION: named transaction;
Create a temporary table : CREATE TEMPORARY TABLE SALESSUMMARY