SQL常用语句整理

    自己做项目时,主要是表单的提交,所以对数据库的操作有所了解。但是面试时问起时,发现自己掌握不牢,而且掌握的都太基本了,故在此将面试中涉及到的和基本的SQL操作一起整理一下。所有的SQL操作我按照以下分类进行整理:

    (1)操作数据库

      create database test;创建数据库test

      drop database test;删除数据库test

      use test;使用数据库test

      show databases;显示所有的数据库

    (2)操作数据表

     create table test (

          username vachar(100),

          passwd varchar,

     );指定列和列的数据类型创建表

     create table new_table like old_table;//从已有表创建新表

     create table new_table as select col1,col2,...,coln from old_table definition only;用旧表的特定列创建新表

     drop table test;删除表test

     Alter table test add column user_sex int;给表test增加一列user_sex并指定数据类型

     Alter table test add primary key(username);给表test添加主键为username
     Alter table test drop primary key(username);删除表的主键

    对数据记录的操作有基本和高级之分,以下是基本的操作:

    (3)查找数据记录

     select * from table_name where arr_1 = value;*表示选出所有列

     select * from table_name where arr_1 between value1 and value2;

     select * from table_name where arr_1 like '%value%';like %value%表示只要含有value字符串就行

     select username, passwd from test;从表test中选出指定列

     select distinct username from test;从表test中选出username列并去重

     select * from table_name where col_1 = 'value1' and(or) col_2 = 'value2';双重条件,若多重可加括号

     select name, age from person order by name, age;//从person表中选出name和age两列,按字母顺序显示name,以数字顺序显示age

     select name, age from person order by name desc, age asc;//从person表中选出name和age两列,按字母顺序显示name,以数字顺序显示age

     select col_1, col_2 from old_table as new_table;从表old_table中取出两列形成新的表new_table

    (4)更新数据记录

     update table_name set col_1 = new_value where col_2 = value2
     update test set passwd = 'newpasswd' where username = 'hbj';更新username为hbj的密码为passwd

    (5)增加数据条目

     insert into table_name values (value1, value2,....);插入所有值
     insert into table_name (col_1, col_2,...) values (value1, value2,....);
     insert into Lab (name, officer) values ('NGIRC', 'cqxu');特定列值插入

    (6)删除数据条目

     delete * from table_name;删除所有列
     delete from table_name where col_1='value1';删除特定列

    以下是几个常用的高级操作:

     1.select top number|percent col_1 from table_name:select top 2 * from test;限制了显示的条目数

     2.selet col_1 from table_name where col_2 in (value1,value2,...);col_2有多选项

     3.select col_1 as name_1, col_2 as name_2 from table_name;给表取个别名

     4.select new1.col_1, new2.col_3, new2.col_7 from table1 as new1, table2 as new2 where new1.col_5 = 'value5' and new2.col_7 = 'value7' ;此语句也可用5来写

     5.select Persons.LastName, Persons.FirstName, Orders.OrderNo from Persons inner join Orders on Persons.Id_P=Orders.Id_P order by Persons.LastName;适用于两个表有共享键的情形,也可用4来写

     6.select column_name(s) from table_name1 left join table_name2  on table_name1.column_name = table_name2.column_name;返回table_name1中所有的行即使table_name2中没有匹配的行。

     7.select column_name(s) from table_name1 left join table_name2  on table_name1.column_name = table_name2.column_name;跟6恰好相反;join的作用只是将选出的部分邻接在一起。

     8.select col_1 from table_name1 union select col_2 from table_name2;将两个表中选出的数据融合到一起

  很好的参考资料:http://www.w3school.com.cn/sql/index.asp

   

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值