桓楠百科网

编程知识、经典语录与百科知识分享平台

mysql 数据库基本操作(mysql数据库的完整步骤)

mysql 数据库基本操作

## 1、创建五张表

-- user 表:后台用户表

-- product 表:产品表

-- account 表:客户账户表

-- product_account 表 : 客户购买表

-- customer 表 : 客户表

## 2、创建表 SQL 语句

注意:下面 SQL 语句是直接在控制台创建表

即:WIN+R --> cmd --> mysql -uroot -p密码)

mysql> create database webtest;

mysql> use webtest;

mysql> create table user (

uid int primary key auto_increment,

username varchar(20),

password varchar(20),

nickname varchar(20)

);

Query OK, 0 rows affected (0.76 sec)



## 3、插入数据

insert into user values (null,'tom','123','张三');

insert into user values (null,'lisi','123','李四');

insert into user values (null,'wangwu','123','王五');

insert into user values (null,'zhaoliu','123','赵六');

insert into user values (null,'zhouqi','123','周七');



## 4、创建表 -- 图形界面工具 SQLyong -- 执行 SQL 查询 语句

下面 SQL 语句是在 SQLyog ULtimate - MySQL GUI(v12.09-64bit)创建

注意:`id` 等两边不是单引号,而 Tab 上面符号。

(图形界面 Navicat SQLyog 等软件,连接 mysql 数据库,密码加密方式不同)

需要先登录 mysql 数据库,通过下面的语句修改用户的密码方式和重置密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '12311';

select User,Host from mysql.user;

才能通过图形界面 Navicat SQLLyog 等软件,连接 mysql 数据库


**--- 创建 p2p 数据库:**

mysql> create database p2p;

**--- 使用 p2p 数据库:**

mysql> use p2p;

**--- 创建 user 数据表:**

create table `user` (

`id` int(11) not null auto_increment,

`username` varchar(20) default null,

`password` varchar(20) default null,

primary key (`id`)

)ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;


**--- 创建 product 数据表:**

CREATE TABLE `product` (

`id` INT(11) NOT NULL AUTO_INCREMENT,

`proNum` VARCHAR(20) DEFAULT NULL,

`proName` VARCHAR(20) DEFAULT NULL,

`proLimit` INT(11) DEFAULT NULL,

`annualized` DOUBLE DEFAULT NULL,

`releaseDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

PRIMARY KEY (`id`)

) ENGINE=INNODB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

**--- 创建 customer 数据表:**

create table `customer` (

`id` int(11) not null auto_increment,

`c_name` varchar(20) default null,

`email` varchar(50) default null,

`email_status` int(11) default null,

`password` varchar(20) default null,

primary key (`id`)

) engine=InnoDB auto_increment=10 default charset=utf8;

**--- 创建 account 数据表:**

create table `account` (

`id` int(11) not null auto_increment,

`total` double default null,

`balance` double default null,

`interest` double default null,

`c_id` int(11) default null,

primary key (`id`),

key `c_id` (`c_id`),

constraint `account_ibfk_1` foreign key (`c_id`) references `customer` (`id`)

) engine=InnoDB auto_increment=2 default charset=utf8;

**--- 创建 product_account 数据表:**

create table `product_account` (

`id` int(11) not null auto_increment,

`pa_num` varchar(20) default null,

`pa_date` timestamp not null default current_timestamp on update current_timestamp,

`c_id` int(11) default null,

`p_id` int(11) default null,

primary key (`id`),

key `c_id` (`c_id`),

key `p_id` (`p_id`),

constraint `product_account_ibfk_1` foreign key (`c_id`) references `customer` (`id`),

constraint `product_account_ibfk_2` foreign key (`p_id`) references `product` (`id`)

) engine=InnoDB default charset=utf8;

## 5、向 user 表插入数据

mysql> insert into user values (null,'tom','123');


## 6、查询 user 表数据

mysql> select * from user;



## 7、其他操作

--- 清空 user 表数据:

truncate user;

![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/b2debfe42fc444eab0677662f425f70a.png)

--- 给某一张表添加一个列

ALTER TABLE `user` ADD `username` TEXT NOT NULL;

--- 例如

alter table user add money Float(11) NULL default 6;


--- 建表时 给某列添加默认值

create table tablename (columnname datatype default defaultvalue);


--- 已建表后修改某表

alter table tablename alter column columnname set default defaultvalue;


--- 给 user 表的 username 添加唯一约束

Alter table user add unique(username);


--- 更改 app_activity 表中 digest 的字段,允许为空

ALTER TABLE app_activity MODIFY digest VARCHAR(255) null;


--- 删除某一字段

ALTER TABLE mytable DROP 字段名;


--- 修改列的类型

alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空];

如: ALTER TABLE product CHANGE releaseDate releaseDate TIMESTAMP NULL;


--- 更改表名

rename table 旧表名 to 新表名;


--- 添加 utf8 编码库,删除一个数据库

CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

DROP database test;


--- 删除一个索引

alter table 表名 drop index 索引列的名字;


--- 查看表的字段信息:

desc 表名;


--- 查看表的所有信息:

show create table 表名;


--- 添加主键约束:

alter table 表名 add constraint 主键 (形如:PK_表名) primary key 表名(主键字段);


--- 添加外键约束:

alter table 从表 add constraint 外键(形如:FK_从表_主表) foreign ey 从表(外键字段) references 主表(主键字段);


--- 删除主键约束:

alter table 表名 drop primary key;


--- 删除外键约束:

alter table 表名 drop foreign key 外键(区分大小写);


--- 删除唯一约束(username该列上有一个唯一约束,app_user为表名) :

drop index username on app_user;

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言