MySQL 基本使用

查看当前所有的数据库
show databases

 查看当前数据库的所有表格

 show tables

## 创建数据库
create database demo01 default character set utf8;
##切换数据库到新建的库
use demo01;

##在新的库中创建表

create table clazz (
    cid int(10) not null auto_increment primary key,
    cname varchar(100) not null,
    csg varchar(200) unique,    
)

##创建学生表
create table student (
    sid int(10) not null auto_increment primary key,      ##主键自增
    sname varchar(100) not null,                          ##非空  
    ssext char(4) not null,                               ##非空  
    sbirth date, 
    sfav varchar(200),
    sphone char(11) unique,                               ##唯一
    cid int(10) references clazz(cid)                     ##外键约束

    ## constraint pk_cid primary key(cid),
    ## constraint un_csg unique key(csg),
    ## constraint fk_cid foreign key(cid) references clazz(cid)
)

##在创建表外部添加约束
## alter table student add constraint pk_cid primary key(cid),
## alter table student add constraint un_csg unique key(csg),
## alter table student modify sphone varchar(100) not null,

 表的修改:
            新增字段
            alter table student add money float;
            删除字段
            alter table student drop sfav;
            修改字段类型
            alter table student modify sphone int(11)
            修改表名
            alter table student rename as stu
            删除表
            drop table student;


添加数据:
            insert into student values(default, '张三', 18, '', '2000-01-01');
            insert into student values(default, '张三', 300, '', '2000-01-01', 900.00);
            insert into student values(default, '张三', 18, 'a', '2000-01-01');


###############################################################
MySQL常见的字段类型 主键自增在创建表的字段后使用 auto_increment 数值类型:
int(长度),表示整数类型的长度 长度可以指定10 float/double 浮点型 字符类型: varchar(长度),动态分配存储长度 char(长度), 分配固定长度 日期类型: date, 格式为yyyy-mm-dd datetime, 格式为yyyy-MM-dd hh:mm:ss 占用8个字节 timestamp, 会自动进行时区的转换, 占用4个字节 time, 时间 year, 年份 其他类型: TEXT, 字符数据 BLOB, 二进制数据 ################################################################ 约束: 主键约束: 在创建表时在字段后使用 primary key 在创建表语句的最后使用 constraint 约束名 primary key(主键字段名) 在创建表后使用 alter table 表名 add constraint 约束名 primary key(字段名) 非空约束: 在创建表时在字段后使用 not null 在创建表后使用 alter table 表名 modify 字段名 类型 not null 注意: MySQL的非空约束中空字符串是可以存进去的 检查约束: 问题: 在MySQL中是没有检查约束的, 但是使用check关键字又不会报错 解决: 使用逻辑代码进行无效数据的过滤(主要) 使用MySQL的存储过程 唯一约束: 在创建表时在字段后使用 unique 在创建表语句的最后使用 constraint 约束名 unique key(字段名) 在创建表后使用 alter table 表名 add constraint 约束名 unique key(字段名) 外键约束: 在创建表时在字段后使用 references 父表名(父表主键名) 在创建表语句的最后使用 constraint 外键约束名 foreign key(字段名) references 父表名(父表主键名) 在创建表后使用 alter table 表名 add constraint 外键约束名 foreign key(字段名) references 父表名(父表主键名)
              (on delete set null on update cascase) ############################################################################# 表的修改: 添加字段: alter table 表名 add 字段名 类型, 删除字段: alter table 表名 drop 字段名 修改字段类型: alter table 表名 modify 字段名 新的类型 修改字段名: alter table 表名 change 字段名 新的字段名 类型 修改表名: alter table 表名 rename as 新的表名 删除表: drop table 表名

#############################################################################

与oracle不一样的地方:
    连接符: contact(字段名, 字段名)

      分组:  select * from emp order by deptno

      分页:  select * from 表名 limit n*(m-1), n (m表示页面数, n表示每页显示的数量)

     外连接: MySQL 中不能使用(+)符号, 所以在外连接时, 应该使用SQL99 的语法

     delete from: Oracle 中的语法是 delete [from], from 可以省略, 但是 MySQL 中, 不能省略 from

 

    

原文地址:https://www.cnblogs.com/jiefangzhe/p/11523258.html