---
title: "MySQL语法"
author: "tianhei"
date: "2010-11-16T14:19:31Z"
tags: ["MySQL", "数据结构"]
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2011-05-19T21:46:34Z"
id: 812754
url: "https://tianhei.iteye.com/blog/812754"
---

primary key 主键 不能为空 唯一  
foreign key 外键 其它表的主键

unique key 唯一键 可以为空 唯一

 

一、mysql语句

1、显示所有数据库

     show databases;

2、使用数据库 test

     use test;

3、创建数据库 test

     create database test;

4、显示所有表

     show tables;

5、显示 user 表的结构

     show create table user;

6、在stu表中增加字段 password

     alter table stu add column(password char(16));

     alter table stu add (password char(16));

7、向stu表中插入数据

     insert into stu values('a','b',27,'d');

     insert into stu(name,password,age,sex) values('a','b',27,'d');

8、查看当前mysql字符集的参数

     show variables like 'character\_set\_%';  
     show variables like 'collation\_%';

9、修改test数据库的字符集：  
     alter database test default character set utf8 collate utf8\_general\_ci;

10、显示user表的索引：

     show index from user;

11、在user表中创建索引，索引名为index\_name，索引关联user表的name字段。

     alter table user add index index\_name(name);

12、删除user表中的索引 index\_name：

     drop index\_name on user;

13、创建空用户  
     grant all privileges on \*.* to [''@localhost](mailto:''@localhost) identified by '1';  
     grant all privileges on \*.* to [''@localhost](mailto:''@localhost) identified by '';