SQL语句简单笔记

Create database database name;//创建数据库
Show databases dbName;    //显示所有数据库

Create table tableName;    //创建表
Show tables tName;      //显示所有表
Alter table oldName rename newName; //修改表名
Desc tableName;             //查看表结构
Drop table tableName;            //删除表
Alter table tName add field type;       //在tName表中添加字段

Alter table tName change name oldname newname type; //修改tName表字段
(Alter table tName modify field type;) 
Alter table tName drop field          //删除字段

Insert into tName (field…)values(value…); //插入数据
Select * from tName;              //查询数据
• Is null ,is not null          
• In(value…) ,not in(value…)        //包含(value)
• Like ‘value%’ ,’%value%’,’%value’,not like value //模糊查询
• Order by field asc or desc      //排序 asc 升序 ,desc 降序
• ‘<>=!=‘              //<> 作用同等!=
• Table1 left join table2 on (condition);(inner joinright joinfull joincross join//连接表查询
• ResultSet1 union ResultSet2 ;(union all//合并结果集
• Common Function               //常用函数
  ○ IfNull(field,defaultValue)         //判断field是否为空,为空赋予defaultValue
  ○ Case When condition Then value1 else value 2 end //类似if else
  ○ If condition then v1 else v2 end if           //if else判断
  ○ Decode(field,condition1,result 1,condition2,result2,default)  //类似if else ,field 为 condition1 则值为result1
• Group by field             //分组
• Having condition            //过滤
• Limit offset,quantity          //分页

Update tName set field=value… where Id = value;//更新数据
Delete from tName Id =value;     //删除数据

Field constraint           //字段约束
• Not null (NK)             //非空约束
• Unique (UK)              //唯一约束
• Primary key (PK)           //主键约束  
  ○ AUTO_INCREMENT      //自动增加

• Foreign key(FK)          //外键约束
  ○  Constraint fk_Name  foreign key (fk_field) references tName (pk_field)//简单运用方法
Create view vName as subquery;  //创建视图
Create replace view vName as subquery
Alter view vName as subquery     //修改视图

Alter table tName add index iName(field)//添加索引
Alter table tName drop index iName   //删除索引

Create trigger  tName 
(Before or after ) 
(Insert,delete,Update) On tblName 
For each row 
tStmt ://(new,old value)        //创建触发器

Show triggers            //显示所有触发器
Drop trigger              //删除触发器

Delimiter \\
Create procedure  pName(
  in p1 type,  
  out p2 type,
  in out p3 type
)Beginend \\                   //创建存储过程 :(call pName();)//使用存储过程

//创建函数
Delimiter \\
create function fName(
  p1 type
)returns int
Begin
Return p1;
End\\            

 

posted @ 2018-07-07 22:38  Gavinloliy  阅读(399)  评论(0编辑  收藏  举报