sql 簡單語法

一、數據庫操做sql

create database student_info  -- 建立數據庫 
drop database student_info    -- 刪除數據庫 

二、表操做數據庫

-- 建立表
create table student(
    id int not null primary key,
    name varchar(20) not null,
    age int null,
    sex varchar(10) 
)    
-- 刪除表
drop table student
-- 修改表,增長一個列
Alter table student add column address varchar(50)

 

 

三、sql語句函數

簡單語句
spa

插入(增):insert into student(id, name, address) values(1, 'Xiaohong', 16)
刪除(刪):delete from student where age<=6
更新(改):update student set name='Lily' where id=1
查詢(查):select * from student 

高級語法code

模糊查詢:select * from student where name like '%Xiao%'
排序:select * from student order by field1,field2 desc
總數:select count(*) as totalcount from student
函數:select sum(age) as sumAge, avg(age) as avgAge, max(age) as maxAge, min(age) as minAge from student
前幾:   select top 10 * from student order by age desc
去重:   select distinct name from student
多個條件: select * from student where name like '%Xiao%' and age=16 or age=20
between:   select * from student where age between 10 and 20
in:   select * from student where name in ('Lily', 'Amy')
分組: select age, count(*) from student group by age
分組帶條件: select age, count(*) from student group by age where age >10 having count(*)<5

四、錶鏈接blog

JOIN: 若是表中有至少一個匹配,則返回行
LEFT JOIN: 即便右表中沒有匹配,也從左表返回全部的行
RIGHT JOIN: 即便左表中沒有匹配,也從右表返回全部的行
FULL JOIN: 只要其中一個表中存在匹配,就返回行排序

create table course(
    cno int not null primary key,
    cname varchar(20) not null
)
create table StudentCourse(
    sno int not null,
    cno int not null,
    score double
)

-- 錶鏈接, 查找全部學生的選課記錄
select s.name as 學生姓名,sc.cno as 選修課號,sc.score as 成績 
from student s, StudentCourse sc
where s.id=sc.sno

-- 內鏈接, 查找全部成績及格的選課記錄
select s.name as 學生姓名,sc.cno as 選修課號,sc.score as 成績 
from student s
inner join StudentCourse sc on s.id=sc.sno
where sc.score>60

-- 左鏈接, 查找全部學生的選課記錄
select s.id as 學號,sc.cno as 選修課號,sc.score as 成績 
from student s
left join StudentCourse sc on s.id=sc.sno

-- 嵌套查詢, 查找王敏同窗的選課記錄
select *
from StudentCourse
where sno in (
   select id from student where name='王敏'
)

--查找每一個學生大於自身平均分的科目
select cno
from StudentCourse a
where score> (
  select avg(score) from StudentCourse b where a.sno=b.sno
)

五、SQL 約束索引

約束用於限制加入表的數據的類型。能夠在建立表時或表建立後規定約束。約束主要有如下幾類:table

  • NOT NULL   強制列不能爲 NULL 值
  • UNIQUE      惟一標識數據庫表中的每條記錄, 每一個表能夠有多個 UNIQUE 約束,可是每一個表只能有一個 PRIMARY KEY 約束。
  • PRIMARY KEY  惟一標識數據庫表中的每條記錄,主鍵必須包含惟一的值,主鍵列不能包含 NULL 值。
  • FOREIGN KEY  一個表中的 FOREIGN KEY 指向另外一個表中的 PRIMARY KEY,如StuentCource 的sno指向Student的id
  • CHECK    在特定的列中對值進行限制
  • DEFAULT  設置默認值
create table student(
    id int not null,
    name varchar(20) not null,
    age int null DEFAULT 1,
    UNIQUE (name),
    PRIMARY KEY (id),
    CONSTRAINT chk_age check (age>0 AND age<200),
    CONSTRAINT uq_name unique(name)
)

六、索引class

您能夠在表中建立索引,以便更加快速高效地查詢數據。

-- 建立索引
create index idx_age on student(age asc)
create unique index idx_name on student(name)
-- 刪除索引
drop index idx_name 

七、視圖

視圖是基於 SQL 語句的結果集的可視化的表。

-- 刪除視圖
if exists (select * from dbo.sysobjects where id = object_id(N'dbo.young_student') and objectproperty(id, N'isview') = 1)
drop view young_student
-- 建立視圖
create view young_student 
as 
select * from student where age<10
相關文章
相關標籤/搜索