SQL基础语法
假设有两张表class和achevement,key 为student_id
1、alias 为列名称和表名称指定别名
例子,查找学生LiMing的语文和数学成绩
select c.student_id,c.name,a.math,a.chinese
from class as c,achevement as a --表的别名
where c.name='LiMing'
--
select c.student_id as Id,name as Student_name --列的别名
from class
2、join 关联两张以上数据表
select c.student_id,c.name,a.math,a.english
from class as c,achevement as a
where c.student_id= a.student_id
-- join
select c.student_id,c.name,a.math,a.english
from class as c
inner join achevement as a
on c.student_id = a.student_id
order by student_id
-- left join 从左表返回所有的行
-- right join 从右表返回所有的行
-- full join 有匹配则返回
3、union 用于合并多个select语句结果集
例如取class1和class2学生姓名
select name from class1
union
select name from class2
--union 只会选取不同的值,如两个班级有同名学生,只会显示一次
-- union all 要显示重复值,使用union all
4、select into 用于创建表的复件
select *
into class_backup -- 制作class的备份
from class
-- 选取北京的学生
select student_id,name,city
into class_back_up1
from class
where city='Beijing'
5、creat db 创建数据库
create database mydb -- 建mydb数据库
create table class -- 建立空数据表
(
student_id int,
name varchar(255),
city varchar(255),
height numeric(250,2)
)
6、SQL约束 创建表时规定表的数据类型
1)not full 不接受null值
create table class -- 建立空数据表
(
student_id int not null,
name varchar(255),
city varchar(255),
height numeric(250,2)
)
- unique
create table class
(
student_id int not null,
name varchar(255),
city varchar(255),
height numeric(250,2),
unique (student_id)
)
--已成立的表,增加unique约束
alter table class
add unique (student_id)
--撤销unique约束
alter table class
drop index student_id
- primary key 约束
create table class
(
student_id int not null,
name varchar(255),
city varchar(255),
height numeric(250,2),
primary key (student_id)
)
- froeign key 约束
Create table class
(
class_id int not null,
student_id int not null,
name varchar(255),
primary key (student_id),
froeign key (class_id) references achevement(class_id)
)
- check 约束 检查值是否处于限制范围
例如建立class表时要求age 必须大于6
create table class
(
student_id int not null,
name varchar(255) not null,
city varhar(255),
age int,
check(age>6)
)
6)default 约束 向列中插入默认值
例如建立class表,默认学生为北京学生
create table
(
student_id int not null,
name varchar(255) not null,
age int,
city varchar(255) default 'BeiJing'
)
7、create index
例如在学生姓名列创建索引
create index class_index
on class(name)
create index class_index
on class(name desc) -- 索引姓名降序排列
create index class_index
on class(name,student_id) -- 多个索引
8、drop 删除
drop table class -- 删除表(包括表数据、结构、索引)
drop database mydb -- 删除数据库
truncate table class --仅删除数据,保留表结构
9、alter 更改
alter table class
add birthday date --增加新列,类型为date
alter table class
alter column birthday year --改变某列的数据类型
alter table class
drop column birthday --删除某列
10、auto increment 在新纪录插入表中时生成唯一的数字
例如,把class表中的studeny_id 定义为auto increment主键
create table class
(
student_id int not null auto_increment, -- 默认开始值为1
name varchar(255),
age int,
primary key (student_id)
)
alter table class auto_increment=10 -- 改变起始值
11、is null 和 is not null
select student_id,name,city
from class
where city is null --取空值
where city is not null --不为空
12、常用数据类型
以Mysql为例
1)文本型:
char() 保存固定长度 ,最多255个字符
varchar() 保存可变长度,最多255个字符
tintext 最长255个字符串
text 最长65535个字符串
blob 最长65535个字符串
2)数值型:
tinyint() 0-255
smallint() 0-65535
mediumint
int
bigint
float(,) 带有浮动小数点的小数字
double(,)带有浮动小数点的大数字
decimal(,)作为字符串存储的double类型,允许固定的小数点