初识mysql数据字段属性_初识mysql

# 经典sql语句

+ 创建数据库

- CREATE DATABASE database_name;

+ 删除数据库

- DROP DATABASE database_name;

+ 创建备份数据库

- USE master

EXEC sp_addupdevice 'disk','testBack' ,'c:\xxx\xxx.dat'

+ 开始备份

- BACKUP DATABASE pubs TO testBack;

---

+ 创建表

- 创建新表 create table tab_name (col1 typ1 [not null] [primary key],col2 typ2 [not null]...);

- 根据已有表创建新表 create table tab_new like tab_old;(使用旧表创建)

- create table as select col1,col2 ... from tab_old definition only;

+ 删除新表

- drop table_name

+ 增加新列

- Alter table tab_name

add column col type ;

- 注意:列增加后将不能删除 DB2 中列加上后 数据类型也不能改变 唯一能改变的事增加varchar类型长度

+ 修改表

- 修改表名 Alter table 旧表名 rename 新表名

- 修改字段 Alter table 表名 change 旧属性名 新书姓名 新类型。。。

- 增加字段 Alter table 表名 Add 属性名 数据类型【完整性约束条件】。。。

- 删除字段 alter table 表名 drop 属性名

+ 添加主键

- Alter table tabname

add primary key(col);

+ 删除主键

- Alter table tabname

drop primary key(col);

+ 创建索引

- create [unique] index index_name

on tablename(col...);

+ 删除索引

- drop index index_name;

- 注意:索引不可以修改 (只能删除 重新建)

+ 创建视图

- create view view_name as select statement;

+ 查看视图

- describe 视图名称;

- show table status like "视图名";

- show create view 视图名;

+ 修改视图

- with check option (保持更新状态)

- create or replace view view_name (属性语句) As select 语句 with check option;

- Alter view As select 语句 [with check option];

+ 删除视图

- drop view view_name;

---

+ 基本sql语法

- 选择 select * from table_name where 条件范围;

- 插入 insert into table1 (field1,field2) values(value1,value2);

- 删除 delete from table_name where 条件范围;

- 更新 updata table1 set field1 = value1 where 条件范围;

- 查找 select * from table1 where field1 like '%value1%'

- 排序 select * from table1 order by field1 field2[desc];

- 总数 select count as totalcount from table1;

- 求和 select sum(field1) as sumvalue from table1;

- 平均 select avg(field1) as avgvalue from table1;

- 最大值 select max(field1) as maxvalue from table1;

- 最小值 select min(filed1) as minvalue from table1;

---

+ UNION (并集)

- UNION 通过组合两个或多个结果表 并消除重复行派生出一个新的结果表

select usename form tab1 UNION select username from tab2;

==>tab1,tab2 username 并集

- UNION ALL 与 UNION 相似 不过是罗列出所有结果 不进行去重

+ EXCEPT (差)

+ INTERSECT (交集)

+ 条件查询 where

- select 字段1,字段2,字段3...from 表名 where 字段 [not] in (元素1,元素2...); //查找(不为)为元素1,元素2...的结果

- select 字段1,字段2...from 表名 where 条件表达式1 and 条件表达式2 [...条件表达式n...]; //and 多语句查询

- select 字段1,字段2... from 表名 where 表达式1 OR 表达式2[...表达式n...]; //or多语句查询

+ 范围查询 between and

- select 字段1,字段2,字段3...from 表名 where 字段[not] between 取值1 > 取值2 ;//大于小于等于 等表达式符号

- select 字段1,字段2,字段3 from 表名 between 取值1 and 取值2 //取值1 , 取值2之间的值 (包含取值1,取值2);

+ 空值查询 not null

- select 字段1,字段2... from 表名 where 字段 is [not] null;

+ 去重查询 distinct

- select distinct 字段名 from 表名;

+ 排序查询

- select 字段1,字段2...from 表名 order by 属性名1[asc(升序,默认)/desc], [属性名2] [asc(升序,默认)/desc]...;

+ 分组查询 group by

- group by 属性名 [having 条件表达式][with rollup]

- 单独使用(毫无意义)

- 与group_concat()函数一起使用

- 与having一起使用 (限制输出结果)

- 与with rollup 一起使用(最后加入一个总和行)

- group 与 函数一起使用

- select count(*) As totals, group_concat(user_name) As userNameDetail from table_name group by sex;

- 聚合函数[ sum() , max() , min() , avg() , count()]

```

totals----------userNameDetail--------sex

3 zhangsan,lisi,wangwu 男

2 li1,li2 女

4 s2,s3,s4,s5 保密

```

- group 与 having一起使用

- select count(*) As totals, group_concat(user_name) As userNameDetail from table_name group by sex having count(*)>3;

- 聚合函数[ sum() , max() , min() , avg() , count()]

```

totals----------userNameDetail--------sex

3 zhangsan,lisi,wangwu 男

4 s2,s3,s4,s5 保密

```

+ 分页查询 limit

- select 字段1,字段2...from table_name limit 初始位置,记录数

```

select * from table_name limit 5,10; 查找6-15数据 //5表示其实位置 10代表数量

```

### 多表查询 连接查询

+ 连接两个或多个表查询

- select * from table_name1,table_name;

+ 内连接查询 inner join on

- 内连接查询是一种最常用的连接查询

- select * from table_name1 a,table_name2 b where a.userId=b.userId;或者select * from table_name1,table_name2 where table_name1.userId=table_name2.userId;

+ 左连接查询 left join on

- 可以查询到表1的所有数据 而表二只有匹配到的数据

- select * from table_name1 a left join table_name2 b on a.uerId=b.userId;

+ 左连接查询 right join on

- 可以查询到表2的所有数据 而表1只有配到的数据

- select * from table_name1 a right join table_name2 b on a.uerId=b.userId;

+ 多语句查询 where后面有多个条件 and连接查 询

### 子查询

+ in 关键字

- 一个查询语句条件可能落在另一个select语句结果中

+ 带比较运算符的子查询

- 子查询可以直接使用比较运算符

+ 带 [not] exists 关键字子查询

- select * from table_name where exists (select * from table_name2);//当table_name2中查找到内容 才会进行外层查询 否则不尽行查询;

+ Any

- select * from table1 where price>=any(select price from table2);//筛选价格大于任意table2表中选出的价格的结果(比table2最小价格大的所有价格)

+ All

- select * from table1 where price >= all(select price from table2);//筛选价格大于全部table2表中选出的价格的结果(比table2最大的价格大的所有价格)

----

## 索引

+ 创建索引 UNIQUE 唯一性索引

```

create [UNIQUE] index index_name on table_name(col(3) ASC);

```

+ 显示索引

```

show index in table_name;

```

+ 创建组合索引

```

create index index_name on table_name(col1,col2);

```

+ 边创建边设置索引

```

create table seller

(

seller_id int not null auto_increment,

seller_name char(50) not null,

seller_address char(50) null,

seller_contact char(50) null,

product_type int null,

sales int null,

primary key (seller_id,product_type),

index index_seller(sales)

);

```

+ 添加索引

```

alter table seller

add index index_seller_name(seller_name);

```

+ 删除索引

```

drop index index_seller_name on seller;

alter table seller

drop index index_seller;

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值