`
loven_11
  • 浏览: 58085 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

sql的一些操作命令

    博客分类:
  • db
阅读更多

一、mysql篇
将mysql安装目录的bin加入到系统环境变量中;

---进入mysql: 
	mysql -u root -p
---查看已经存在的库:
	show database;
---创建库:
	create database test;
---使用库:
	use test;
---创建表:
	create table t (id int not null, name char(20) );
---显示表的结构:
                describe mytable; 
---插入数据:
	insert into t select 1,'1' union select 2,'2';
---mysql alter 语句用法, 添加 修改 删除字段:
   ----主键
    alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);
   ----增加一个新列
	alter table t2 add d timestamp;
	alter table infos add ex tinyint not null default '0';
   ----删除列
    alter table t2 drop column c;
   ----重命名列
    alter table t1 change a b integer;
   ----改变列的类型
    alter table t1 change b b bigint not null;
    alter table infos change list list tinyint not null default '0';
   ----重命名表
    alter table t1 rename t2;
   ----加索引
    alter table tablename change depno depno int(5) not null;
    alter table tablename add index 索引名 (字段名1[,字段名2 …]);
    alter table tablename add index emp_name (name);
   ----加主关键字的索引
    alter table tablename add primary key(id);
   ----加唯一限制条件的索引
    alter table tablename add unique emp_name2(cardnumber);
   ----删除某个索引
    alter table tablename drop index emp_name;
----修改表:
   ----增加字段:
    ALTER TABLE table_name ADD field_name field_type;
   ----修改原字段名称及类型:
    ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;
   ----删除字段:
    ALTER TABLE table_name DROP field_name; 

恢复数据:

MySQL -h localhost -u root -p mydb2 < e:\MySQL\mydb2.sql 

备份数据:

MySQLdump -h localhost -u root -p mydb mytable>e:\MySQL\mytable.sql 

创建database

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

 

GRANT ALL PRIVILEGES ON test.* TO 'root'@'localhost'
  IDENTIFIED BY 'goodsecret' 

二、sql server篇

--创建测试表 
	create table t(
	  i int identity,
	  d int NOT NULL default(0)
	) 
--插入测试数据 
	insert t 
		      select 6 
		union select 2 
		union select 3 
--查看测试数据 		
	select * from t 
	sp_helpconstraint t 
--本例找到DF__t__d__0702021
	alter table t drop constraint DF__t__d__07020F21 
--命令成功完成 
	alter table t drop column d 
--查看删除列后的表 
	select * from t 
--删除测试表
	drop table t 

 
三、postgre篇

 

create table t (  ---建表
	id integer not null,
	name character(100)
)
insert into t select 1 union select 2 union select 3 -- 插入数据
alter table t drop column name --删除列
alter table t add column name character(100) null default null --添加列
delete from t -- 删除数据

insert into t (id,name) --插入数据
select 4,'12' union select 5,'23'
select * from t --查看
---alter table t alter column name character(101) null;
drop table t; --删除表

 

朋友的网站 http://woods2010.cn.alibaba.com/ 专业电子器件代购

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics