就如同酒肆里的说书人嘴里的故事,他沉浸在那场生离死别的故事里,而那故事却与他完全无关。
从头至尾,他是那惊堂木,是折扇,是案几,是说书人手中的茶碗,是一个道具,却偏偏不是那故事里的人。
SQL不是一个数据库,而是一门大家约定俗成的数据操作语言。但软件开发环境下的数据库操作种类很多,存在着很多不同版本的 SQL 语言,但是为了与 ANSI 标准相兼容,它们必须以相似的方式共同地来支持一些主要的关键词(比如 SELECT、UPDATE、DELETE、INSERT、WHERE 等等)。
作为 一名初级程序员,我拿它是完成任务的,首先,最基本的建库建表。
SQL Server 下:
新建数据库
use master
if exists(select * from dbo.sysdatabases where name = 'MyBill')
drop table MyBill
go
create database MyBill
on(
name = 'MyBill_data',
filename = 'D:\MyData\MyBill.mdf',
size = 10,
maxsize = 20,
filegrowth = 4
)
log on(
name = 'MyBill_log',
filename = 'D:\MyData\MyBill.idf',
size = 10 mb,
maxsize = 20 mb,
filegrowth = 4 mb
)
go
新建表
use MyBill
新建Users表
if exists(select * from dbo.sysobjects where name = 'Users')
drop Users
go
create table Users(
id int primary key not null, //在建表的同时给id列添加主键约束
Name varchar not null unique, //在建表的同时给Name列添加唯一约束
Pwd varchar not null
)
go
新建Emp表
if(select * from dbo.sysobjects where name = 'Emp')
drop table Emp
go
create table Emp(
Eid int primary key not null,
Job varchar not null,
Sal money not null,
U_id int foreign key references Users(id ) //在建表的同时添加主外键约束。<Emp的foreign key 指向 Users的primary key>
)
go
如果表已经建好了,想要添上约束,可以用下面的语句:
添加主键约束:
alter table Users
Add primary key(id)
alter table Users
Add Consrtaint pk_id primary key(id)
撤销<mysql>
alter table Users
drop primary key
<oracle,sqlserver>
alter table Users
drop Consrtaint pk_id
添加唯一约束<单列>:
alter table Users
Add unique(Name)
<多列>
alter table Users
Add Constraint un_id Unique(id,Name)
撤销:<oracle,sqlserver,ms access>
alter table Users
drop Constraint un_id
<mysql>
alter table Users
drop index un_id
添加主外键约束:
alter table Emp
Add Constraint fk_constraint
foreign key(U_id)
references Emp(id)
撤销<oracle,sqlserver,ms access下>
alter table Emp
drop Constraint fk_constraint
<mysql 下>
alter table Emp
drop foreign key fk_constraint
添加Check约束:
alter table Users
add Consrtaint ck_no Check(id>0)
撤销:<sqlserver oracle ms access 下>
alter table Users
drop Consrtaint ck_no
<mysql下>
alter table Users
drop check ck_no
添加default约束<sqlserver oracle 下>
alter table Users
alter column Name set default = 'JEEP'
<mysql下>
alter table Users
alter Name set default = 'JEEP'
撤销<sqlserver oracle 下>
alter table Users
alter column Name drop default
<mysql下>
alter table Users
alter Name drop default
基本的增删改查:
增:
insert into Users values('Admin','123');
删:
delete from Users where id = 1;
查:
select * from Users
select Name from Users <查询一列的值>
distinct关键字
select distinct Name Users <返回唯一值>
or关键字
select * from Emp where job = ‘主管’ or sal = 6000
and关键字
select * from Emp where job = '员工' And sal = 5000
select * from Emp where (job = '实施' or sal =6000) and id = 1
order by 关键字
select * from Emp where order by Eid
select * from Emp where order by Eid desc
Top子句
select Top 1 * from Emp -------sqlserver
select top 50 percent * from Emp --sqlserver 查询50%的内容
select * from Emp where rownum <= 1 ---oracle
select * from Emp where limit 1 --mysql
Like子句
select * from Emp where sal like 'A%' -- 查询职务以’A‘开头的所有信息
select * from Emp where sal like '%A' -- 查询职务以‘A’结尾的所有信息
select * from Emp where sal like '%A%' -- 查询职务包含‘A’的所有信息
select * from Emp where sal not like '%A%' -- 查询职务里不包含‘A’字符的所有信息
通配符:
select * from Emp where sal like '_A_D_' --第二个字符是A和第四个字符是D的
select * from Emp where sal like '[ABC]%' --以ABC 任意一个开头的所有
select *from Emp where sql like '[!ABC]%' --不以ABC 任意一个开头的所有
IN关键字:
select * from Emp where Eid in(0,1)
Between 关键字
select * from Emp where Eid between 1 and 3 --Eid 介于 1 ~ 3 间
select * from Emp where Eid not between 1 and 3
AS关键字
select Eid as '编号', sal as '收入', job as '职务' from Emp
select e.id,e.sal from Emp as e
inner Join
select e.Eid,e.sal,e.job,u.Name from Emp as e inner join Users as u on u.id = e.U_id
left Join
select e.Eid,e.sal,u.Name from Users as u left join Emp as e on u.id = e.U_id ------会返回左表(Users)里的所有列 哪怕右表里没有匹配的项
rigth Join
select e.Eid,e.sal,u.Name from Users as u rigth jion Emp as e on u.id = e.U_id ------会返回右表(Emp )里的所有列 哪怕左表里没有匹配的项
full Join
select e.Eid,e.sal,u.Name from Users as u full jion Emp as e on u.id = e.U_id -----返回所有 不管有没有匹配的
union 鸡肋
select name from Student
union
select name from Teacher
select * into
select * into Test from Emp --备份
select e.sal,u.id into Test1 from Emp as e full jion Users as u on e.U_id = u.id
改:
Update Users set Name = 'Tom' where id = 2;
数据类型:
int(lenth) integer(lenth) smallint(lenth) tinyint(lenth)
char(lenth) varchar(lenth)
decimal(lenth,i) data(yyyy-mm-dd)
未完。。。。