(2)使用命令时delete from student时会出现报错:
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.0.000 sec
执行该语句 SET SQL_SAFE_UPDATES = 0 进入非安全模式即可
还原到安全模式,执行该语句 SET SQL_SAFE_UPDATES = 1 进入安全模式
(3)alter database test character set utf8 更改数据库的编码方式
(4)show variables like 'character%'; 查看编码方式
其中,
character_set_client为客户端编码方式;
character_set_connection为建立连接使用的编码;
character_set_database数据库的编码;
character_set_results结果集的编码;
character_set_server数据库服务器的编码;
只要保证以上五个采用的编码方式一样,就不会出现乱码问题。
修改语句:
setcharacter_set_client=utf8;
setcharacter_set_connection=utf8;
setcharacter_set_database=utf8;
setcharacter_set_results=utf8;
setcharacter_set_server=utf8;
<1>登陆账户
<span style="font-size:18px;"> mysql -u root -p</span>
登陆具有root权限的账户,密码默认为空<2>创建数据库
<span style="font-size:18px;">create database <数据库名称>;</span>
<3>删除数据库<span style="font-size:18px;">drop database <数据库名称>;</span>
<4>查看所有的数据库<span style="font-size:18px;">show databases;</span>
>5>选择数据库<span style="font-size:18px;">use <数据库名称>;</span>
我们已经选择将要操作的 数据库,后续所有操作将在该数据库上执行<6>创建表
<span style="font-size:18px;">create table <表名>(<列名><数据类型>[列级完整性约束条件],...,<列名><数据类型>[列级完整性约束条件],<表级完整性约束条件>);</span>
举例1:<span style="font-size:18px;">create table tutorials_tbl(
tutorial_id INT NOT NULL AUTO_INCREMENT,
tutorial_title VARCHAR(100) NOT NULL,
tutorial_author VARCHAR(40) NOT NULL,
submission_date DATE,
PRIMARY KEY ( tutorial_id )//表级完整性约束条件
);</span>
<7>删除表<span style="font-size:18px;">drop table <表名>;</span>
<8>插入数据<span style="font-size:18px;">insert into <表名>(<列名1>...<列名n>) values(<值1>...<值n>;</span>
举例1:<span style="font-size:18px;">insert into user(id,name,year) values(10,'Jhon',12);</span>
以下为了便于说明我们建立三个表,并插入一些数据<9>查询数据
<span style="font-size:18px;">create table STUDENT(
Sno int4,
Sname varchar(20),
Sex varchar(10),
Sage int4,
Sdept varchar(10)
);
insert into student(sno,sname,sex,sage,sdept) values(95001,'LiYong','male',20,'CS');
insert into student(sno,sname,sex,sage,sdept) values(95002,'LiuChen','female',19,'IS');
create table SC(
Sno int4,
Cno int4,
Grade int4
);
insert into SC(sno,cno,grade) values(95001,1,92);
insert into SC(sno,cno,grade) values(95001,2,85);
insert into SC(sno,cno,grade) values(95001,3,88);
insert into SC(sno,cno,grade) values(95002,2,90);
create table COURSE(
Cno int4,
Cname varchar(20),
Cpno varchar(20),
Ccredit int4
);
insert into course(cno,cname,cpno,ccredit) values(1,'DataBase',5,4);
insert into course(cno,cname,ccredit) values(2,'math',2);
insert into course(cno,cname,cpno,ccredit) values(3,'InforMationSystem',1,4);
insert into course(cno,cname,cpno,ccredit) values(4,'OperatingSystem',6,3);
insert into course(cno,cname,cpno,ccredit) values(5,'dataStructure',7,4);
insert into course(cno,cname,ccredit) values(6,'dataHandle',2);
insert into course(cno,cname,cpno,ccredit) values(7,'Pascal',6,4);</span>
<9>查询语句:<span style="font-size:18px;">select <all || distinct> <目标列表达式1>,...,<目标列表达式n> from <表名或视图名>,...,<表名或视图名>
where <条件表达式>
group by <列名1>,...,<列名n> having <条件表达式>
order by <列名1>,...,<列名n> <ASC || DESC></span>
说明: (1)这里的<目标列表达式>可以是算数表达式,也可以是字符串常量,函数等。<pre name="code" class="sql" style="font-size:18px;"> 举例1:
<span style="font-size:18px;">select Sname,Sno from STUDENT;</span>
结果:LiYong, 95001
LiuChen, 95002
举例2:
<span style="font-size:18px;">select Sname,2014-Sage from STUDENT;</span>
结果LiYong, 1994
LiuChen, 1995
说明:这里的2014-Sage是一个算数表达式
举例3:
<span style="font-size:18px;">select Sname,'Year of Birth:',2014,Lower(Sdept) from STUDENT;</span>
结果:# Sname, Year of Birth:, 2014, Lower(Sdept)
LiuChen, Year of Birth:, 2014, is
LiYong, Year of Birth:, 2014, cs
说明:这里的'Year of Birth:'就是字符串常量,Lower(Sdept)就是函数
举例4
<span style="font-size:18px;">select Sname StudentName,Sage StudentAge from STUDENT;</span>
结果# StudentName, StudentSage
LiYong, 20
LiuChen, 19
说明:
相当于对Sname重命名为StudentName,和对Sage重命名为StudentAge
举例5:
Distinct的作用,消除重复的行
<span style="font-size:18px;">select distinct Sno from SC;</span>
结果:# Sno
95001
95002
举例5::
查询空值(NULL)
<span style="font-size:18px;">select * from course where Cpno is NULL</span>
查询非空值
<span style="font-size:18px;">select * from course where Cpno is not NULL</span>
举例6:
GROUP BY字句
<span style="font-size:18px;">select Sno StudentNumber,count(grade) from SC group by Sno;</span>
说明:按Sno属性进行分组结果:
# StudentNumber, count(grade)
95001, 3
95002, 1
HAVING 字句
<span style="font-size:18px;">select Sno StudentNumber,count(grade) from SC group by Sno having count(*)>2;</span>
说明:对分组结果进行筛选
结果:
# StudentNumber, count(grade)
95001, 3
Where和Having字句的区别
(1)where字句不能使用聚集函数作为条件表达式
(2)where和having的作用对象不同,where字句作用于基本表或视图,having字句作用于组,因为where是在分组前产生作用,而having是分组后对组产生作用。
<10>连接查询
举例1
<span style="font-size:18px;">select STUDENT.*,SC.* from STUDENT,SC;</span>
说明:当没有条件筛选的时候该语句相当于笛卡尔积
结果:
# Sno, Sname, Sex, Sage, Sdept, Sno, Cno, Grade
95001, LiYong, male, 20, CS, 95001, 1, 92
95001, LiYong, male, 20, CS, 95001, 2, 85
95001, LiYong, male, 20, CS, 95001, 3, 88
95001, LiYong, male, 20, CS, 95002, 2, 90
95002, LiuChen, female, 19, IS, 95001, 1, 92
95002, LiuChen, female, 19, IS, 95001, 2, 85
95002, LiuChen, female, 19, IS, 95001, 3, 88
95002, LiuChen, female, 19, IS, 95002, 2, 90
如果使用嵌套语句
select STUDENT.Sno from (select STUDENT.*,SC.* from STUDENT,SC);
使用该语句会报错,every derived table must have its own alias(
每个派生出来的表都必须有一个自己的别名)因为,进行嵌套查询的时候子查询出来的的结果是作为一个派生表来进行上一级的查询的,所以子查询的结果必须要有一个别名
详细内容见该博客
http://blog.sina.com.cn/s/blog_5d2eee260100xu8b.html
使用以下语句就正确了:
select * from (select Student.Sno StudnetSno,SC.Sno CSSno,Sname,Sex,Sage,Sdept,Cno,Grade from STUDENT,SC ) as result;
说明:该语句实际上是等值链接(不是自然连接)
结果:
# StudnetSno, CSSno, Sname, Sex, Sage, Sdept, Cno, Grade
95001, 95001, LiYong, male, 20, CS, 1, 92
95002, 95001, LiuChen, female, 19, IS, 1, 92
95001, 95001, LiYong, male, 20, CS, 2, 85
95002, 95001, LiuChen, female, 19, IS, 2, 85
95001, 95001, LiYong, male, 20, CS, 3, 88
95002, 95001, LiuChen, female, 19, IS, 3, 88
95001, 95002, LiYong, male, 20, CS, 2, 90
95002, 95002, LiuChen, female, 19, IS, 2, 90