MySQL基本语法操作
前言
今天在沈阳华清远见学习MySQL数据库的第一天,整理了简单的MySQL语法
一、MySQL数据库的名词
1、关键字:某个字段或多个字段的值可以唯一地标识一条记录,则该字段或字段组就称为关键字
例如:BETWEEN、AS等
2、主键:一个关键字是用以标识每条记录的唯一性,并作为该表与其他表实现关联之用
候选关键字,primary key用来唯一的约束字段里面的数据,不能重复,不能为空,一张表中最多
只能有一个主键
3、外键:外键是某个已存在表的主键,外键可以保证数据完整性,减少存储冗余
二、MySQL数据库基本操作
1.常用命令:
- show databases; //查看所有数据库
- create database student; //创建数据库
- use student; //使用数据库
- create table stu(id int primary key,name varchar(30),age int,gender char(1)); //创建stu表
- insert into stu values(1,"Tony",18,"m"); //插入数据
- select name from stu; //查询
- delete from stu where id = 2; //删除
- update stu set gender = "m" where name = "Tom"; //更新修改
- drop database 数据库名; //删除数据库
-
show tables //显示表名
2.表命令
1、创建表命令:
create table 表名(
字段名 类型 <约束>,
字段名 类型 <约束>,
......
字段名 类型 <约束>
);
代码如下:
create table Users(
id int primary key auto_increment,
name varchar(30) not null unique,
sex char(4) not null);
注意:
(1)常用的约束:
auto_increment自增长
unique唯一性约束
not null不为空
age int default 18, 默认(值)约束
primary key 主键约束
check 检查约束
(2)添加约束的时机
1.创建表时
2.修改表时
(3)约束添加分类
1.列级约束:(对一个数据列建立的约束,称为列级约束,既可以在列定义时声明,也可以在列定义后声明)语法上都支持,但外键约束没有效果,不支持列级约束。
create table if not exists test_s1( # 判断是否存在test_s1表,不存在则创建
id int primary key auto_increment, #主键,自增约束
sex char(4) check(sex='男' or sex ='女'), #检查约束,mysql没有效果但不报错
majorId int references major(id) #外键约束,mysql没有效果,但不报错
);
2.表级约束 :(对多个数据列建立的约束,称为表级约束表级约束只能在列定义后声明),
除了非空、默认、其它的都支持。在创建表字段的最下面 constraint (可写可不写,为了创立一个约束名字)约束名 约束类型(字段名)
create table if not exists test_s2( # 判断是否存在test_s1表,不存在则创建
id int ,
majorId int ,
constraint pk primary key(id), #约束名pk随意,主键名不生效,但不报错
constraint qw unique(seat), #唯一约束
constraint sex char(4) check(sex='男' or sex ='女'), #检查约束,mysql但不报错
constraint _uid_ foreign key(majorId) references major(id) #外键约束名_uid_
);
(4)sex char(4)
想要存储一个汉字,例如“”男“”,需要把char规定为4
2、删除表命令: (被外键关联的表无法删除,如果想删除,先删除关联的表)
drop table 表名;
3、查看表命令:
show tables; #查看当前数据库下所有的表的表名
4、查看表结构命令:
desc 表名; #查看该表的表结构
5、更改表结构命令:
(1)向表名中增加列列名
alter table 表名 add column 列名 字段类型
注:在末尾还可以加约束
alter table 表名 add column 列名 字段类型<字段约束>
alter table Users add column realname varchar(30);
alter table Users add column gender char(1) not null;
(2)删除表中某一列
alter table 表名 drop column 列名;
alter table test drop column id;
注:主键也可以删
(3)修改表中某个列的字段类型
alter table 表名 modify column 列名 字段类型
注:在末尾还可以加约束
alter table 表名 modify column 列名 字段类型<字段约束>
alter table Users modify column username varchar(30);
alter table Users modify column password varchar(30) not null;
(4)更该表的名字
alter table 表名rename to 新表名;// to可以省略
(5)更改列名
alter table 表名 change 原有列名 新的列名 新列名字段类型;
6、复制表命令:
方法一:(只复制表结构)在create table语句的末尾添加like子句,可以将源表的表结构复制到新表中,语法格式如下。主键约束自动增长有,外键约束没有。
create table 新表名 like 原表
方法二、(全复制,数据和结构)在create table语句的末尾添加一个select语句,可以实现表结构的复制,甚至可以将源表的表记录拷贝到新表中。下面的语法格式将源表的表结构以及源表的所有记录拷贝到新表中。主键约束,自动增长和外键约束就没有了
create table 新表名 select * from 原表
create table emp2 select * from emp; #查询emp表中所有字段的数据存储到新创建的emp2表中
create table emp3 select * from emp where deptno=20;#查询emp表中deptno值是20所有字段的数据存储到新创建的emp3表中
7、牛刀小试:
表1语法:
create table Users(id int primary key auto_increment,username varchar(30) not null unique,password varchar(30) not null);
表2语法:
create table LoginHistorys(id int primary key auto_increment,time datetime not null,uid int,constraint sun(自定义名字) foreign key(uid) references Users(id));
总结
总结了一天学习MySQL数据库的语法