Mysql資料庫中對錶操作sql語句總結

來源:互聯網
上載者:User

   --建立資料庫

  create database school

  --開啟資料庫

  use school

  --建立表

  create table student

  (

  id int,

  name varchar(20),

  sex char(2),

  age int,

  date datetime,

  info text,

  bak varchar(500)

  )

  --查看錶結構

  exec sp_help student

  --修改、添加列(欄位)

  alter table student

  add tel varchar(20)

  --刪除列(欄位)

  alter table student

  drop column bak

  --屬性修改

  --修改列名(欄位名)

  exec sp_rename 'student.sex','sex2'

  --修改類型

  alter table student

  alter column age char(20)

  --刪除表

  drop table student

  -------------------------------表(結構):--------------------------------------------

  ----建立表,查看錶結構,修改(增加列、刪除列、屬性(如姓名、年齡),刪除表)

  ---資料的完整性:主鍵約束、唯一約束、檢查性約束、預設約束、外鍵約束

  create table biao

  (

  id int primary key,

  name varchar(20),

  sex char(2) check(sex='男' or sex='女'),

  age int,

  date datetime,

  info text,

  tel char(16) unique,

  bak varchar(500) default '我是學生'

  )

  create table grade

  (

  id int not null,

  name varchar(20),

  sex char(2),

  age int,

  date datetime,

  info text,

  bak varchar(500)

  )

  alter table grade

  add tel char(16)

  ---增加主鍵

  alter table grade

  add constraint aa primary key(id)

  ---添加唯一性限制式

  alter table grade

  add constraint bb unique(tel)

  ---查看約束

  exec sp_helpconstraint grade

  ---添加檢查性約束

  alter table grade

  add constraint sex check(sex='男' or sex='女')

  ---添加預設約束

  alter table grade

  add constraint ccc default '我是好學生' for bak

  ---刪除約束

  alter table grade

  drop constraint ccc

  -----------------------------添加約束的格式------------------------------------------

  ---alter table 表名

  ---add constraint 約束名(別名(任意取)) 約束關鍵字

  ----作業題,7.28-----

  create table shop_jb

  (

  id int primary key,

  namel varchar(20),

  spec varchar(20),

  stock int,

  price float,

  datel datetime default '2010-7-6'

  )

  create table shop_yw

  (

  ywid int primary key,

  name2 varchar(20),

  sex char(2) check(sex='男' or sex='女'),

  age int,

  tel varchar(18) unique,

  address varchar(20)

  )

  create table shop_xs

  (

  id int not null,

  sale char(20),

  quantity char(20),

  date2 datetime default '2010-5-3',

  ywid int

  foreign key(id) references shop_jb,

  foreign key(ywid) references shop_yw

  )

  例如:

  修改表expert_info中的欄位birth,允許其為空白

  >alter table expert_info change birth birth varchar(20) null;

  1.增加一個欄位(一列)

  alter table table_name add column column_name type default value; type指該欄位的類型,value指該欄位的預設值

  例如:alter table mybook add column publish_house varchar(10) default '';

  2.更改一個欄位名字(也可以改變類型和預設值)

  alter table table_name change sorce_col_name dest_col_name type default value; source_col_name指原來的欄位名稱,dest_col_name

  指改後的欄位名稱

  例如:alter table Board_Info change IsMobile IsTelphone int(3) unsigned default 1;

  3.改變一個欄位的預設值

  alter table table_name alter column_name set default value;

  例如:alter table book alter flag set default '0';

  4.改變一個欄位的資料類型

  alter table table_name change column column_name column_name type;

  例如:alter table userinfo change column username username varchar(20);

  5.向一個表中增加一個列做為主鍵

  alter table table_name add column column_name type auto_increment PRIMARY KEY;

  例如:alter table book add column id int(10) auto_increment PRIMARY KEY;

  6.資料庫某表的備份,在命令列中輸入:

  mysqldump -u root -p database_name table_name > bak_file_name

  例如:mysqldump -u root -p f_info user_info > user_info.dat

  7.匯出資料

  select_statment into outfile"dest_file";

  例如:select cooperatecode,createtime from publish limit 10 into outfile"/home/mzc/temp/tempbad.txt";

  8.匯入資料

  load data infile"file_name" into table table_name;

  例如:load data infile"/home/mzc/temp/tempbad.txt" into table pad;

  9.將兩個表裡的資料拼接後插入到另一個表裡。下面的例子說明將t1表中的com2和t2表中的com1欄位的值拼接後插入到tx表對應的

  欄位裡。

  例如:insert into tx select t1.com1,concat(t1.com2,t2.com1) from t1,t2;

  10,刪除欄位

  alter table form1 drop column 列名;

  補充一個:

  PHP操作MySQL對錶增加一列

  於已經建立好的資料庫,在一個已經有欄位的表內新加欄位可用以下方法:

  mysql_query(“ALTER TABLE `表名` ADD `欄位` 欄位類型”) or die(mysql_error());

  例如,對錶article添加欄位keywords

  代碼:

 代碼如下  
<?php
$link = mysql_connect($servername,$dbusername,$dbpassword);
if (mysql_select_db($dbname)) {
if ($link) {
echo “connect succeed”;
mysql_query(“ALTER TABLE `article` ADD `keywords` varchar(100) NOT NULL default ””) or die(mysql_error());
echo “Add succeed”;
} else {
echo “connect failed”;
}
mysql_close($link);
}
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.