mysql 常用语句命令(建表、修改属性等)
2017-11-02 11:04
519 查看
show variables like ‘character_set_client’;#查询字符集
show databases;#列出所有的服务器上的数据库alter
create database if not exists test;#创建一个数据库
drop database fk;#删除数据库
show tables from test;#显示一个数据库中的表
use test;
create table tb_dept(
Id int primary key auto_increment,#部门编号 整形 主键 自增长
Name varchar(18),#部门名称
description varchar(100)#描述
);
show tables from test;
desc tb_dept;#查看表信息
show create table tb_dept;
use test;
id int primary key auto_increment,#auto_increment只是MySQL特有的
Name varchar(18),
sex varchar(2),
age int,
address varchar(200),
email varchar(100)
);
drop table tb_dept;
只有当字段只包含空值时才可以修改。
alter table tb_emp modify sex varchar(4);
alter table tb_emp drop column tel;
rename table emp to tb_emp;
insert into dept_emp (Name,sex,age,address,email)values(”,”,”,”,”);
/*
not null
unique 唯一键tb_depttb_dept
primary key
foreign key 外键
check 检查
*/
create table tb_emp(
id int primary key auto_increment,
Name varchar(18),
sex varchar(2) default’男’ check(sex=’男’or sex=’女’),#表级写法check 在mysql中不起作用
age int,
address varchar(200),
email varchar(100) unique,
dept_id int,#references tb_dept(id) #表级写法外键不起作用
constraint foreign key fk_emp(dept_id) references tb_dept(id)
);
show databases;#列出所有的服务器上的数据库alter
create database if not exists test;#创建一个数据库
drop database fk;#删除数据库
show tables from test;#显示一个数据库中的表
use test;
create table tb_dept(
Id int primary key auto_increment,#部门编号 整形 主键 自增长
Name varchar(18),#部门名称
description varchar(100)#描述
);
show tables from test;
desc tb_dept;#查看表信息
show create table tb_dept;
use test;
创建员工表
create table tb_emp(id int primary key auto_increment,#auto_increment只是MySQL特有的
Name varchar(18),
sex varchar(2),
age int,
address varchar(200),
email varchar(100)
);
drop table tb_dept;
修改列类型
注意:不是任何情况下都可以去修改的,只有当字段只包含空值时才可以修改。
alter table tb_emp modify sex varchar(4);
增加列
alter table tb_emp add tel varchar(12);删除列
alter table tb_emp drop tel;alter table tb_emp drop column tel;
列改名
alter table tb_emp change Name emp_Name varchar(18);更改表名
alter table tb_emp rename emp;rename table emp to tb_emp;
insert into dept_emp (Name,sex,age,address,email)values(”,”,”,”,”);
约束
是在表上强制执行地数据校验规则,主要用于保证数据库地完整性/*
not null
unique 唯一键tb_depttb_dept
primary key
foreign key 外键
check 检查
*/
create table tb_emp(
id int primary key auto_increment,
Name varchar(18),
sex varchar(2) default’男’ check(sex=’男’or sex=’女’),#表级写法check 在mysql中不起作用
age int,
address varchar(200),
email varchar(100) unique,
dept_id int,#references tb_dept(id) #表级写法外键不起作用
constraint foreign key fk_emp(dept_id) references tb_dept(id)
);
创建表之后在添加
alter table tb_emp add constraint foreign key fk_emp(dept_id) references tb_dept(id);相关文章推荐
- mysql 增加列,修改列名、列属性,删除列语句
- MySQL修改表属性的SQL语句
- mysql 常用修改语句
- 修改MYSQL提示符,常用命令,和它的语句的规范
- mysql 连接命令 表管理 ,克隆表,临时表,字符串属性,设定语句间的分隔符
- mysql 常用SQL语句和命令
- mysql操作常用命令语句学习笔记(有点杂)
- MySql 基础知识-常用命令及sql语句
- mysql常用命令之-用户密码修改
- MYSQL的常用命令和增删改查语句和数据类型
- MYSQL的常用命令和增删改查语句和数据类型
- MySQL数据定义语句:CREATE(创建)命令、ALTER(修改)命令、DROP(删除)
- mysql的常用命令和增删改查语句
- MySQL中常用的修改表的命令
- MYSQL的常用命令和增删改查语句和数据类型
- MYSQL的常用命令和增删改查语句和数据类型
- MySql的基本命令已经常用的SQL语句总结
- mysql 常用 插入,修改,删除,查询语句
- mysql常用命令和语句
- MYSQL的常用命令和增删改查语句