基本管理

SQL基本操作

创建数据表

USE miao;
-- 创建表tb_prodect
CREATE TABLE tb_prodect(
id INT(11) NOT NULL AUTO_INCREMENT,
prodect_name VARCHAR(30) DEFAULT NULL,
prodect_num INT(11) DEFAULT NULL,
prodect_price DECIMAL(8,2) DEFAULT NULL,
prodect_pic VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (id)
);
-- 查看刚刚新建的表
SHOW TABLES;
DESCRIBE tb_prodect;

查看表结构

DESCRIBE tb_prodect;

删除表

drop table tb_prodect,tb2..

安全复制表

用户可以将已有表中的全部或部分数据赋值到新表中:

create table new_table select * from tb_prodect;

注意:使用select赋值是不能赋值键(key)的。

  • 赋值特定的列或特定的行
create table new_table2 select id,prodect_name from tb_prodect limit 1;

  • 使用like方式赋值,只能赋值表结构,包含含键
create table new_table4 like tb_prodect;

安全修改表

当前表结构

新增字段
alter table tb_prodect
	add column click_num int null after prodect_pic;

当前表结构:

删除字段(alter ...drop column)
alter table tb_product
	drop column click_num;
修改字段(alter ...change| modify )
  • alter ... change colName newName int null

    alter table tb_product
    	change prodect_num prodect_num mediumint(11) null;
    
  • alter ... modify

alter table tb_prodect
	modify prodect_num mediumint(11) null;
添加主键
  • 删除主键
alter table tb_prodect
	change id id int(11) not null,
	drop primary key;
  • 将id设置为主键
alter table tb_prodect
	add primary key(id);
原文地址:https://www.cnblogs.com/zhuxiang1633/p/10864370.html