{"id":1195,"date":"2024-09-27T17:59:47","date_gmt":"2024-09-27T09:59:47","guid":{"rendered":"https:\/\/note.ui11.cc\/?p=1195"},"modified":"2024-09-27T17:59:48","modified_gmt":"2024-09-27T09:59:48","slug":"sql%e8%af%ad%e5%8f%a5exercise","status":"publish","type":"post","link":"https:\/\/note.ui11.cc\/index.php\/2024\/09\/27\/sql%e8%af%ad%e5%8f%a5exercise\/","title":{"rendered":"sql\u8bed\u53e5exercise"},"content":{"rendered":"\n<p><code>ALTER<\/code>\u00a0\u662f SQL\uff08Structured Query Language\uff0c\u7ed3\u6784\u5316\u67e5\u8be2\u8bed\u8a00\uff09\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u4fee\u6539\u6570\u636e\u5e93\u8868\u7684\u7ed3\u6784\u3002\u4f7f\u7528\u00a0<code>ALTER<\/code>\u00a0\u8bed\u53e5\u53ef\u4ee5\u6267\u884c\u591a\u79cd\u64cd\u4f5c\uff0c\u6bd4\u5982\u6dfb\u52a0\u3001\u5220\u9664\u6216\u4fee\u6539\u5217\uff0c\u4fee\u6539\u5217\u7684\u6570\u636e\u7c7b\u578b\uff0c\u6dfb\u52a0\u6216\u5220\u9664\u7ea6\u675f\u7b49\u3002<\/p>\n\n\n\n<p>\u4fee\u6539\u8868\u540d\u7684\u8bed\u53e5\uff1a<\/p>\n\n\n\n<p>alter tab<\/p>\n\n\n\n<p>1.<\/p>\n\n\n\n<p><strong>\u6dfb\u52a0\u5217<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name\nADD column_name column_type;<\/code><\/pre>\n\n\n\n<p>.<\/p>\n\n\n\n<p><strong>\u5220\u9664\u5217<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name\nDROP COLUMN column_name;<\/code><\/pre>\n\n\n\n<p><strong>\u4fee\u6539\u5217\u7684\u6570\u636e\u7c7b\u578b<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name\nMODIFY column_name new_column_type;\n\n\n<\/code><\/pre>\n\n\n\n<p>\u6216\u8005\u5728\u67d0\u4e9b\u6570\u636e\u5e93\u7cfb\u7edf\u4e2d\u4f7f\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name\nALTER COLUMN column_name TYPE new_column_type;\n<\/code><\/pre>\n\n\n\n<p><strong>\u4fee\u6539\u5217\u540d<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name\nRENAME COLUMN old_column_name TO new_column_name;\n<\/code><\/pre>\n\n\n\n<p><strong>\u6dfb\u52a0\u4e3b\u952e\u7ea6\u675f<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name\nDROP PRIMARY KEY;\n<\/code><\/pre>\n\n\n\n<p><strong>\u6dfb\u52a0\u5916\u952e\u7ea6\u675f<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name\nADD CONSTRAINT fk_name FOREIGN KEY (column_name) REFERENCES other_table(other_column);\n<\/code><\/pre>\n\n\n\n<p><strong>\u5220\u9664\u5916\u952e\u7ea6\u675f<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ALTER TABLE table_name\nDROP FOREIGN KEY fk_name;<\/code><\/pre>\n\n\n\n<p class=\"has-white-background-color has-background\">\u67e5\u8be2\u552f\u4e00\u7ea6\u675f\u540d\u79f0\uff1a<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-link-color wp-elements-5c04ee6de7786bb2ad56572edb9b4c10\">show index from  \u8868\u540d\uff1b<\/p>\n\n\n\n<p>\u5220\u9664\u552f\u4e00\u7ea6\u675f\uff1a<\/p>\n\n\n\n<p>alter tabler \u8868\u540d drop index \u5b57\u6bb5\uff1b<\/p>\n\n\n\n<p>\u521b\u5efa\u73ed\u7ea7\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>create table \u8868\u540d \uff08\ncid int PRIMARY key,\ncname VARCHAR(50) UNIQUE\n\uff09;\n<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u5b66\u751f\u8868<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>create table if not exists student(\nid int PRIMARY KEY,\nname VARCHAR(10),\ngender enum('\u7537'\uff0c\u2018\u5973\u2019)\uff0c\nbirthday date,\nage int,\nclassId int,\nbegin_year year \n\n);\n<\/code><\/pre>\n\n\n\n<p>\u6dfb\u52a0\u5916\u952e\uff1a<\/p>\n\n\n\n<p>\u4e00\uff1a\u5f53\u8868\u5df2\u5b58\u5728\uff0c\u901a\u8fc7\u4fee\u6539\u8868\u7684\u65b9\u5f0f\u6dfb\u52a0\u5916\u952e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Alter table \u8868\u540d add foreign key(classid) references \u8868\u540d\uff08\u5b57\u6bb5\u540d\uff09<\/code><\/pre>\n\n\n\n<p>\u4e8c\uff1a\u521b\u5efa\u8868\u7684\u65f6\u5019\u6dfb\u52a0\u5916\u952e\u7ea6\u675f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7b2c\u4e00\u6b65\uff0c\u5fc5\u987b\u5148\u521b\u5efa\u4e3b\u8868\uff1a\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>create table t_class1\uff08\ncid int primary key ,\ncname carcher(50) unique\n\uff09;\n<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e8c\u6b65\uff1a\u521b\u5efa\u4ece\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>create table if not exists student01(\nid int primary key,\nname varcher(10),\ngender enum('\u7537'\uff0c'\u5973')\uff0c\nbirthday date,\nage int ,\nclass_id int ,\nbegin year,\nforeign key (calss_id) references  t_class1(cid)\n);<\/code><\/pre>\n\n\n\n<p>\u67e5\u770b\u5916\u952e\u7ea6\u675f\u540d<\/p>\n\n\n\n<p>select *from information_schema.key_column_usage where table_schema=&#8217;school_db1&#8242; and table_name=&#8217;student1&#8242;<\/p>\n\n\n\n<p>\u5220\u9664\u5916\u952e\u7ea6\u675f\uff1a<\/p>\n\n\n\n<p>alter table student1 drop foreign key student1_ibfk_1;<\/p>\n\n\n\n<p>#\u975e\u7a7a\u7ea6\u675f<\/p>\n\n\n\n<p>\u6dfb\u52a0\u975e\u7a7a\u7ea6\u675f<\/p>\n\n\n\n<p>\u65b9\u6cd5\u4e00\uff1a<\/p>\n\n\n\n<p>create table t_stu1(<\/p>\n\n\n\n<p>id int primary key,<\/p>\n\n\n\n<p>name varcher(50) not null<\/p>\n\n\n\n<p>)<\/p>\n\n\n\n<p>desc t_stu;<\/p>\n\n\n\n<p>\u65b9\u6cd5\u4e8c\uff1a<\/p>\n\n\n\n<p>ALTER table t_stu MODIFY id int not null;<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color wp-elements-cd4f456cfd3bc71bd0e488e0a86c93b4\">\u5728SQL\u4e2d\uff0c<code>MODIFY<\/code>\u00a0\u5173\u952e\u5b57\u901a\u5e38\u4e0e\u00a0<code>ALTER TABLE<\/code>\u00a0\u8bed\u53e5\u4e00\u8d77\u4f7f\u7528\uff0c\u7528\u4e8e\u4fee\u6539\u8868\u4e2d\u73b0\u6709\u5217\u7684\u5b9a\u4e49\u3002\u8fd9\u5305\u62ec\u6539\u53d8\u5217\u7684\u6570\u636e\u7c7b\u578b\u3001\u5217\u7684\u540d\u79f0\u3001\u6dfb\u52a0\u6216\u5220\u9664\u5217\u7684\u9ed8\u8ba4\u503c\u7b49\u3002<\/p>\n\n\n\n<p>\u5220\u9664\u975e\u7a7a\u7ea6\u675f<\/p>\n\n\n\n<p>ALTER ATBLE t_stu modify id int;<\/p>\n\n\n\n<p>#####\u81ea\u589e\u7ea6\u675f<\/p>\n\n\n\n<p>\u6dfb\u52a0\u81ea\u589e\u7ea6\u675f<\/p>\n\n\n\n<p>\u65b9\u6cd5\u4e00\uff1a<\/p>\n\n\n\n<p>create table t_stu2\uff08<\/p>\n\n\n\n<p>id int primary key AUTO_INCREMENT,<\/p>\n\n\n\n<p>name varcher(50)  not null<\/p>\n\n\n\n<p>\uff09;<\/p>\n\n\n\n<p>\u65b9\u6cd5\u4e8c\uff1a<\/p>\n\n\n\n<p>alter table t_stu2 modify id int primary key AUTO_INCREMENT;<\/p>\n\n\n\n<p>DESC t_stu;<\/p>\n\n\n\n<p>\u5220\u9664\u81ea\u52a8\u9012\u589e\uff1a<\/p>\n\n\n\n<p>alter table t_stu modify id int ;<\/p>\n\n\n\n<p>###<\/p>\n\n\n\n<p>check \u7ea6\u675f\uff1a<\/p>\n\n\n\n<p>\u6dfb\u52a0check\u7ea6\u675f\uff1a<\/p>\n\n\n\n<p>WAY \u4e00\uff1a<\/p>\n\n\n\n<p>create table if not exists student2(<\/p>\n\n\n\n<p>id int primary key ,<\/p>\n\n\n\n<p>name varcher(10) check (LENGTH)>=2\uff0c<\/p>\n\n\n\n<p>gender ENUM(&#8216;\u7537&#8217;\uff0c&#8217;\u5973&#8217;)\uff0c<\/p>\n\n\n\n<p>birthday date ,<\/p>\n\n\n\n<p>age int check(age >=18 and age&lt;=25)\uff0c<\/p>\n\n\n\n<p>classId int,<\/p>\n\n\n\n<p>begin_year year,<\/p>\n\n\n\n<p>froeign key \uff08classid\uff09 references t_class1(cid)<\/p>\n\n\n\n<p>);<\/p>\n\n\n\n<p>\u67e5\u8be2check\u7ea6\u675f\uff1a<\/p>\n\n\n\n<p>select *from information_schema.check_constraints;<\/p>\n\n\n\n<p>\u5220\u9664checkl\u7ea6\u675f<\/p>\n\n\n\n<p>Alter table student2 drop check student2_chk_2;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ALTER\u00a0\u662f SQL\uff08Structured Query Language\uff0c\u7ed3\u6784\u5316\u67e5\u8be2\u8bed\u8a00\uff09\u4e2d\u7684\u4e00\u4e2a\u5173\u952e\u5b57\uff0c\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1195","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/1195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/comments?post=1195"}],"version-history":[{"count":1,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/1195\/revisions"}],"predecessor-version":[{"id":1196,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/posts\/1195\/revisions\/1196"}],"wp:attachment":[{"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/media?parent=1195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/categories?post=1195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/note.ui11.cc\/index.php\/wp-json\/wp\/v2\/tags?post=1195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}