创建网站开发公司,中南建设集团有限公司,网页设计论文目录,服务网点网站建设#xff08;一#xff09;基础
创建数据库#xff0c;数据库名为shujuku01
SQL 对大小写不敏感#xff1a;SELECT 与 select 是相同的create database shujuku01创建表#xff0c;if not exists表示如果表不存在则创建#xff0c;存在则不创建create database if not e…一基础
创建数据库数据库名为shujuku01
SQL 对大小写不敏感SELECT 与 select 是相同的create database shujuku01创建表if not exists表示如果表不存在则创建存在则不创建create database if not exists shujuku01;
创建表表名为biao01
create table biao01( biaoid int not null primary key, name varchar(30) not null, money varchar(30) not null, waijianid int not null )
not null 不为空约束primary key 主键约束unique唯一约束确保一列的每一行必须具有唯一值default默认约束foreign key 外键约束check检查约束AUTO_INCREMENT 自增
insert into插入表
insert into biao01(biaoid,name,money,waijianid)value(2,“二号”,102,2);
显示表所有列的信息
describe biao01;
select 查询表数据
select * from biao01 查询biao01 表所有信息 select biaoid,name,money from biao01 查询表部分信息
where过滤
where子句允许运算符,, ,like,in,not in,between…and…in运算符是逻辑运算符用于检查一组值中是否存在特定值 IN运算符是逻辑运算符用于检查一组值中是否存在特定值in(100,300)检查money是否存在100或者300这两个特定的值。
and 和or order by子句用于按升序或降序对查询返回的数据进行排序
asc升序desc倒序
TOP子句用于限制返回的行数,一些数据库不支持top支持limit
select top 3 * from biao01select * from biao01 limit 3
distinct子句用于从结果集中删除重复的行,查询的时候不显示重复数据 update 更新已存在数据 delete 按条件删除数据可以删除所有数据 delete from biao01;#删除表中所有数据
truncate 清空表
truncate table biao01;
drop 删除表可一次性删除多个
drop table biao01,biao02;
二SQL链接
前期准备一个教师表一个学生表一个班级表。 教师表五个老师wang01-wang05 学生表八个学生sthdent01-08 班级表三个班级 class01-03 相关代码如下 create table if not exists teacher(tid int not null primary key auto_increment,tmane varchar(30),tmoney varchar(30),tclassid int
);
create table if not exists student(sid int not null primary key auto_increment,sname varchar(30),sclassid int
);
create table if not exists class(cid int not null primary key,cname varchar(30)
)insert into teacher(tid,tname,tmoney,tclassid)value(101,wang01,1000,301);
insert into teacher(tid,tname,tmoney,tclassid)value(102,wang02,2000,302);
insert into teacher(tid,tname,tmoney,tclassid)value(103,wang03,3000,303);
insert into teacher(tid,tname,tmoney,tclassid)value(104,wang04,4000,304);
insert into teacher(tid,tname,tmoney,tclassid)value(105,wang05,5000,305);insert into student(sid,sname,sclassid)value(201,sthdent01,301);
insert into student(sid,sname,sclassid)value(202,sthdent02,301);
insert into student(sid,sname,sclassid)value(203,sthdent03,301);
insert into student(sid,sname,sclassid)value(204,sthdent04,301);
insert into student(sid,sname,sclassid)value(205,sthdent05,301);
insert into student(sid,sname,sclassid)value(206,sthdent06,301);
insert into student(sid,sname)value(207,sthdent07);
insert into student(sid,sname,sclassid)value(208,sthdent08,302);insert into class(cid,cname)value(301,class01);
insert into class(cid,cname)value(302,class02);
insert into class(cid,cname)value(303,class03);
join是用来链接两个表的inner join 内连接 外连接 左连接left join 外连接 右链接right join 交叉链接cross join 外连接 完全链接full join。MySQL不支持全连接但是Oracle支持。MySQL实现全连接需要使用关键字union或者union all前者可以去重后者不去重union和union all使用时select下的字段数量必须一致否则会报错
select * from teacher
union all
select * from student
三SQL高级