SQL语句入门级(MySQL测试)
来源:互联网 发布:mac 画流程图的软件 编辑:程序博客网 时间:2024/05/22 06:36
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 from 表 where id=1 and name='happyteemo' //删除ID =1 和name='happyteemo' 那一行数据
3、改
update 表 set 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 peoplewhere 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);
- SQL语句入门级(MySQL测试)
- mysql执行SQL脚本,SQL语句入门
- Mysql使用入门(一)sql语句分类
- PHP MYSQL精确测试SQL语句执行时间
- mysql测试数据库employees一些sql语句
- mysql测试数据库employees一些sql语句
- MySQL安装和SQL语句入门
- sql语句(mysql)
- SQL入门(mysql)
- 常用SQL语句(mysql)
- sql语句(windows mysql)
- 常用SQL语句(Mysql)
- MySQL (1)SQL语句
- 常用sql语句(mysql)
- 测试mysql的sql语句预编译效果
- 入门级基本SQL语句学习(一)
- 入门级基本SQL语句学习(二)
- 测试SQL语句执行时间
- 移动端开发——zepto和jquery mobile的区别
- 使用纯文本方式编写软件设计文档
- spring——xml文件配置
- wifi一键配网smartconfig原理及应用
- 打包时候能转动camera的脚本
- SQL语句入门级(MySQL测试)
- Oracle 误操作删除表的恢复方法
- 四种数据库随机获取10条数据的方法
- Codeforecs 808E Selling Sovenirs 枚举+DP(超大背包)
- Linux内存管理(三)——用户空间管理
- 读-李林峰-分布式服务框架和原理1-7
- Python自学笔记1
- 蓝桥杯java第三届决赛第一题--星期日
- iOS学习之路06