甲骨人-MySQL网络课-day03


                      
=====================================================================================
=======================
第三章 mysql SQL语句
一、mysql接口自带命令mysql

1.一、\h 或 help 或 ?
1.二、\G
1.三、\T 或 tee
1.四、\c 或 CTRL+c
1.五、\s 或 status
1.六、\. 或 source
1.七、\u 或usesql

二、SQL语句种类介绍
SQL92 SQL99:
DDL
DML
DCL
DQL数据库

三、DDL:数据定义语言
3.1 介绍:
逻辑结构:
库(库名字、库的属性)、表(表名、表的列(列名字、列属性、约束)、表其余属性、表数据)
DDL 用来定义:数据库的元数据code

3.2 DDL语句——库定义
3.2.1 建立:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_specification] ...orm

CREATE DATABASE jiaguren CHARSET utf8;接口

开发规范:
一、库名字,使用小写字母。
二、建立库时,一并指定字符集。ci

查询建立的数据库:
show databases;
show create database test;开发

3.2.2 删除:
drop database test;it

3.2.3 修改:
alter database test1 charset utf8;io


3.3 DDL语句——表定义

3.3.1 建立
create table 
help create table

USE jiaguren;
CREATE TABLE student(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '学员序号',
NAME VARCHAR(20) NOT NULL  COMMENT '学员姓名',
age TINYINT UNSIGNED   NOT NULL COMMENT '学员年龄',
gender ENUM('m','f') NOT NULL DEFAULT 'm' COMMENT '学员性别',
ruxuedate DATETIME NOT NULL DEFAULT NOW() COMMENT '入学时间'
)ENGINE=INNODB CHARSET utf8;

查看建立的表:
SHOW TABLES;
show create table student;
desc student;

扩展:
create table stu like student;
create table stu1 select * from student;


3.3.2 删除定义
drop table stu1;


3.3.3 修改
(1)修改表名字
alter table student rename to stu;
(2)在表中添加一列telnum char(11);
alter table stu add telnum char(11);
(3)在name列后加入新列 qq varchar(20) not null unique
alter table stu add qq varchar(20) not null unique after name;
(4)在表的第一列加入新列classid int not null
alter table stu add classid int not null  first;
(5)删除表中的classid列
alter table stu drop classid;
(6)修改表中telnum 列的数据类型为bigint
alter table stu change   telnum telnum bigint;

(7)删除表的全部数据行
truncate  table stu;


3.4 DML语句
操做表中的数据行

3.4.1 增(insert)
insert into stu values(1,'zs','12345',20,'m',now(),110);
insert into stu(name,qq,age,telnum) values('ls','23456',21,119);
insert into stu(name,qq,age,telnum) values('lss','223456',29,118),('lsss','2234516',129,1222);

select * from stu;

3.4.2 删
delete from stu;
delete from stu where name = 'lsss';
delete from stu where name like 'ls%';

3.4.3 改
update stu set age=40 where name='zs';

扩展:屏蔽delete,伪删除,使用update替代delete
alter table stu add state enum('1','0') not null default '1';
update  stu set state='0' where name='lsss'; 
替代:
delete from stu where name='lsss';
在未来业务中:
select * from stu where state='1';


3.5 DQL:
3.5.1 数据行的查询(select)
(1) from 
(3) join 
(2) on 
(4) where 
(5) group by(开始使用select中的别名,后面的语句中均可以使用)
(6) having 
(7) order by
(8) limit


-- 一、全表查询
SELECT * FROM city;

-- 二、部分数据查询
SELECT NAME ,population  FROM city;

-- 三、where子句的时候--->过滤查询
--- 3.1 等值查询
SELECT * FROM city WHERE countrycode='CHN';

--- 3.2 不等值查询(> < >= <= ,<>) 
SELECT * FROM city WHERE countrycode='CHN' AND population>5000000;
--- 3.3 链接符(AND,OR)
--- AND,须要保证先后条件都知足
--- OR, 只须要知足其中一个条件便可
   
SELECT * FROM city WHERE countrycode='CHN' OR countrycode='USA';
SELECT * FROM city WHERE countrycode IN ('CHN','USA');

---> 通常会改写为 UNION
SELECT * FROM city WHERE countrycode='CHN'
UNION
SELECT * FROM city WHERE countrycode='USA';

--- 3.4 BETWEEN AND 

SELECT * FROM city WHERE population BETWEEN  1000000 AND 2000000 ;


-- 四、order by + limit

SELECT * FROM city WHERE countrycode='USA' ORDER BY population DESC;
SELECT * FROM city WHERE countrycode='USA' ORDER BY population DESC LIMIT 10;

SELECT * FROM city WHERE countrycode='USA' ORDER BY population DESC LIMIT 10,10;
SELECT * FROM city WHERE countrycode='USA' ORDER BY population DESC LIMIT 10 OFFSET 10;

元数据查询 mysqlshow show information_schema

相关文章
相关标签/搜索