SQL语句入门级(MySQL测试)

1.对表的操作

1、查看表

show tables; # 查看数据库全部表
select * from 表名; # 查看一张表所有内容

2、创建表

create table course(cno int unsigned,name varchar(30));
//course是表名,cno,name是字段名,int unsigned是数据类型
create table customer(cno int auto_increment primary key,name varchar(30),password varchar(50));   
//primary key表示是主键,auto_increment表示自动增长,比如ID,每增加一个就自增,则每个ID都不一样了。
//注意:1、对于自增列,必须是索引(含主键)2、对于自增可以设置步长和起始值
//DEFAULT可以用来添加默认值,比如是理科班,就可以性别默认值设定为男

主键可以创建表时添加也可以后面更改。主键只是约束的一种:

约束(Constraint):对输入的数据进行合法性校验,保证数据的完整性
约束分为五类:
1.非空约束(NOT NULL)
2.唯一约束(UNIQUE)
3.主键约束(PRIMARY KEY):非空并且唯一
4.外键约束(FOREIGN KEY):一个字段的取值受限于另一个表的某个字段
5.检查约束(CHECK):取值范围限定

//创建表时设置
create table student (
sno int auto_increment primary key,
a int not NULL,
b int unique,
age check(age>10 and age<100));

3、删除表

drop table 表名

4、清空表内容

delete from 表名
truncate table 表名

5、修改表

添加列: alter table 表名 add 列名 类型
删除列: alter table 表名 drop column 列名
修改列:
alter table 表名 modify column 列名 类型; – 类型
alter table 表名 change 原列名 新列名 类型; – 列名,类型

alter table student add column birth varchar(30);
alter table t_book modify name varchar(22);
alter table student change column name2 name varchar(30);

修改列名:

修改列名MySQL: alter table bbb change nnnnn hh int;
修改列名SQLServer:exec sp_rename't_student.name','nn','column';
修改列名Oracle:lter table bbb rename column nnnnn to hh int;

添加主键:
alter table 表名 add primary key(列名);
删除主键:
alter table 表名 drop primary key;
alter table 表名 modify 列名 int, drop primary key;
添加外键: alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
删除外键: alter table 表名 drop foreign key 外键名称
修改默认值:ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

2.对内容的操作

1、增

insert into 表 (列名,列名…) values (值,值,…)
insert into 表 (列名,列名…) values (值,值,…),(值,值,值…)
insert into 表 (列名,列名…) select (列名,列名…) from 表
例:

insert into tab1(name,email) values('happyteemo','happyteemo.com')

2、删

delete from 表 # 删除表里全部数据

delete fromwhere id=1 and name='happyteemo' 
//删除ID =1 和name='happyteemo' 那一行数据

3、改

updateset name = 'happyteemo' where id>1

4、查

查询是数据库最复杂也是最有用的一个部分,这里只是做入门级介绍。

////////查行////////
select * from student;
select * from student where name = 'zhang%'
select * from student where name like 'zhang%';
//模糊查询 %为通配符表示0个或多个字符,_表示1个任意字

//////分组查询:使用group by子句实现/////////
select avg(sight) from student;//求平均值
select sex,avg(sight) 平均视力 from student group by sex;//看男女的视力
select sex,avg(sight) 平均视力 from student group by sex having sex = '0';
//只看男生视力,having子句只能和group by一起使用

//////条件查询///////
select * from student where sight is in(4.3,4.5);//视力在4.3-4.5之间的
select * from student where left(phone,3)='186';//电话前三位是186的
select * from student where left(phone,3) in('131','185');//结合应用

///////distinct(去除重复列) 和 limit(解决TOP N的问题) 关键字//////
select distinct sex from student;//只看性别
select * from student order by birth desc;//列出所有按生日排序
select * from student order by birth desc limit 3;//列出前三

////////查找重复项:////////
select * from people
where peopleId in (select   peopleId from   people group by   peopleId having count(peopleId) > 1)

//////多表查询,笛卡尔集//////
select *from course,sc;
select *from course,sc where sno =200101 and name='c' and course.cno=sc.cno;

select USERS.OID as u_oid,ORGAN.OID as o_oid  from USERS,ORGAN where USERS.US_JGDM=ORGAN.OG_CODE
//联合查询最好重命名。

/////嵌套查询,子查询//////
///将查询结果集作为一个临时表或一个集合或者一个单值来看待。
select * from student where sno not in (select distinct sno from sc);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的提千万

江山父老能容我,不使人间造孽钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值