前端常用SQL语句

常用SQL语句

SQL(Structure Query Language)结构化查询语言用于关系型数据库,Mysql,Oracle,sqlServer
前端学会这些基本的DB操作基本够用了

DDL(Data Definition Language) 数据定义语言

包括:create(创建数据库对象),alter(修改数据库对象的结构)和drop(删除数据库对象)

  • create(创建数据库对象)
    创建数据库: CREATE DATABASE XXX;
    创建数据库表:
Create Table 表名(
列名1 数据类型 相关约束,
列名2 数据类型 相关约束,
....
列名n 数据类型 相关约束
)
CREATE TABLE `User` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `role` enum('admin','uploader','normal') DEFAULT 'uploader',
  `name` varchar(255) DEFAULT NULL,
  `created` int(11) unsigned NOT NULL,
  `updated` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • alter(修改数据库对象的结构)

  • 添加列:Alter table 表名 add 列名 【数据类型 相关约束】 ;

alter table Users2 ADD test INT
  • 删除列:Alter table 表名 drop 列名 ;
alter table Users2 drop  test
  • 修改列: Alter table 表名 change 新 旧 数据类型 ;
alter table Users2 change test1 test2 int
  • drop(删除数据库对象)
  • 删除表:Drop table 表名;
drop table "表格名

DML(Data ManipulationLanguage) 数据操纵语言

包括:Insert(插入数据),update(修改数据),delete(删除数据)

  • Insert 插入数据
  • insert into table_name(列名1,列名2,…) values(‘值1’,‘值2’,…);
INSERT INTO `Users` (`id`, `role`)
VALUES
   (1,'admin')
  • Update 更新数据
  • //更新某一行的某一列
    update tableName set xxx=xxx where xxx=‘xxx’;
update Users1 set name = 'yuyang' where id = 2
  • //更新某一行的若干列
    update tableName set xxx=xxx,xxx=xx where xxx=‘xxx’
  • Delete 删除数据
  • //删除表中的某一行
    delete from tableName where xxx=xxx
    delete from Users1 where id=8;
  • //删除表中的所有数据
    delete * from tableName;

DQL(Data Query Language) 数据查询语言

格式:Select 字段列表|* from 表

select * from tableName
select id from tableName
  • as 命别名 select * from tableName
  • count 总数:select count(*) from tableName
  • sum 求和:select sum(field1) from tableName
  • avg 平均:select avg(field1)from tableName
  • max 最大:select max(field1) from tableName
  • min 最小:select min(field1)from tableName
  • 去重 distinct select DISTINCT name from tableName

where 条件表达式(1=1)

select * from tableName where 1=1

  • = >= < <=等运算符
    select count(*) as count from tableName where id > 10
  • and
    select count(*) as count from tableName where id > 10 and name = 'yuyang'
  • between … and
select * from Users where 1 and id not between 1 and 10  order by id asc
  • or select count(*) as count from tableName where id > 10 or name = 'yuyang'
  • in select * from tableName where id > 10 and name in ('yuyang','taozi')
  • like 模糊查询
  • 1、%:表示任意0个或多个字符。可匹配任意类型和长度的字符,有些情况下若是中文,请使用两个百分号(%%)表示 LIKE ‘%羽%’
select * from tableName where id > 10 and name like '%y%'
  • _: 表示任意单个字符。匹配单个任意字符,它常用来限制表达式的字符长度语句 LIKE ‘
select * from tableName where id > 10 and name like 'yuyan_'
  • 子查询
    select name from tableName where id in (select id from `Tags` where id > 4) select name from tableName where id=(select id from `Tags` where id = 4)
  • 连接查询
    select Pic_Models.`ModelId`, Pic_Models.`PicId`,Pics.`src` from Pic_Models , Pics where Pic_Models.`PicId` = Pics.`id`
  • order by 分组表达式
    • ASC:升序
    • DESC:降序
  • limit 分页表达式(limit );
    select * from tableName LIMIT 10;–检索前10行数据,显示1-10条数据
    select * from tableName LIMIT 1,10;–检索从第2行开始,累加10条id记录,共显示id为2…11

ORM框架(sequelize)http://www.nodeclass.com/api/sequelize.html#mixes

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章