SQL基础
SQL 是: Structure Quey Language (结构化查询语言)
它是使用关系模型的数据语言,由IBM在20世纪70年代开发出来,作为IBM关系数据库原型SystemR的原型
关系语言,实现了关系数据中的信息检索。
SQL语句分为3大类: DDL DML DCL
DDL:(Data Definition Language)
解释: 数据库实体描述语句
作用: 定义不同的数据段,数据库,表,列,索引等。
常用关键字: create drop alter
DML:(Data Manipulation Language)
解释: 数据操纵语言
作用: 用于添加,删除,更新和查询数据记录。
常用关键字: insert delete update select
DCL:(Data Control Language)
解释: 数据库控制语言
作用: 用于控制不同数据段直接的许可和访问级别语句定义数据库,字段,用户访问权限和安全级别。
常用关键字: grant revoke
-----------------------------------------------------------------------------------------------------------------
DDL语句
语法:create database php
功能:创建一个数据库 php
说明:如果数据库中存在 php 则会提示报错
语法:show databases
功能:列出系统中的数据库
语法:drop database php
功能:删除系统中的数据库 php
---------------------------------------
语法:use php
功能:选择数据库 php
(只有选择数据库后才能查看数据库中的数据表)
---------------------------------------
语法:show tables
功能:查看数据库 php 中所有的数据表
语法:create table student(name varchar(4) , age int(6) , sex int(6))
功能:创建一个名称为:student 的表并且定义 name age sex 三个信息字段,其字段的类型分别为 varchar(4) int(6) sex int(6)
语法:drop table student
功能:删除数据表 student
---------------------------------------
语法:show create table student
作用:详细的查看表 student 中的定义信息
语法:desc student
作用:直观的查看表 student 中的定义信息
语法:alter table student add height int(8)
功能:在表 student 中添加定义信息 height 并定义类型 int(8)
语法:alter table student add weight int(10) first
功能:在表 student 中的信息栏第一位添加定义信息 weight 并定义类型 int(12)
语法:alter table student add weight1 int(10) after height
功能:在表 student 的定义信息 height 后面添加一个信息 weight1 并定义类型 int(10)
语法:aler table student modify weight1 varchar(2)
功能:把表 student 中的信息 weight1 int(10) 改为 varchar(2)
(modify和change的区别在于modify只能对其信息的属性进行改动,change除了属性还可以对其改变其名称)
语法:alter table student change weight1 weight2 int(10)
功能:把表 student 中的信息 weight1 varchar 名称改为 weight2 属性改为 int(10)
语法:alter table student drop weight2
功能:把表 student 中的信息 weight2 删除
---------------------------------------------------------------------------------------------------------------
DDL逻辑流程:
数据库|--------------------------------->表--------------------------------->表中的信息
|---> 增 create database php |--->增 create table student |--->增alter table student add name
|---> 查 show database |--->查 show tables |--->查desc /show create table student
|---> 改 |--->改 |--->改alter table student modify/change
|---> 删 drop database php |--->删 drop table student |--->删alter table student drop name
作者 想当然 (感谢PHP湖北技术支持)