SQL数据库的语句整理

来源:互联网 发布:古代兵器知乎 编辑:程序博客网 时间:2024/06/06 08:32

欢迎来到unity学习unity培训unity企业培训教育专区,这里有很多U3D资源U3D培训视频U3D教程我们致力于打造业内unity3d培训学习第一品牌。

 

      1. 建立数据库:create database <数据库名>

      2. 利用数据库:  use <数据库名>

      3. 建立数据表:


      create table 表名(
      id int identity(101,1) primary key,
      name varchar(20) not null,
      password varchar(10)
       ) 

 

      4.删除数据库:drop database third

 

      5.删除表:drop table student

 

      6. 查询所有信息:select * from <表名>

 

      7.插入一行数据insert  into <表名>[(列名)] values(值列表)

 

      8.插入多行数据:insert into<表名>(列名)

           insert into <表名>(列名)

           select <列值>union

           select <列值>union

 

      9.更新数据:

            update <表名>set<列名=更新值> [where<更新条件>]

 

      10.删除数据: delete from<表名>[where<删除条件>]

 

      11.添加一列:alter table 表名 add 列名 类型(长度) null

 

      12:更改一个列的类型:alter table 表名 alter column 列名 数据类型(长度)

 

      13.删除一列:alter table 表名 drop column 列名

 

      14.主键约束:alter table 表名 add constraint 主键别名 primary key (主键列

 

      15.唯一键约束:alter table 表名 add constraint 唯一键别名 unique (唯一键列)

 

      16.默认键约束: alter table 表名  add constraint 默认键别名 default (‘默认值’) for 默认键

 

      17.检查键约束:alter table 表名 add constraint 检查键别名check(stuAge>=15 and stuAge<=40)

 

      18.添加检查键:teacherusers表添加主外关联

          alter table score add constraint t_fk foreign key(uid) references users(id)

 

      19.删除约束:alter table 表名  drop constraint 约束别名

 

      20.查询部分数据:select id,name from users where name='张三' --查询表中name为‘张三’的数据

 

      21.去掉重复字段查询记录:select distinct name from student

 

      22.合并查询(合并两表中相同的字段):select * from student union select * from score

 

      23.用AS来命名列:select id as 编号,name as 姓名 from users

 

      24.用 = 来命名列:select 编号 =id ,姓名=name  from users

 

      25.查询空行:select id, name from users where password is null

 

      26.查询非空行select name from users where name is not null

 

      27.使用常量列(默认值):select name as  姓名 ,'密码' as password from users 

 

      28.限制固定行数:select top 3 * from users

 

      29.返回百分之多少行:select top 50 percent * from users

 

      30.升序:select * from users order by idselect * from users order by id asc

 

      31.降序: select * from users order by id desc

 

      32.按多列排序(当排序的值相同时,按第二个字段排序):select * from users order by name,id

 

      33.模糊查询Like:SELECT * FROM 数据表 WHERE 编号 LIKE 数值

 

      34.模糊查询is null:SELECT * FROM 数据表 WHERE 编号 IS NULL 数值

 

      35.模糊查询Between:SELECT StudentID, Score FROM SCore WHERE Score BETWEEN 60 AND 80

 

      36.模糊查询in:SELECT SName AS 学员姓名,SAddress As 地址 FROM Students WHERE SAddress IN ('北京','广州','上海')

 

      37.sum总和:select sum(age) as '年龄总和' from student --计算student中所有学生年龄的总和

 

      38.avg平均:select avg(id) as 平均 from users  --计算user中所有id的平均数

 

      39.Mix,Min最大最小值:select max(id) as 最大,min(id) as 最小 from users --计算出userid的最大值和最小值

 

      40.country:SELECT COUNT(*)  AS 及格人数 From Score WHERE Score>=60

 

      41.分组查询Group by: select id,avg(id) as 平均 from users group by id

 

      42.内连接查询(跟表的位置无关):

           SELECT Students.SName, Score.CourseID, Score.Score

           FROM  Students,Score

           WHERE  Students.SCode = Score.StudentID

43.左外连接查询(跟表的位置有关):

           SELECT  S.SName,C.CourseID,C.Score 

           From  Students AS S

           LEFT JOIN  Score AS C

           ON  C.StudentID = S.SCode

 

44.右外连接查询(跟表的位置有关):

           SELECT Titles.Title_id, Titles.Title, Publishers.Pub_name

           FROM titles 

           RIGHT OUTER JOIN Publishers 

           ON Titles.Pub_id = Publishers.Pub_id

 

 

 

 

 

 

 

 更多精彩请看:http://www.gopedu.com/

0 0
原创粉丝点击