mysql基本语句

	—— 显示所有数据库  
	show databases;  
	 
	—— 创建数据库  
	CREATE database first;  
	 
	—— 删除数据库  
	DROP database first;  
	 
	——  显示 first 数据库里的所有表  
	use first;  
	SHOW tables;  
	 
	   
	 
	—— 创建表格   
	CREATE TABLE Test  
	(  
	 ID   Int,  
	 price DECIMAL,  
	            —— 指定默认值  
	 na    varchar(30)  default 'xxx'  
	)  
	 
	   
	—— 修改表格 增加列  
	alter table test  
	add 
	(  
	 num  int 
	)  
	 
	   
	—— 修改表格 修改列  
	alter table test  
	modify num varchar(30);  
	 
	   
	—— 修改表格 删除列  
	alter table test  
	drop num;  
	 
	—— mysql 专用  重命名表格  
	alter table test  
	rename mytest;  
	 
	   
	—— mysql 专用 重命名列名 并 修改列属性  
	alter table mytest  
	change na nan int;  
	 
	   
	—— 删除表格  
	drop table mytest  
	 
	   
	—— 删除表格的所有数据  
	truncate mytable  
	 
	 
	—— 定义非空约束  
	CREATE table test  
	(  
	 name varchar(30) not null,  
	 num  INT         null 
	)  
	 
	 —— 修改约束  
	alter table test  
	modify num int  not null 
	 
	   
	—— 唯一约束 unique  
	CREATE table test1  
	(  
	 name varchar(30) not null unique 
	)  
	 
	   
	 
	CREATE table test2  
	(  
	 name varchar(30) not null ,  
	 num INT  not null,  
	—— 表级定义约束 unique  
	unique(name),  
	 Constraint test2_uk unique(name,num)  
	)  
	 
	 
	—— mysql 专用 删除约束  
	alter table test1  
	drop INDEX name;  
	 
	 
	—— 主键约束  
	create table mytest  
	(  
	    id  INT primary key 
	);  
	 
	create  table mytest2  
	(  
	    id int ,  
	—— 表级定义主键约束 MYSQL 不能更改主键约束名字 即名字依然为 primary  
	    constraint a PRIMARY key(id)  
	);  
	 
	—— 删除主键约束  
	alter table mytest  
	drop PRIMARY key;  
	 
	create table mytest  
	(  
	          —— 定义自动增长  
	    id int AUTO_INCREMENT PRIMARY KEY 
	)  
	 
	 
	—— MYSQL 列定义外键不会生效  
	create table mytest3  
	(  
	    id int AUTO_INCREMENT PRIMARY KEY,  
	    fid int REFERENCES mytest2(id)  
	)  
	 
	—— MYSQL 表级定义 外键才会生效  
	create table mytest4  
	(  
	    id int AUTO_INCREMENT PRIMARY KEY,  
	    fid int,   
	                        —— 删除联系的表的记录  
	    foreign key (fid) REFERENCES mytest2(id) on delete cascade 
	                        —— on delete set null  
	)  
	 
	—— 创建视图  
	create or replace view myview  
	as 
	select *  
	From mytest  
	—— 不能修改视图数据  
	with check option;  
	 
	—— 删除视图  
	drop view myview  
	 
	 
	 
	—— 添加数据  
	insert into a  
	values (null,'ddd')aa  
	 
	 
	—— 添加多行数据 mysql 专用  
	insert into b  
	values(null,2,'xxxxx'),  
	    (null,2,'xsxsxs')  
	 
	 
	—— 修改数据  
	update a   
	set name = 'hqw' 
	where ida = 1  
 
        —— 删除数据  
         delete from b  
        where idb = 2  
	      


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值