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;
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;
相关文章推荐
- mysql外键(FOREIGN KEY)的简单使用
- 一天一点MySQL复习——存储过程
- 在Dos下初步使用MySQL
- MySql 中文乱码
- MySql 中文乱码
- MySQL数据类型
- MySQL for MAC 重置root密码
- mysql批量导入,导出
- 【Mysql sql inject】【入门篇】SQLi-Labs使用 part 1【01-11】
- mysql 中的语法执行顺序
- Windows10 64位安装解压版mysql-5.7.12-winx64的错误总结- MySQL服务无法启动
- 用批处理启动MySQL命令行工具
- 常用mysql数据库引擎——MyISAM和InnoDB区别
- mysql学习总结
- MYSQ系列-MYSQL基础增强(Mysql基本语句)
- MySQL基本操作命令
- 3.1 mysql客户端工具
- Mysql在高并发情况下,防止库存超卖而小于0的解决方案
- mysql基础语法
- 【J2EE核心开发学习笔记001】通过JDBC进行简单的增删改查(以MySQL为例)