java学习笔记之sql常用语句
来源:互联网 发布:软件评测师视频 编辑:程序博客网 时间:2024/05/18 03:41
DDL(Data Definition Language):数据库定义语句;
DML(Data Manipulation Language):用于增删改数据库中数据,不包括查询;
DCL(Data Control Language)用来向用户赋予/取消对数据对象的控制权限;
TCL(Transaction Control Language):用来对事务进行管理
数据库登录:
win+R运行cmd,输入 双引号里面的内容:"mysql -uroot -proot"回车,root为用户名和密码。
注意:执行win命令,最后不需要加“;”,但执行mysql语句则需要加“;”
一、库操作
1.创建名为"mydb1"数据库:
create database mydb1;
2.显示所有数据库:
show databases;
3.创建名为"mydb1"并带字符集的数据库:
create database mydb2 CHARACTER SET=utf8;
4.显示数据库mydb2创建语句:
show create database mydb2;
5.如果存在数据库mydb2,则删除mydb2(如果不加上if exits判断,一旦要删除的数据库不存在,就会报错):
drop database if exits mydb2;
6.修改数据库的库字符编码:
alter database mydb2 character set gb2312;
7.数据库备份(root是用户名,这是一个win命令(即打开cmd,直接输入),将数据库备份到c盘下):
mysqldump -u root -p mydb2>c://test.sql;
8.数据库恢复(两种方式):
(1)这是一个win命令:mysql -u root -p mydb2<c://test.sql
(2)source c://test.sql;
二、表操作
1.创建学生表:
create table student( /* 直接指定主键,也可单独指定*/ sno varchar(4) primary key, sname varchar(10) not null, sage int, ssex char(2), email varchar(20) unique, constraint ck_student_ssex_sage check(ssex in('男','女')and sage between 10 and 50) )
2.增加列(字段):
alter table tableName add columnName varchar(30);
3.修改列,改变长度:
alter table tableName modify cloumnName varchar(60);
4.删除列:
alter table tableName drop column cloumnName;
5.修改表名:
rename table tableName_old to tableName_new;
6.修改表的字符集:
alter table user character set gbk;
7.修改列名:
alter table tableName change column cloumnName columnName_new varchar(60);
8.删除表:
drop table tableName;
三、增删改查
准备表
create table employee( id int, name varchar(40), sex varchar(4), birthday date, entry_date date, salary decimal(8,2), resume text);
1.插入数据:
insert into employee(id,name,sex,birthday,entry_date,salary,resume) values(1,'zhangsan','male','1993-03-04','2016-11-10','1000','i am a developer');/*可以省略表字段,但是必须插入全部字段*/insert into employee values(1,'zhangsan','male','1993-03-04','2016-11-10','1000','i am a developer');
2.指定某些列插入数据:
insert into employee(id) values(6);
3.查看汉字时不乱码:
insert into employee(id,name) values(6,'张三'); /*告诉mysql客户采用gb2312编码*/ show variables like 'chara%'; set character_set_client=gb2312; insert into employee(id,username) values('3','张三'); /*查看汉字时不乱码*/ show variables like 'chara%'; set character_set_results=gb2312; select * from employee;
4.删除表数据:
/*删除表中名称为’zs’的记录*/delete from employee where name='zs';/*删除表中所有记录*/delete from employee;/*使用truncate删除表中记录(快速删除表中所有数据,保留表的数据结构)*/truncate table employee;
5.修改表中数据:
/*将所有员工薪水修改为5000元*/update employee set salary=5000;/*将姓名为’zs’的员工薪水修改为3000元*/update employee set salary = 3000 where name='zs';/*将姓名为’aaa’的员工薪水修改为4000元,job改为ccc*/update employee set salary = 4000,job='ccc' where name='aaa';/*将lisi的薪水在原有基础上增加1000元*/update employee set salary = salary+1000 where name='lisi';
6.查询表中数据:
/*查询表中所有学生的信息*/select id,name,chinese,english,math from student;/*查询表中所有学生的姓名和对应的英语成绩*/select name,english from student;/*过滤表中重复数据*/select distinct english from student;在所有学生分数上加10分特长分*/select name,(chinese+english+math)+10 from student;/*统计每个学生的总分*/select name,(chinese+english+math) from student;/*使用别名表示学生分数*/select name,(chinese+english+math) as 总分 from student;/*使用别名表示学生分数,可以不用as*/select name,(chinese+english+math) 总分 from student;/*查询姓名为张三的学生成绩*/select * from student where name='张三';/*查询英语成绩大于90分的同学*/select * from student where english>'90';/*查询总分大于200分的所有同学*/select name,(chinese+english+math) 总分 from student where chinese+english+math>200;/*查询英语分数在 80-90之间的同学*/select * from student where english>=80 and english=<90;select * from student where english between 80 and 90;/*查询数学分数为89,90,91的同学*/select * from student where math=89 or math=90 or math=91;select * from student where math in(89,90,91);/*查询所有姓李的学生成绩*/select * from student where name like '李%';select * from student where name like '李_';/*查询数学分>80,语文分>80的同学*/select * from student where math>80 and chinese>80;select * from student where chinese is null;/*对数学成绩排序(降序:从高到低,desc)后输出。*/select name,math from student order by math desc;/*对数学成绩排序(升序:从低到高,asc)后输出。*/select name,math from student order by math asc;/*对总分排序后输出,然后再按从高到低的顺序输出*/select name,math+english+chinese from student order by math+english+chinese desc;/*对姓李的学生成绩排序输出*/select name,math+english+chinese from student where name like '李%' order by math+english+chinese desc;/*统计一个班级共有多少学生*/select count(*) from student;select count(id) from student;/*统计数学成绩大于80的学生人数*/select count(*) from student where math>80;/*统计总分大于250的人数*/select count(*) from student where math+english+chinese>250;/*细节 null不能被count*/select count(chinese) from student;/*统计一个班级数学总成绩*/select sum(math) from student;/*统计一个班级语文、英语、数学各科的总成绩s*/elect sum(math),sum(english),sum(chinese) from student;/*统计一个班级语文、英语、数学的成绩总和*/select sum(math+english+chinese) from student;/*统计一个班级语文成绩平均分*/select sum(chinese)/count(*) from student;/*求一个班级数学平均分*/select avg(math) from student;/*求一个班级总分平均分*/select avg(math+english+chinese) from student;/*求班级最高分和最低分(数值范围在统计中特别有用)*/select max(math+english+chinese) from student;select min(math+english+chinese) from student;/*对订单表中商品归类后,显示每一类商品的总价*/select product,sum(price) from orders group by product;/*查询购买了几类商品,并且每类总价大于100的商品*/select product,sum(price) from orders group by product having sum(price)>100;
四、表约束
1.定义主键约束:
create table demo1( id int primary key, name varchar(40));
2.插入空id:
insert into demo1(name) values('aaa');/*报错,主键不能为空*/
3.插入重复的id:
insert into demo1(id,name) values(1,'aaa');/*报错,主键不能重复*/insert into demo1(id,name) values(1,'aaa');
4.显定义主键自动增长(auto_increment):
create table demo2( id int auto_increment primary key, name varchar(40));
5.定义唯一约束(unique):
create table demo3( id int auto_increment primary key, name varchar(40) unique /*unique:唯一约束*/);
6.定义非空(not null):
create table demo4( id int auto_increment primary key, name varchar(40) not null);
7.定义外键约束():
create table department( id int auto_increment primary key, name varchar(30) not null);create table employee2( id int auto_increment primary key, name varchar(30) not null, salary double, department_id int, /*外键约束*/ constraint department_id_FK foreign key(department_id) references department(id));
6.定义非空(not null):
create table demo4( id int auto_increment primary key, name varchar(40) not null);
7.删除主键:
alter table tablename drop primary key;
五、索引
创建索引
在执行CREATE TABLE语句时可以创建索引,也可以单独用CREATE INDEX或ALTER TABLE来为表增加索引。1.ALTER TABLE
ALTER TABLE用来创建普通索引、UNIQUE索引或PRIMARY KEY索引。
//INDEX(普通索引)ALTER TABLE table_name ADD INDEX index_name (column_name);//添加多列索引 ALTER TABLE table_name ADD INDEX index_name (column_name1,column_name2,column_name3 );//添加FULLTEXT(全文索引) ALTER TABLE table_name ADD FULLTEXT (column_name);//添加UNIQUE ALTER TABLE table_name ADD UNIQUE (column_name);ALTER TABLE table_name ADD PRIMARY KEY (column_name);
其中table_name是要增加索引的表名,column_list指出对哪些列进行索引,多列时各列之间用逗号分隔。索引名index_name可选,缺省时,MySQL将根据第一个索引列赋一个名称。另外,ALTER TABLE允许在单个语句中更改多个表,因此可以在同时创建多个索引。
2.CREATE INDEX
CREATE INDEX可对表增加普通索引或UNIQUE索引。
CREATE INDEX index_name ON table_name (column_name);CREATE UNIQUE INDEX index_name ON table_name (column_name);table_name、index_name和column_list具有与ALTER TABLE语句中相同的含义,索引名不可选。另外,不能用CREATE INDEX语句创建PRIMARY KEY索引。
3.索引类型
在创建索引时,可以规定索引能否包含重复值。如果不包含,则索引应该创建为PRIMARY KEY或UNIQUE索引。对于单列惟一性索引,这保证单列不包含重复的值。对于多列惟一性索引,保证多个值的组合不重复。
PRIMARY KEY索引和UNIQUE索引非常类似。事实上,PRIMARY KEY索引仅是一个具有名称PRIMARY的UNIQUE索引。这表示一个表只能包含一个PRIMARY KEY,因为一个表中不可能具有两个同名的索引。
下面的SQL语句对students表在sid上添加PRIMARY KEY索引。
ALTER TABLE students ADD PRIMARY KEY (sid);
删除索引
可利用ALTER TABLE或DROP INDEX语句来删除索引。类似于CREATE INDEX语句,DROP INDEX可以在ALTER TABLE内部作为一条语句处理,语法如下。DROP INDEX index_name ON talbe_name;ALTER TABLE table_name DROP INDEX index_name;ALTER TABLE table_name DROP PRIMARY KEY;其中,前两条语句是等价的,删除掉table_name中的索引index_name。
第3条语句只在删除PRIMARY KEY索引时使用,因为一个表只可能有一个PRIMARY KEY索引,因此不需要指定索引名。如果没有创建PRIMARY KEY索引,但表具有一个或多个UNIQUE索引,则MySQL将删除第一个UNIQUE索引。如果从表中删除了某列,则索引会受到影响。对于多列组合的索引,如果删除其中的某列,则该列也会从索引中删除。如果删除组成索引的所有列,则整个索引将被删除
- java学习笔记之sql常用语句
- Oracle学习笔记之DBA常用SQL语句
- SQL学习笔记:常用SQL语句操作
- Java学习笔记--sql语句
- PB学习笔记之SQL语句
- SQL学习笔记(1)之DML语句
- SQL学习笔记(2)之DDL语句
- [学习笔记]MySql之SQL语句优化
- 数据库学习笔记之SQL语句
- 学习笔记之 MySQL中优化sql语句查询常用的30种方法
- SQL常用语句学习
- SQL学习----SQL常用语句
- sql语句学习笔记(6)-常用字符串操作函数
- Sql语句学习笔记(7)-常用日期函数
- MySQL学习笔记(一):基础常用SQL语句
- SQL语句学习笔记
- SQL语句学习笔记
- 常用SQL操作语句笔记
- RBAC新解:基于资源的权限管理(Resource-Based Access Control)
- c++中必须在类初始化列表中初始化的几种情况
- css的基本排版与元素分类简述
- 作为测试,怎么样才能确切知道程序哪有风险呢?
- 树点分治与树链刨分
- java学习笔记之sql常用语句
- 欢迎使用CSDN-markdown编辑器
- 阅读笔记一(Android系统创建桌面快捷方式ShortCut)
- UVA 10564 Paths through Hourglass(dp)
- NSDate那点事(时间处理封装)
- 实现业务系统中的用户权限管理
- Linux下的文件管理函数(不带I/O缓冲)2.0
- android编程取消标题栏方法(appcompat_v7、Theme.NoTitleBar)
- 复习--JS基础