shell>mysql -u 用户名 -p Enter password: 或者 shell>mysql -u 用户名 -p 密码
mysql> QUIT
mysql> show databases ;
mysql> create database abc; Query OK, 1 row affected (0.00 sec)
<span style="color:#000000;">格式: mysql> create table 表名( 字段1 数据类型 , 字段2 数据类型 , ...... 字段n 数据类型 , );</span>
mysql> use test1; 结果:Database changed 否则显示:1046 - No database selected
mysql> create table student( sno varchar(20) not null default '', sname varchar(30), difdate date, grade varchar(10) )ENGINE=InnoDB DEFAULT CHARSET=utf8; create table course( cno varchar(20) not null default '', cname varchar(20) )ENGINE=InnoDB DEFAULT CHARSET=utf8; create table score( sno varchar(20) not null default '', cno varchar(20) , score double )ENGINE=InnoDB DEFAULT CHARSET=utf8; 结果: Query OK, 0 rows affected Query OK, 0 rows affected Query OK, 0 rows affected
mysql> CREATE DATABASE test1 CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';b.然后创建表时设置编码格式
create table student( sno varchar(20) not null default, sname varchar(30), difdate date, grade varchar(10) )ENGINE=InnoDB DEFAULT CHARSET=utf8;
<span style="color:#FF0000;">注:</span>mysql中的插入日期格式为‘YYYY-MM-DD’,而oracle中 格式为‘DD-M月-YYYY’;字符、汉字、日期需要用单引号; 格式: 1.插入部分字段 mysql>insert into 表名 (字段1,字段2……字段n) values(字段1值,字段2值,……) 2.插入全部的字段,可以省略字段名,如: mysql>insert into 表名 values(字段1值,字段2值,……)
*表1 mysql> insert into student values('1','李丽','1993-02-03','2007'); insert into student values('2','王明','1993-04-02','2008'); insert into student values('3','李云','1994-03-12','2009'); insert into student values('4','李四','1992-02-13','2007'); insert into student values('5','王五','1992-04-13','2010'); insert into student values('6','赵六','1992-03-28','2009'); insert into student values('7','张三','1992-08-03','2008'); *表2 mysql>insert into course(cno,cname) values('1','语文'); insert into course(cno,cname) values('2','大学英语'); insert into course(cno,cname) values('3','数学'); insert into course(cno,cname) values('4','物理'); insert into course(cno,cname) values('5','生物'); *表3 mysql>insert into score (sno,cno,score) values('1','1',69); insert into score (sno,cno,score) values('1','2',40); insert into score (sno,cno,score) values('1','3',67); insert into score (sno,cno,score) values('1','4',79); insert into score (sno,cno,score) values('1','5',89); insert into score (sno,cno,score) values('2','1',97); insert into score (sno,cno,score) values('2','2',69); insert into score (sno,cno,score) values('2','3',39); insert into score (sno,cno,score) values('2','4',90); insert into score (sno,cno,score) values('2','5',99); insert into score (sno,cno,score) values('3','1',99); insert into score (sno,cno,score) values('3','2',69); insert into score (sno,cno,score) values('3','3',39); insert into score (sno,cno,score) values('3','4',69); insert into score (sno,cno,score) values('3','5',99); insert into score (sno,cno,score) values('4','1',99); insert into score (sno,cno,score) values('4','2',69); insert into score (sno,cno,score) values('4','3',39); insert into score (sno,cno,score) values('4','4',69); insert into score (sno,cno,score) values('4','5',99); insert into score (sno,cno,score) values('5','1',79); insert into score (sno,cno,score) values('5','2',70); insert into score (sno,cno,score) values('5','3',39); insert into score (sno,cno,score) values('5','4',69); insert into score (sno,cno,score) values('5','5',99); insert into score (sno,cno,score) values('6','1',99); insert into score (sno,cno,score) values('6','2',69); insert into score (sno,cno,score) values('6','3',39); insert into score (sno,cno,score) values('6','4',69); insert into score (sno,cno,score) values('6','5',80); insert into score (sno,cno,score) values('7','1',39); insert into score (sno,cno,score) values('7','2',69); insert into score (sno,cno,score) values('7','3',89); insert into score (sno,cno,score) values('7','4',64); insert into score (sno,cno,score) values('7','5',59);
*表1 mysql> select * from student ; 结果: +-----+-------+------------+-------+ | sno | sname | difdate | grade | +-----+-------+------------+-------+ | 1 | 李丽 | 1993-02-03 | 2007 | | 2 | 王明 | 1993-04-02 | 2008 | | 3 | 李云 | 1994-03-12 | 2009 | | 4 | 李四 | 1992-02-13 | 2007 | | 5 | 王五 | 1992-04-13 | 2010 | | 6 | 赵六 | 1992-03-28 | 2009 | | 7 | 张三 | 1992-08-03 | 2008 | +-----+-------+------------+-------+ 7 rows in set *表2 mysql> select * from course; 结果: +-----+----------+ | cno | cname | +-----+----------+ | 1 | 语文 | | 2 | 大学英语 | | 3 | 数学 | | 4 | 物理 | | 5 | 生物 | +-----+----------+ 5 rows in set *表3 mysql> select * from score ; +-----+-----+-------+ | sno | cno | score | +-----+-----+-------+ | 1 | 1 | 69 | | 1 | 2 | 40 | | 1 | 3 | 67 | | 1 | 4 | 79 | | 1 | 5 | 89 | | 2 | 1 | 97 | | 2 | 2 | 69 | | 2 | 3 | 39 | | 2 | 4 | 90 | | 2 | 5 | 99 | | 3 | 1 | 99 | | 3 | 2 | 69 | | 3 | 3 | 39 | | 3 | 4 | 69 | | 3 | 5 | 99 | | 4 | 1 | 99 | | 4 | 2 | 69 | | 4 | 3 | 39 | | 4 | 4 | 69 | | 4 | 5 | 99 | | 5 | 1 | 79 | | 5 | 2 | 70 | | 5 | 3 | 39 | | 5 | 4 | 69 | | 5 | 5 | 99 | | 6 | 1 | 99 | | 6 | 2 | 69 | | 6 | 3 | 39 | | 6 | 4 | 69 | | 6 | 5 | 80 | | 7 | 1 | 39 | | 7 | 2 | 69 | | 7 | 3 | 89 | | 7 | 4 | 64 | | 7 | 5 | 59 | +-----+-----+-------+ 35 rows in set
格式: 1:个别字段查询 select 字段1,字段2 ...... from 表名 2:显示表中的全部内容 select * from 表名 3:限定条件查询 select 字段1,字段2 ...... from 表名 where 条件 范例:查询2007级姓李的学生信息 mysql> select * from student where sname like '李%' and grade=2007; 结果: +-----+-------+------------+-------+ | sno | sname | difdate | grade | +-----+-------+------------+-------+ | 1 | 李丽 | 0000-00-00 | 2007 | | 4 | 李四 | 0000-00-00 | 2007 | +-----+-------+------------+-------+ 2 rows in set 4:分组查询 select 字段1,字段2 ...... from 表名 [where 条件] group by 字段 此语句表示按照这个指定的字段进行分组,一般和sql的合计函数一起使用,如sum(),count(),avg(),min(),max(),Var 和 VarP 函数等 <pre name="code" class="sql">范例:统计每个年级的人数 mysql> select grade,count(sno) from student group by grade; 结果: +-------+------------+ | grade | count(sno) | +-------+------------+ | 2007 | 2 | | 2008 | 2 | | 2009 | 2 | | 2010 | 1 | +-------+------------+ 4 rows in set5:限定分组后的字段显示条件查询
select字段1,字段2 ...... from 表名 [where 条件] [group by 分组字段 having 条件] order by 字段a [asc|desc],字段b [asc|desc],...... 表示按照字段a升序排序或降序排序;若字段a相同,则按照字段b升序排序或降序排序;若字段b相同,则按照字段 ....升序排序或降序排序;......
1:内链接 inner join 范例:查询<span style="color:#222222;">2007</span><span style="color:#222222;">级的各学生的平均成绩</span><span style="color:#222222;">,没有成绩的为</span><span style="color:#222222;">0</span><span style="color:#222222;">;</span> <strong><span style="color:#6600CC;">a)</span></strong>先作笛卡尔积 mysql> select * from student s,score sc where s.sno=sc.sno; +-----+-------+------------+-------+-----+-----+-------+ | sno | sname | difdate | grade | sno | cno | score | +-----+-------+------------+-------+-----+-----+-------+ | 1 | 李丽 | 1993-02-03 | 2007 | 1 | 1 | 69 | | 1 | 李丽 | 1993-02-03 | 2007 | 1 | 2 | 40 | | 1 | 李丽 | 1993-02-03 | 2007 | 1 | 3 | 67 | | 1 | 李丽 | 1993-02-03 | 2007 | 1 | 4 | 79 | | 1 | 李丽 | 1993-02-03 | 2007 | 1 | 5 | 89 | | 2 | 王明 | 1993-04-02 | 2008 | 2 | 1 | 97 | | 2 | 王明 | 1993-04-02 | 2008 | 2 | 2 | 69 | | 2 | 王明 | 1993-04-02 | 2008 | 2 | 3 | 39 | | 2 | 王明 | 1993-04-02 | 2008 | 2 | 4 | 90 | | 2 | 王明 | 1993-04-02 | 2008 | 2 | 5 | 99 | | 3 | 李云 | 1994-03-12 | 2009 | 3 | 1 | 99 | | 3 | 李云 | 1994-03-12 | 2009 | 3 | 2 | 69 | | 3 | 李云 | 1994-03-12 | 2009 | 3 | 3 | 39 | | 3 | 李云 | 1994-03-12 | 2009 | 3 | 4 | 69 | | 3 | 李云 | 1994-03-12 | 2009 | 3 | 5 | 99 | | 4 | 李四 | 1992-02-13 | 2007 | 4 | 1 | 99 | | 4 | 李四 | 1992-02-13 | 2007 | 4 | 2 | 69 | | 4 | 李四 | 1992-02-13 | 2007 | 4 | 3 | 39 | | 4 | 李四 | 1992-02-13 | 2007 | 4 | 4 | 69 | | 4 | 李四 | 1992-02-13 | 2007 | 4 | 5 | 99 | | 5 | 王五 | 1992-04-13 | 2010 | 5 | 1 | 79 | | 5 | 王五 | 1992-04-13 | 2010 | 5 | 2 | 70 | | 5 | 王五 | 1992-04-13 | 2010 | 5 | 3 | 39 | | 5 | 王五 | 1992-04-13 | 2010 | 5 | 4 | 69 | | 5 | 王五 | 1992-04-13 | 2010 | 5 | 5 | 99 | | 6 | 赵六 | 1992-03-28 | 2009 | 6 | 1 | 99 | | 6 | 赵六 | 1992-03-28 | 2009 | 6 | 2 | 69 | | 6 | 赵六 | 1992-03-28 | 2009 | 6 | 3 | 39 | | 6 | 赵六 | 1992-03-28 | 2009 | 6 | 4 | 69 | | 6 | 赵六 | 1992-03-28 | 2009 | 6 | 5 | 80 | | 7 | 张三 | 1992-08-03 | 2008 | 7 | 1 | 39 | | 7 | 张三 | 1992-08-03 | 2008 | 7 | 2 | 69 | | 7 | 张三 | 1992-08-03 | 2008 | 7 | 3 | 89 | | 7 | 张三 | 1992-08-03 | 2008 | 7 | 4 | 64 | | 7 | 张三 | 1992-08-03 | 2008 | 7 | 5 | 59 | +-----+-------+------------+-------+-----+-----+-------+ 35 rows in set <strong> <span style="color:#000099;">b)</span><span style="background-color: rgb(0, 153, 0);"></span></strong>再添加条件 mysql>select * from student s,score sc where s.sno=sc.sno and s.grade='2007' ; 结果: +-----+-------+------------+-------+-----+-----+-------+ | sno | sname | difdate | grade | sno | cno | score | +-----+-------+------------+-------+-----+-----+-------+ | 1 | 李丽 | 1993-02-03 | 2007 | 1 | 1 | 69 | | 1 | 李丽 | 1993-02-03 | 2007 | 1 | 2 | 40 | | 1 | 李丽 | 1993-02-03 | 2007 | 1 | 3 | 67 | | 1 | 李丽 | 1993-02-03 | 2007 | 1 | 4 | 79 | | 1 | 李丽 | 1993-02-03 | 2007 | 1 | 5 | 89 | | 4 | 李四 | 1992-02-13 | 2007 | 4 | 1 | 99 | | 4 | 李四 | 1992-02-13 | 2007 | 4 | 2 | 69 | | 4 | 李四 | 1992-02-13 | 2007 | 4 | 3 | 39 | | 4 | 李四 | 1992-02-13 | 2007 | 4 | 4 | 69 | | 4 | 李四 | 1992-02-13 | 2007 | 4 | 5 | 99 | +-----+-------+------------+-------+-----+-----+-------+ 10 rows in set <strong><span style="color:#6600CC;">c)</span></strong>有重复,所以要进行分组 mysql> select s.sno ,s.sname , avg(sc.score) from student s,score sc where s.sno=sc.sno and s.grade='2007' group by s.sno,s.sname; 结果: +-----+-------+---------------+ | sno | sname | avg(sc.score) | +-----+-------+---------------+ | 1 | 李丽 | 68.8 | | 4 | 李四 | 75 | +-----+-------+---------------+ 2 rows in set 2:外连接 outer join a)左连接 left join b)右连接 right join c) 3:交叉连接
mysql> update course set cname='高等数学' where cname='数学'; Query OK, 1 row affected Rows matched: 1 Changed: 1 Warnings: 0查看结果:
mysql> select * from course; +-----+----------+ | cno | cname | +-----+----------+ | 1 | 语文 | | 2 | 大学英语 | | 3 |<span style="color:#FF0000;"> 高等数学 </span>| | 4 | 物理 | | 5 | 生物 | +-----+----------+ 5 rows in set
mysql> update score sc inner join course c<span style="color:#FF0000;"> inner join</span> student s <span style="color:#FF0000;">on</span> s.sno=sc.sno and c.cno=sc.cno set score=5+sc.score where s.grade='2007'and c.cname='数学'; Query OK, 2 rows affected Rows matched: 2 Changed: 2 Warnings: 0
mysql> delete sc.* from score sc <span style="color:#FF0000;">inner join </span>student s inner join course c <span style="color:#FF0000;">on</span> s.sno=sc.sno and sc.cno=c.cno where s.sname='张三' and c.cname='语文'; Query OK, 1 row affected
mysql> alter table student add constraint pk_sno primary key(sno); Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 02.增加外键
mysql> alter table score add constraint fk_sno foreign key(sno) references student(sno);
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u010270403/article/details/48001945