您的位置:首页 > 数据库 > MySQL

Mysql 基础语法

2016-05-22 17:03 471 查看
1、 创建数据库

Create database 数据库名;

例如:

mysql> createdatabase stu;

Query OK, 1 row affected (0.02 sec)

2、 显示当前用户所有数据库

mysql> show databases;

3、 使用数据库

use 数据库名

例如:use stu;

4、 创建表

Create table 表名(字段1 数据类型{约束条件[not null][primarykey]},字段2 数据类型[约束条件],…..);

例如:

mysql> createtable student(id int not null primary key,name varchar(20) not null,classesvarchar(30),sex char(4) not null);

5、 显示当前数据库中的表

show tables;

6、 显示表结构

desc[ribe] 表名

例如:desc student;

7、 修改表

a. 修改表名

alter table 表名 rename 新表名;

例如:alter table student rename stu;//将表student修改为stu

b. 增加一个字段

alter table 表名 add [column] 新字段数据类型[约束条件];

例如:alter table student add column passwd varchar(20) not null;

c. 修改一个字段(列)

Alter table 表名change [column] 字段新字段 数据类型[约束条件];

例如:alter table student change column passwd password varchar(20) notnull;

d. 删除一个字段(列)

Alter table 表名 drop [column] 字段(列名);

例如:alter table student drop password;

e. 调整字段顺序(将字段1调至字段2之后)

Alter table 表名 change [column] 字段1 字段1 数据类型[约束条件] after 字段2;

mysql> alter table student change password passwordvarchar(20)not null after id;

8、 更新表

a. 插入一条数据

Insert into 表名(字段1,字段2,…) value(值1,值2,…);

insert intostudent(id,name,classes,sex,password) value(1001,"耿远博","云

计算4班","男","123456");

b. 删除表中一条记录

Delete from 表名 where 条件;

例如:delete from student where id=1001;

c. 查询表中记录

Select 字段1(列1),字段2(列2),… from 表名 where 条件;

例如:select *from student;

d. 更新表中记录(字段值)

Update 表名set 属性=值 where 条件;

update student set id=1003 where id=1002;

9、 删除表

drop table 表名;

例如:drop table student;

10、 删除数据库

drop database 数据库名;

例如:drop database stu;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: