mysql基本语法

----~~~~~~~~~~~~创建数据库~~~~~~~~~~~~~~~~~~
create database  数据库名;
---~~~~~~~~~~~~~使用数据库~~~~~~~~~~~~~~~~~
use 数据库名
 
---~~~~~~~~~~~~~~~~删除数据库~~~~~~~~~~~~~~
drop database 数据库名;
 
--~~~~~~~~~~~~~~~~~查看数据库中的表~~~~~~~~~~~
show tables 
 
---~~~~~~~~~~~检查数据库是否存在~~~~~~~~~~~~~~~
drop table if exists 表名;
drop table if exists student ; 
 
--~~~~~~~~~~~~~~~~创建表~~~~~~~~~~~~~~~~~
---创建学生表
create table student 
(
  stuID int auto_increment,
  stuName varchar(32),
  stuAge int, 
  deptID int,
  primary key(stuID)
)engine=innodb charset=utf8;
 
--创建课表
create table course 
(
courseID int primary key,
courseName varchar(32)
)engine=innodb charset=utf8;
  
--创建分数表 
create table sc
(
stuID int,
courseID int,
score float,
primary key(stuID,courseID),     ----创建一个主键
foreign key(stuID) references course(courseID)   ----创建一个外键
)engine=innodb charset=utf8
 
 
--~~~~~~~~~~~~~~插入数据~~~~~~~~~~~~~~~~~~
--给课程表插入数据
insert into course(courseID,courseName) values
(1,'语文'),
(2,'数学'),
(3,'英语');
 
--给分数表插入数据
insert into sc(stuID,courseID,score)values 
(1,1,100),
(1,2,89),
(1,3,99),
(2,1,100),
(2,2,66),
(2,3,77);
----- ~~~~~~~~~查看表中数据~~~~~~~~~~~~~~~~~~~
---查看student表中的数据
select * from student;
 
---使用distinct关键字,可以将重复行去掉,distinct必须放在列名前面
select distinct depart from staff;
 
---使用limit子句可以限制返回前几行数据
select name from staff limit 3;(查询前三行的数据)
---查询name列的第三行开始的两行数据
select name from staff limit 3,2;
 
-----使用完全限定表名查询(如果在其他数据库中查询表名,就需要使用完全限定表名)
select staff.name from lianxi.staff;
 
----数据检索排序(关键字order by)
select * from pers_info order by wage;(按照薪水升序排列) 
----指定排序,升序是默认排序可以使用desc指定降序排序
select * from per_info order by wage desc;(按照薪水降序排列)
---对多列数据指定降序排序,必须每个列都要指定desc关键字
select * from per_info order by wage desc,wage desc;(当tel降序排序相同时就按wage降序排序)
--查找最小值和最大值
select * from per_innfo order by wage limit 1;(查找工资最大值)
select * from per_innfo order by wage  desc limit 1;(查找工资最小值)
 
---~~~~~~~~~~~~where条件语句
select * from per_info where id=3;
select id,name,from staff where name='wang';
--~~~~~~~~~~~~where 子句中常用操作符  
--  = 等于    <> != 不等于    <小于   <=小于   >大于  >=大于等于
select *from per_info where wage>3000;(查询薪水大于三千)
---~~~~~~~~~between...and....范围查询
select * from per_info where  wage between 3000 and 5000;(查询薪水3千到5千的)
--查询空值null 
select * from per_info  where age is null ; 
---多个条件可以通过and连接
select * from per_info where wage>3000 and age is not null;
--or语句只满足一个条件就行
select * from per_info where wage<3000 or  age is not null;
--()优先级大于 and 优先级高于or
select * from per_info where wage<3000 (id <4 or id >9);
--in 操作符 指定条件范围查询
select * from per_info where id in (2,4,5);(查询id为2,4,5的)

 

posted on 2013-07-30 11:47  星空、寒夜  阅读(96)  评论(0)    收藏  举报

导航