12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
mysql数据库 时间:2024-12-24 19:11:21
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
与上一篇的《SQL简单使用-基础篇》相连续的篇章,《SQL简单使用-基础篇》以下简称《基础篇》。在《基础篇》中,主要简单的带大家了解一下SQL命令中最主要的增删改查命令的使用,
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
与上一篇的《SQL简单使用-基础篇》相连续的篇章,《SQL简单使用-基础篇》以下简称《基础篇》。在《基础篇》中,主要简单的带大家了解一下SQL命令中最主要的增删改查命令的使用,增INSERT INTO、删DETELE/DROP/TRUNCATE、改UPDATE、查SELECTE。因为增删改查是SQL命令的核心也是最基础的部分,所以本篇张还是围绕增删改查的使用进行进阶性的介绍与使用。
先从《基础篇》中提到的where子句里面的通配符讲起。
1.like用于在where子句中搜索列中的指定模式
示例:select * from websites where name like '%oo%';
注:(%分号表示任意数据,_表示任意一个数据,动手练两边就能熟悉)
'G%'搜索以G开头的数据
'%G'搜索以G结尾的数据
'%g%' 搜索包含g的数据
'G' 搜索以G开头的两位数据
'G' 搜索以G结尾的两位数据
'G' 搜索包含G的三位数据
1.1通配符还有一种(%、_和[charlist])
示例:[charlist]使用select * from websites where name REGEXP '^[A-H]';
2.between 用于选取介于两个值之间的数据范围内的值
示例:select * from websites where alexa between 1 and 20;
示例:添加not使用select * from websites where alexa not between 1 and 20;
示例:结合IN使用select * from websites where ( alexa BETWEEN 1 and 20) and country in ('USA','CN');
示例:文本select * from websites where name between 'A' and 'H';不包含H
3.top 用于规定返回记录的数据,实用
示例:SQL server (SELECT TOP number|percent column_name(s)FROM table_name;)select top 50 percent * from websites;
示例:Oracle(SELECT column_name(s) FROM table_name WHERE ROWNUM <= number;)select * from websites where ROWNUM <5;
示例:MYSQL (SELECT column_name(s) FROM table_name LIMIT number;)select * from websites limit 3;
4.IN 操作符允许在where子句中规定多个值
示例:查看表websites中name列的多条数据select * from websites where name in('baidu','Google');
5.别名可以为表名称或列名称指定别名。
语法:列名称语法
SELECT column_name AS alias_name FROM table_name;
示例:select name AS n,country AS c from websites;
语法:表名称语法
SELECT column_name(s) FROM table_name AS alias_name;
示例:select w.name,w.url,a.count,a.date from websites AS w ,access_log AS a where w.id=a.site_id and w.name='菜鸟教程';
注:
1.在查询中涉及超过一个表
2.在查询中都是用了函数
3.列名称很长或者可读性差 都需要把两个列或者多个列结合在一起。
6.join 子句用于把来自两个表或者多个表的行结合起来,基于这些表之间的共同字段
join类型有一下几种:
INNER JOIN:如果表中有至少一个匹配,则返回行
LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
FULL JOIN:只要其中一个表中存在匹配,则返回行(MYSQL不支持)
首先,连接的结果可以在逻辑上看作是由SELECT语句指定的列组成的新表。
左连接与右连接的左右指的是以两张表中的哪一张为基准,它们都是外连接。
外连接就好像是为非基准表添加了一行全为空值的万能行,用来与基准表中找不到匹配的行进行匹配。假设两个没有空值的表进行左连接,左表是基准表,左表的所有行都出现在结果中,右表则可能因为无法与基准表匹配而出现是空值的字段。
来源:《数据库系统原理教程》,王珊,陈红编著,P86
示例: inner join
SELECTwebsites.id,websites.NAME,access_log.count,access_log.dateFROMwebsitesINNER JOIN access_log ON websites.id = access_log.site_id;
7.union 用于合并两个或多个select语句的结果集
语法:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
示例:union 去重
SELECT country FROM WebsitesUNIONSELECT country FROM appsORDER BY country;
示例:union all显示所有包括重复部分
select country from websitesunion allselect country from apps;
示例:带有where的union all
select country,name from websites where country='CN'union allselect country,app_name from apps where country='CN' order by country;
SELECT * INTO newtable [IN externaldb] FROM table1;
SELECT column_name(s) INTO newtable [IN externaldb] FROM table1;
8.1 insert into select
示例:复制 "apps" 中的数据插入到 "Websites" 中:INSERT INTO websites (name,country) select app_name,country from apps;
create database db_name;
create table table_name(column_name1 data_type(size),column_name2 data_type(size),column_name3 data_type(size),... ...);date_type 数据类型,size参数规定表中列的最大长度
示例:创建名称为runoob的数据库create database runoob;
示例:创一个student_informaton表,包含五列:student_id,student_name,student_class,student_tele,student_add
create table student_infomation (student_id int(10),student_name char(4),student_class char(10),student_tele int(11),student_add varchar(255));
前面是列名,后面跟的是对于列名的数据类型
10.约束用于规定表中的数据规则
约束可以在创建表的时候通过create table语句规定,或者在表创建之后通过alter table语句规定
语法:crate table + constraint
create table table_name(column_name1 type_data(size) constraint,column_name2 type_data(size) constraint,column_name3 type_data(size) constraint,... ...);
在SQL中,我们有如下约束:
NOT NULL 指示某列不能存储 NULL 值,强制字段始终包含值,否则就无法插入新记录或者更新记录。
UNIQUE 保证某列的每行必须有唯一的值。
PRIMARY KEY - NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助于更容易更快速地找到表中的一个特定的记录。
FOREIGN KEY保证一个表中的数据匹配另一个表中的值的参照完整性。
CHECK保证列中的值符合指定的条件。
DEFAULT规定没有给列赋值时的默认值。
10.1 not null约束约束强制不接受到任何null值
示例: student_tele不能为空
create table student_information (student_id INT (10) ,student_name CHAR (4),student_class CHAR (10),student_tele INT (11) NOT NULL,student_add VARCHAR (255));
10.2 UNIQUE 约束唯一标识数据库表中的每条记录。
UNIQUE 和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。
PRIMARY KEY 约束拥有自动定义的 UNIQUE 约束。
请注意,每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。
示例:MYSQL
create table student_information (student_id INT (10),student_name CHAR (4),student_class CHAR (10),student_tele INT (11),student_add VARCHAR (255),unique (student_id));
示例:SQL server/oracle
create table student_information (student_id int (10) NOT NULL UNIQUE,student_name CHAR (4),student_class CHAR (10),student_tele INT (11),student_add VARCHAR (255),);
示例:SQL mysql/server/oracle定义过个列的unique约束。
create table student_information (student_id int (10) NOT NULL UNIQUE,student_name CHAR (4),student_class CHAR (10),student_tele INT (11),CONSTRAINTstu_inf UNIQUE (student_id,student_name));这里的 stu_inf 为约束名称constraint_name,自定义。
alter table时的unique约束
示例:
alter table student_informationadd unique (student_id);
示例:添加多个unique,
alter table student_informationadd constraint stu_inf unique (student_id,student_add);
撤销unique约束
示例:mysql
alter table student_informationdrop index stu_inf;
示例:SQL
alter table student_informationdrop constraint stu_inf;
10.3 primary key 主键必须包含唯一的值,主键不能为null,每个表都应该有一个主键,并且是唯一的。
示例:参照unique,将其中的unique替换为 primary key即可。上述有添加多个unique示例,如果改为primary 可以意思就是主键由添加的几个列组成。
10.4 foreign key 约束
a.可以用来预防破坏表之间连接的行为
b.防止非法数据插入外键列,因为它必须是指向的那个表中的值之一
示例:MYSQL
create table websites(id int(11) NOT NULL,name char(20) NOT NULL,url varchar(255) NOT NULL,alexa int(11) NOT NULL,country char(10) NOT NULL,primary key (id),foreign KEY (id) references apps(id));
示例:SQL server/oracle
CREATE TABLE websites ( Id int NOT NULL PRIMARY KEY, OrderNo int NOT NULL, Id int FOREIGN KEY REFERENCES apps(Id) );
示例:MySQL/SQL Server/Oracle
CREATE TABLE websites ( Id int NOT NULL, OrderNo int NOT NULL, Id int, PRIMARY KEY (O_Id), CONSTRAINT fk_PerOrders FOREIGN KEY (Id) REFERENCES apps(Id) );
alter table 使用foreign key约束
示例:
ALTER TABLE Orders ADD FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
示例:如需命名 FOREIGN KEY 约束,并定义多个列的 FOREIGN KEY 约束
ALTER TABLE Orders ADD CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
撤销FOREIGN KEY约束
示例:mysql
alter table Orders drop index fk_PerOrders;
示例:SQL
alter table Orders drop constraint fk_PerOrders;
10.5CHECK 约束
用于限制列中的值的范围
示例:MYSQL
create table websites(id int(11) NOT NULL,name char(20) NOT NULL,url varchar(255) NOT NULL,alexa int(11) NOT NULL,country char(10) NOT NULL,check (id>0));
alter table 使用check约束alter table websites add check (id>0);
撤销check约束(参照unique约束中的alter table)alter table websites drop check constraint_name;
10.6 DEFAULT 约束
1.用于向列中插入默认值
2.如果没有规定其它值,那么将默认值添加到所有的记录
示例:MYSQL
create table student_information (student_id INT (10) NOT NULL,student_name CHAR (4),student_class CHAR (10) DEFAULT '' comment '班级',student_tele INT (11),student_add VARCHAR (255));comment 是为 字段或列的属性添加注释用的
alter table 使用 default
示例:MYSQL
alter table websitesalter country set default 'CN';
示例:SQL serveralter table websites add constraint ad_c default 'CN' for country;
示例:oraclealter table websites modify country default 'CN';
撤销default约束
示例:MYSQL
alter table websitesalter country drop default;
示例:SQL server/oracle
alter tables websitesalter column country drop default;
create index index_name ON table_name (column_name);
create UNIQUE index index_name ON table_name (column_name);
示例:将websites表中name列中创建名为web_index的索引。create index web_index ON websites (name);
12.drop可以删除表,索引和数据库
DROP INDEX 语句用于删除表中的索引。
用于 SQL Server 的 DROP INDEX 语法:DROP INDEX table_name.index_name
用于 DB2/Oracle 的 DROP INDEX 语法:DROP INDEX index_name
用于 MySQL 的 DROP INDEX 语法:ALTER TABLE table_name DROP INDEX index_name
DROP TABLE 语句用于删除表。
DROP DATABASE 语句用于删除数据库。
仅仅需要删除表内的数据,但并不删除表本身TRUNCATE TABLE table_name
13.ALTER TABLE 用于在已有的表中添加、删除或修改列。
添加列的语法:ALTER TABLE table_name ADD column_name datatype;
删除表中的列语法:ALTER TABLE table_name DROP COLUMN column_name datatype;
改变表中数据类型语法:ALTER TABLE table_name MODIFY COLUMN column_name datatype;
示例: 在website表中添加名为column_date的列,然后修改列的数据类型,删除添加的列
alter table websites add column_date date;添加alter table websites modify column column_date year;修改alter table websites drop column column_date; 删除
参考菜鸟教程请添加链接描述整理的笔记
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19